Advertisement
tseanlaws

sccm+process

Oct 15th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. # // Website: http://www.marco-difeo.de / https://marco-difeo.de/2011/10/13/powershell-sccm-readvertise-a-previously-installed-softwarepackage-remotly/
  2. # // Scriptname: rerun_sccm_advertisement_on_client.ps1
  3. # //
  4. # // Usage: This script can be modified to fit your needs
  5. # // Pingback: http://marco-difeo.de/2011/10/13/powershell-sccm-readvertise-a-previously-installed-softwarepackage-remotly/
  6. # // #######################################################################################
  7. # // Script begin
  8. # // #######################################################################################
  9. # //
  10. # //
  11. # // Should check if the computer is already online or not.
  12. # // Your SCCM Client!!!! advertisementID (you can get this from the SCCM reports)
  13. $ADVID = "XXX00001"
  14. # // Computername on which the magic takes place ( . is the local computer)
  15. $Computer = "computer01"
  16.  
  17. trap [Exception] {
  18. write-host $("ERROR: " + $_.Exception.Message) -ForegroundColor red
  19. continue;
  20. }
  21. try
  22. {
  23. # // Check, if computer is online!
  24. if ($computer -eq "." -or $(new-object system.net.networkinformation.ping).send( $Computer ).status -eq "Success" )
  25. {
  26. if ( $Computer -ne "" -AND $AVID -ne "" ){
  27.  
  28. # // Variable to store your current repeat run behaviour (mostly RerunIfFail)
  29. $CurrentRepeatRunBehaviour = ""
  30.  
  31. # // Query the CCM_Softwaredistribution class to set the ADV_RepeatRunBehavior. This variable defines, when your advertisement has to rerun
  32. # // Mostly, this key has the value "RerunIfFail"
  33.  
  34. $Advertisement = get-wmiobject -query "SELECT * FROM CCM_Softwaredistribution WHERE ADV_AdvertisementID LIKE '$ADVID'" -namespace "root\CCM\Policy\Machine\ActualConfig" -Computer $Computer -Authentication PacketPrivacy -Impersonation Impersonate
  35. # -> if returnvalue.length == 1
  36.  
  37. if ( $Advertisement -ne $null ){
  38.  
  39. # // RUUUDE, fix it
  40. $intCounter = 0
  41. foreach ($a in $Advertisement) { $intCounter += 1 }
  42.  
  43. if ( $intCounter -eq 1 ){
  44. # // Store your current ADV_RepeatRunBehavior value
  45. $CurrentRepeatRunBehaviour = $Advertisement.ADV_RepeatRunBehavior
  46. if ( $CurrentRepeatRunBehaviour -ne "" ){
  47.  
  48. # // Set the ADV_RepeatRunBehavior to RerunAlways. We set this back to its previous value after triggering the SCCM_Client to rerun your advertisement
  49. $Advertisement.ADV_RepeatRunBehavior = "RerunAlways"
  50. # // Commit your changes
  51. $returnValue = $Advertisement.put()
  52. if ( $returnValue -ne $null ){
  53. # // Get the ScheduledMessageID. You need this ID to trigger the scheduler to process your advertisement to rerun
  54. $ScheduledMessageID = (get-wmiobject -query "SELECT ScheduledMessageID FROM CCM_Scheduler_ScheduledMessage WHERE ScheduledMessageID LIKE '$($ADVID)-%' " -namespace "root\CCM\Policy\Machine\ActualConfig" -Computer $Computer -Authentication PacketPrivacy -Impersonation Impersonate).ScheduledMessageID
  55. if ( $ScheduledMessageID -ne "" ){
  56.  
  57. # // Get Root Namespace of your SMS_Client for triggering the rerun
  58. $SMSClient = [WmiClass]"\\$($Computer)\ROOT\ccm:SMS_Client"
  59. # // Get the ParameterList from the TriggerSchedule method
  60. $ParameterList = $SMSClient.psbase.GetMethodParameters("TriggerSchedule")
  61. # // Set sScheduleID to your previously determined ScheduledMessageID
  62. $ParameterList.sScheduleID = $ScheduledMessageID
  63. # // Invoke the TriggerSchedule WMI Method and commit your ParameterList (which only contains your ScheduledMessageID)
  64. $ret = $SMSClient.psbase.InvokeMethod("TriggerSchedule",$ParameterList,$NULL)
  65. if ( $ret.ReturnValue -eq $null ){
  66. ## ->-> CHECKEN
  67. # // After triggering the TriggerSchedule your advertisement will rerun immediately, but lets wait a sec or two
  68. sleep 3
  69.  
  70. # // Reset the ADV_RepeatRunBehaviour field of CCM_Softwaredistribution, so no undesired advertise will rerun
  71. # // Query your CCM_Softwaredistribution
  72. $Advertisement = get-wmiobject -query "SELECT * FROM CCM_Softwaredistribution WHERE ADV_AdvertisementID LIKE '$($ADVID)' " -namespace "root\CCM\Policy\Machine\ActualConfig" -Computer $Computer -Authentication PacketPrivacy -Impersonation Impersonate
  73. # // Set the ADV_RepeatRunBehavior to the previous value
  74. $Advertisement.ADV_RepeatRunBehavior = $CurrentRepeatRunBehaviour
  75. # // Commit changes
  76. $returnValue = $Advertisement.put()
  77. if ( $returnValue -ne $null ){
  78. # // YAY, we are done!
  79. # // Party!
  80. # //
  81. write-output "OK: Advertisement will rerun immediately"
  82. }else{
  83. write-output "The variable ADV_RepeatRunBehavior could not be reset"
  84. }
  85. }else{
  86. write-output "Rerun could not be triggered"
  87. }
  88. }else{
  89. write-output "Can't get $ScheduledMessageID of your advertisement"
  90. }
  91. }else{
  92. write-output "The variable ADV_RepeatRunBehavior could not be set"
  93. }
  94. }else{
  95. write-output "Can't read ADV_RepeatRunBehavior from advertisement"
  96. }
  97. }else{
  98. write-output "The advertisement query returned $($Advertisement.length()) values. Check your AdvertisementID"
  99. }
  100. }else{
  101. write-output "Could not get your Advertisement"
  102. }
  103. }else{
  104. write-output "Computer or AdvertisementID is empty"
  105. }
  106. }else{
  107. write-output "Computer is not online"
  108. }
  109. } catch [GetWMIManagementException]{
  110.  
  111. }
  112.  
  113. #----------------------------------------------------------------------------------------------------
  114. # Begin connection test and process monitoring
  115. #----------------------------------------------------------------------------------------------------
  116.  
  117. Function Send-Email2 {
  118. # param (
  119. [Parameter(Mandatory=$true)]
  120. [String]$Body,
  121. [String]$ComputerName
  122. # )
  123. Send-MailMessage -From “admin@domain.com" -To “tseanlaws@domain.com" -SMTPServer mail.domain.com -Subject “$($computername) is OFFLINE " # -Body $Body
  124. }
  125.  
  126. function Test-Running {
  127. param (
  128. [Parameter(Mandatory=$true)]
  129. [System.String]$ComputerName
  130. )
  131. $processList = @'
  132. "Name", "Expected", "Running"
  133. "cmd", "1", "0"
  134. "powershell", "1", "0"
  135. "OfficeClickToRun", "2", "0"
  136. '@ | ConvertFrom-Csv | ForEach-Object {$_.Expected = [int]$_.Expected; $_}
  137. $splat = @{}
  138. $splat['ComputerName'] = $computerName
  139. Write-Host "Testing processes on $($computerName)" -ForegroundColor Yellow
  140. Do {
  141. $processList | ForEach-Object {
  142. $_.Running = @(Get-Process $_.Name @splat -ErrorAction SilentlyContinue).Count
  143. }
  144. ($processList | Format-Table -AutoSize | Out-String).Split("`r`n", [StringSplitOptions]::RemoveEmptyEntries) | Write-Host
  145. If ($running = @($processList | Where-Object {$_.Running -ge $_.Expected}).Count) {
  146. Start-Sleep -Seconds 5
  147. }
  148. } Until (-not $running)
  149.  
  150. Restart-Computer -Computer $Computername -Force
  151.  
  152. return 0
  153. }
  154.  
  155. function Test-MyConnection {
  156. param (
  157. [Parameter(Mandatory=$true)]
  158. [System.String]
  159. $ComputerName
  160. )
  161. if (Test-Connection $computername -Quiet -Count 2) {
  162. Write-Host "Ping successful on $($computerName)" -ForegroundColor Green
  163. #----------------------------------------------------------------------------------------------------
  164. # Run SCCM Advertisement here
  165. #----------------------------------------------------------------------------------------------------
  166. Test-Running -ComputerName $ComputerName
  167. }
  168. else {
  169. send-email $computername -ComputerName $computername -body $processlistOP
  170. Write-Host $($computerName) "is Offline." -ForegroundColor Red
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement