Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # // Website: http://www.marco-difeo.de / https://marco-difeo.de/2011/10/13/powershell-sccm-readvertise-a-previously-installed-softwarepackage-remotly/
- # // Scriptname: rerun_sccm_advertisement_on_client.ps1
- # //
- # // Usage: This script can be modified to fit your needs
- # // Pingback: http://marco-difeo.de/2011/10/13/powershell-sccm-readvertise-a-previously-installed-softwarepackage-remotly/
- # // #######################################################################################
- # // Script begin
- # // #######################################################################################
- # //
- # //
- # // Should check if the computer is already online or not.
- # // Your SCCM Client!!!! advertisementID (you can get this from the SCCM reports)
- $ADVID = "XXX00001"
- # // Computername on which the magic takes place ( . is the local computer)
- $Computer = "computer01"
- trap [Exception] {
- write-host $("ERROR: " + $_.Exception.Message) -ForegroundColor red
- continue;
- }
- try
- {
- # // Check, if computer is online!
- if ($computer -eq "." -or $(new-object system.net.networkinformation.ping).send( $Computer ).status -eq "Success" )
- {
- if ( $Computer -ne "" -AND $AVID -ne "" ){
- # // Variable to store your current repeat run behaviour (mostly RerunIfFail)
- $CurrentRepeatRunBehaviour = ""
- # // Query the CCM_Softwaredistribution class to set the ADV_RepeatRunBehavior. This variable defines, when your advertisement has to rerun
- # // Mostly, this key has the value "RerunIfFail"
- $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
- # -> if returnvalue.length == 1
- if ( $Advertisement -ne $null ){
- # // RUUUDE, fix it
- $intCounter = 0
- foreach ($a in $Advertisement) { $intCounter += 1 }
- if ( $intCounter -eq 1 ){
- # // Store your current ADV_RepeatRunBehavior value
- $CurrentRepeatRunBehaviour = $Advertisement.ADV_RepeatRunBehavior
- if ( $CurrentRepeatRunBehaviour -ne "" ){
- # // Set the ADV_RepeatRunBehavior to RerunAlways. We set this back to its previous value after triggering the SCCM_Client to rerun your advertisement
- $Advertisement.ADV_RepeatRunBehavior = "RerunAlways"
- # // Commit your changes
- $returnValue = $Advertisement.put()
- if ( $returnValue -ne $null ){
- # // Get the ScheduledMessageID. You need this ID to trigger the scheduler to process your advertisement to rerun
- $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
- if ( $ScheduledMessageID -ne "" ){
- # // Get Root Namespace of your SMS_Client for triggering the rerun
- $SMSClient = [WmiClass]"\\$($Computer)\ROOT\ccm:SMS_Client"
- # // Get the ParameterList from the TriggerSchedule method
- $ParameterList = $SMSClient.psbase.GetMethodParameters("TriggerSchedule")
- # // Set sScheduleID to your previously determined ScheduledMessageID
- $ParameterList.sScheduleID = $ScheduledMessageID
- # // Invoke the TriggerSchedule WMI Method and commit your ParameterList (which only contains your ScheduledMessageID)
- $ret = $SMSClient.psbase.InvokeMethod("TriggerSchedule",$ParameterList,$NULL)
- if ( $ret.ReturnValue -eq $null ){
- ## ->-> CHECKEN
- # // After triggering the TriggerSchedule your advertisement will rerun immediately, but lets wait a sec or two
- sleep 3
- # // Reset the ADV_RepeatRunBehaviour field of CCM_Softwaredistribution, so no undesired advertise will rerun
- # // Query your CCM_Softwaredistribution
- $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
- # // Set the ADV_RepeatRunBehavior to the previous value
- $Advertisement.ADV_RepeatRunBehavior = $CurrentRepeatRunBehaviour
- # // Commit changes
- $returnValue = $Advertisement.put()
- if ( $returnValue -ne $null ){
- # // YAY, we are done!
- # // Party!
- # //
- write-output "OK: Advertisement will rerun immediately"
- }else{
- write-output "The variable ADV_RepeatRunBehavior could not be reset"
- }
- }else{
- write-output "Rerun could not be triggered"
- }
- }else{
- write-output "Can't get $ScheduledMessageID of your advertisement"
- }
- }else{
- write-output "The variable ADV_RepeatRunBehavior could not be set"
- }
- }else{
- write-output "Can't read ADV_RepeatRunBehavior from advertisement"
- }
- }else{
- write-output "The advertisement query returned $($Advertisement.length()) values. Check your AdvertisementID"
- }
- }else{
- write-output "Could not get your Advertisement"
- }
- }else{
- write-output "Computer or AdvertisementID is empty"
- }
- }else{
- write-output "Computer is not online"
- }
- } catch [GetWMIManagementException]{
- }
- #----------------------------------------------------------------------------------------------------
- # Begin connection test and process monitoring
- #----------------------------------------------------------------------------------------------------
- Function Send-Email2 {
- # param (
- [Parameter(Mandatory=$true)]
- [String]$Body,
- [String]$ComputerName
- # )
- Send-MailMessage -From “admin@domain.com" -To “tseanlaws@domain.com" -SMTPServer mail.domain.com -Subject “$($computername) is OFFLINE " # -Body $Body
- }
- function Test-Running {
- param (
- [Parameter(Mandatory=$true)]
- [System.String]$ComputerName
- )
- $processList = @'
- "Name", "Expected", "Running"
- "cmd", "1", "0"
- "powershell", "1", "0"
- "OfficeClickToRun", "2", "0"
- '@ | ConvertFrom-Csv | ForEach-Object {$_.Expected = [int]$_.Expected; $_}
- $splat = @{}
- $splat['ComputerName'] = $computerName
- Write-Host "Testing processes on $($computerName)" -ForegroundColor Yellow
- Do {
- $processList | ForEach-Object {
- $_.Running = @(Get-Process $_.Name @splat -ErrorAction SilentlyContinue).Count
- }
- ($processList | Format-Table -AutoSize | Out-String).Split("`r`n", [StringSplitOptions]::RemoveEmptyEntries) | Write-Host
- If ($running = @($processList | Where-Object {$_.Running -ge $_.Expected}).Count) {
- Start-Sleep -Seconds 5
- }
- } Until (-not $running)
- Restart-Computer -Computer $Computername -Force
- return 0
- }
- function Test-MyConnection {
- param (
- [Parameter(Mandatory=$true)]
- [System.String]
- $ComputerName
- )
- if (Test-Connection $computername -Quiet -Count 2) {
- Write-Host "Ping successful on $($computerName)" -ForegroundColor Green
- #----------------------------------------------------------------------------------------------------
- # Run SCCM Advertisement here
- #----------------------------------------------------------------------------------------------------
- Test-Running -ComputerName $ComputerName
- }
- else {
- send-email $computername -ComputerName $computername -body $processlistOP
- Write-Host $($computerName) "is Offline." -ForegroundColor Red
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement