Script – SCCM – Recuperation et export des Distribution points

par | Déc 27, 2023 | PowerShell, SCCM, Script, sql | 0 commentaires

Le script ci-dessous requete la base SQL de SCCM pour lister et exporter en CSV, les points de distribution SCCM

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<pre class="wp-block-syntaxhighlighter-code">########################################################################################################
### REQUETE LA BASE SQL DE SCCM POUR OBTENIR LA LISTE DES POINTS DE DISTRIBUTION SCCM.
### EXPORT DES RESULTAT EN FICHIER CSV  #####
########################################################################################################
 
# AUTHOR: CJOURDAN
 
<#
 
    .SYNOPSIS
        REQUETE LA BASE SQL DE SCCM POUR OBTENIR LA LISTE DES POINTS DE DISTRIBUTION SCCM
        EXPORT DU RESULTAT EN FICHIER CSV.
 
    .PARAMETER 
        SQLInstance : Instance SQL
        SQLDB : Instance SQL
        SQLQuery : Requete SQL
        ExportFolder : Dossier d'export du fichier CSV
        LogFolder : Chemin du dossier où creer le log du script
 
  
    .EXAMPLE
     .\SCCM_SCCM_Distribution_Points.ps1 -SQLInstance SQLSCCM\SCCM -SQLDB CM_BIM -ExportFolder C:\MyExport -LogFolder C:\MyLogs
#>
 
 
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,HelpMessage="Instance SQL")]
[string]$SQLInstance,
 
[Parameter(Mandatory=$true,HelpMessage="Base SQL")]
[string]$SQLDB,
 
[Parameter(Mandatory=$false,HelpMessage="Requete SQL")]
[string]$SQLQuery= $("/* --- ALL SCCM DISTRIBUTION POINTS --- */
 
Declare @UserSIDs As Varchar(25);
Set @UserSIDs = 'Disabled'
 
SELECT DISTINCT                
        dp.ServerName AS Distribution_Point
         
from fn_rbac_SystemResourceList(@UserSIDs)  as sys
join fn_rbac_DistributionPointInfo(@UserSIDs)  as dp
on sys.NALPath = dp.NALPath
where sys.RoleName = 'SMS Distribution Point'
 
"),
 
 
[Parameter(Mandatory=$true,HelpMessage="Dossier d'export du fichier CSV")]
[string]$ExportFolder,
 
[Parameter(Mandatory=$true,HelpMessage="Chemin du dossier où creer le log du script")]
[string]$LogFolder
 
)
 
 
# SCRIPT NAME
$ScriptName = "SCCM_SCCM_Distribution_Points.ps1"
 
 
# LogName = ScriptName without extension
$Log = $ScriptName.Split('.')[0]
 
 
### FUNCTIONS
 
# Function Write-Log
 
 
 
function Write-Log
{
    <#
    .SYNOPSIS
        This function creates or appends a line to a log file.
  
    .PARAMETER  Message
        The message parameter is the log message you'd like to record to the log file.
  
    .EXAMPLE
        PS C:\> Write-Log -Message 'Value1'
        This example shows how to call the Write-Log function with named parameters.
    #>
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$Message,
        [Parameter(Mandatory)]
        [string]$LogPath,
        [Parameter(Mandatory)]
        [string]$LogName
         
    )
      
    try
    {
        $DateTime = Get-Date -Format ‘MM-dd-yy HH:mm:ss’
        Add-Content -Value "$DateTime # $Message" -Path "$LogPath\$LogName.log"
    }
    catch
    {
        Write-Error $_.Exception.Message
    }
}
 
 
Function GetSQLData {
 
<#
    .SYNOPSIS
        This function query SQL Database and get Data
  
    .PARAMETER 
        SQLInstance: Instance SQL.
        SQLDB: Base SQL.
        SQLQuery: Requete SQL.
 
  
    .EXAMPLE
        GetSQLData -SQLInstance "MyInstance" -SQLDB "MyDB" -SQLQuery "Select * from MyView"
    #>
 
 
[CmdletBinding()]
    Param(
        [Parameter(Mandatory=$false)]
        [string[]]
        $SQLInstance,
        [Parameter(Mandatory=$false)]
        [string[]]
        $SQLDB,
        [Parameter(Mandatory=$false)]
        [string[]]
        $SQLQuery
         
        )
 
$connectionString = "Data Source=$SQLInstance;"+"Integrated Security=SSPI;"+"Initial Catalog=$SQLDB"
 
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($SQLQuery,$connection)
$connection.Open()
 
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
 
$connection.Close()
$dataSet.Tables
 
}
 
 
 
 
 
 
# EXECUTE Query ($SQLQuery)
    Write-Log -Message "Execution of GetSQLData on $SQLDB" -LogPath $LogFolder -LogName $Log
    $Result =
    Try {
        GetSQLData -SQLInstance $SQLInstance -SQLDB $SQLDB -SQLQuery $SQLQuery
        }
    Catch
        {
        $Message = "ERROR DURING EXECUTION OF QUERY"
        Write-Host -F Red $Message
        Write-Log -Message "$Message - $($Error[0].Exception)" -LogPath $LogFolder -LogName $Log
        Exit 1
        }
 
 
########################################
# SOUS-REGROUPEMENTS
########################################
 
 
# All SCCM DP
$AllSCCMDP = $Result | ConvertTo-Csv -Delimiter ';' -NoTypeInformation | foreach {$_.replace('"','')}
 
$AllSCCMDP
 
# EXPORTS TO TXT FILES
$AllSCCMDP  |  Out-File -FilePath "$ExportFolder\All_SCCMDP.txt" -Force
 
 
 
# DISPLAY SUCCESS
$Message = "--- EXECUTION OK ---"
Write-Host -F Green $Message
Write-Log -Message $Message -LogPath $LogFolder -LogName $Log
 
 
</pre>


 

0 commentaires

Soumettre un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *