SCCM/SMS & PowerShell: Introduction (3/3)
Pour cette dernière partie, je vais vous montrer un autre exemple d’utilisation de powershell avec Sccm. Nous allons voir comment customiser certaines options d’un advertisement. |
A. Déscription des besoins
Voici les options que nous allons configurer en script Powershell:
Grâce au SDK Sccm, nous pouvons retrouver ces options au niveau de la classe SMS_Advertisement
Cette classe possède la propriété RemoteClientFlags qui est celle qui nous intéresse.
Voici sa décription (extrait provenant de MSDN):
Data type: UInt32
Access type: Read/Write
Qualifiers: [bits, ResID(822), ResDLL(« SMS_RSTT.dll »)]
Flags specifying how the program should run when the client is connected either locally or remotely to a distribution point. Possible values are listed below. The default value is 48.
Hex (Bit) | Description |
0x00000001 (0) | BATTERY_POWER. Run the program by using battery power. This value is currently unused. |
0x00000002 (1) | RUN_FROM_CD. Run the program from CD. This value is currently unused. |
0x00000004 (2) | DOWNLOAD_FROM_CD. Download the program from CD. This value is currently unused. |
0x00000008 (3) | RUN_FROM_LOCAL_DISPPOINT. Run the program from the local distribution point. |
0x00000010 (4) | DOWNLOAD_FROM_LOCAL_DISPPOINT. Download the program from the local distribution point. |
0x00000020 (5) | DONT_RUN_NO_LOCAL_DISPPOINT. Do not run the program if there is no local distribution point. |
0x00000040 (6) | DOWNLOAD_FROM_REMOTE_DISPPOINT. Download the program from the remote distribution point. |
0x00000080 (7) | RUN_FROM_REMOTE_DISPPOINT. Run the program from the remote distribution point. |
0x00000100 (8) | DOWNLOAD_ON_DEMAND_FROM_LOCAL_DP. Download the program on demand from the local distribution point. This is only applicable for task sequences. |
0x00000200 (9) | DOWNLOAD_ON_DEMAND_FROM_REMOTE_DP. Download the program on demand from the remote distribution point. This is only applicable for task sequences. |
0x00000400 (10) | BALLOON_REMINDERS_REQUIRED. Balloon reminders are required. |
0x00000800 (11) | RERUN_ALWAYS. Always rerun the program. |
0x00001000 (12) | RERUN_NEVER. Never rerun the program. |
0x00002000 (13) | RERUN_IF_FAILED. Rerun the program if execution previously failed. |
0x00004000 (14) | RERUN_IF_SUCCEEDED. Rerun the program if execution previously succeeded. |
Avant même de pouvoir faire quoique ce soit en powershell, il va nous falloir calculer la bonne valeur à donner à notre propriété.
Voici ce dont nous avons besoins:
En suivant les valeur binaires présentes dans le tableau, cela nous donne:
0x00000800 + 0x00000020 + 0x00000008 = 0x00000828
Il ne nous reste plus qu’à convertir cette valeur binaire en valeur décimale: 2088 (merci à la calculatrice scientifique )
Il est temps de passer au script.
B. En Powershell, cela donne ceci
Nous allons créer une query nous permettant de ressortir la liste des advertisements qui nous intéresse. Dans cet exemple, je désire avoir tout ceux dont le nom commence par A_
Voici la fonction qui va nous permettre de modifier les valeurs de chaque advertisement:
write-host $oAdv.AdvertisementName
write-host "Old RCF value" $oAdv.RemoteClientFlags
try{
$oAdv.RemoteClientFlags = $rcf
$oAdv.Put()
$cpt++
}catch {
Write-Host -ForegroundColor Red "CRITICAL ERROR: " $Error[0]
}
write-host -ForegroundColor Green "New RCF value" $oAdv.RemoteClientFlags
}
C. Exemple complet
#
#NAME: PS-AdvRCF
#
#VERSION: 1.0
#AUTHOR: Malfroidt Olivier
#DATE: 07/06/2011
#
#COMMENT:
#
#=========================================================================
#*****************
#Arguments
#*****************
param ([string]$sccmSrv, [string]$siteCode, [int]$rcf, [string]$whatIf, [string]$file)
#*****************
#Functions
#*****************
Function HelpMsg()
{
$helpText=@"
Name: PS-AdvRCF - v1.0 Release
Usage:
PS-AdvRCF -sccmSrv [server name] -siteCode [SCCM site code] -rcf [remoteClientFlags value] [-whatIf [y/n] -file [File Path]]
Options:
Mandatory
-sccmSrv [ServerName]: SCCM Server name.
-siteCode [SMS Site code]: SCCM Site code.
-rcf [remoteClientFlags value]: SCCM RemoteClientFlags value.
Optional
-whatIf: If you want to test first your modification.
-file [File Path]: File path to store the whatIf switch result.
Exemple:
PS-AdvRCF -sccmSrv MyServer -siteCode 00A -rcf 2088
PS-AdvRCF -sccmSrv MyServer -siteCode 00A -rcf 2088 -whatIf y -file c:\Temp\export.txt
"@
$helpText
exit
}
#*****************
#Variables
#*****************
$nameSpace = "root\SMS\Site_" + $siteCode
$wmiQuery = "Select * from SMS_Advertisement where AdvertisementName like 'A[_]%'"
$cpt = 0
#*****************
#Main
#*****************
try{
if (($sccmSrv -ne "") -and ($siteCode -ne "") -and ($rcf -ne "")) {
If (($whatIf -eq "y") -and ($file -ne "")){
try{
Get-WmiObject -ComputerName $sccmSrv -Namespace $nameSpace -Query $wmiQuery | Select-Object AdvertisementID, AdvertisementName, RemoteClientFlags | Out-File -FilePath $file
}catch{
Write-Host -ForegroundColor Red "CRITICAL ERROR: " $Error[0]
}
}else{
$listAdv = Get-WmiObject -ComputerName $sccmSrv -Namespace $nameSpace -Query $wmiQuery
foreach ($oAdv in $listAdv){
write-host $oAdv.AdvertisementName
write-host "Old RCF value" $oAdv.RemoteClientFlags
try{
$oAdv.RemoteClientFlags = $rcf
$oAdv.Put()
$cpt++
}catch {
Write-Host -ForegroundColor Red "CRITICAL ERROR: " $Error[0]
}
write-host -ForegroundColor Green "New RCF value" $oAdv.RemoteClientFlags
}
write-host -foregroundColor Blue "$cpt advertisement(s) modified successfully."
}
}else{
HelpMsg
}
} catch {
Write-Host -ForegroundColor Red "CRITICAL ERROR: " $Error[0]
}
Sources:
http://msdn.microsoft.com/en-us/library/cc146108.aspx
A la suite de ces 3 articles, j’espère vous avoir un peu éclairé sur l’utilisation de SCCM et Powershell.
Enjoy !!
Leave a Reply