Script 3:
Collection - ReRun Advertisement (part 2)
$rform1.Add_Shown({
$PackageID = Get-CMDeployment -DeploymentID $DepID | Select-Object PackageID | Format-Table -HideTableHeaders | out-string
$PackageID = $PackageID -replace '\s', ''
$PackageID = $PackageID -replace '\n', ''
$ProgramName = Get-CMDeployment -DeploymentID $DepID | Select-Object ProgramName | Format-Table -HideTableHeaders | out-string
$ProgramName = $ProgramName -replace '\n', ''
$ProgramName = $ProgramName -replace '\s', ''
#Get the Devices in the collection
Get-CMDevice -CollectionName $ColName | Where-object {$_.IsClient -eq $True} | Select-Object -Property Name | Foreach {
#If you can ping it
$ComputerName = $_.Name
$success = ""
$rError = ""
If(Test-Connection -Cn $_.Name -BufferSize 16 -Count 2 -ea 0 -quiet){
#Delete the history from the registry
If ($ProgramName -notcontains '*') {
If (is64bit($ComputerName)){
#constant for the HLKM
$HKLM = "&h80000002"
#creates an SwbemNamedValueSet object
$objNamedValueSet = New-Object -COM "WbemScripting.SWbemNamedValueSet"
#adds the actual value that will requests the target to provide 64bit-registry info
$objNamedValueSet.Add("__ProviderArchitecture", 64) | Out-Null
#back to all the other usual COM objects for WMI that you have used a zillion times in VBScript
$objLocator = New-Object -COM "Wbemscripting.SWbemLocator"
$objServices = $objLocator.ConnectServer($ComputerName,"root\default","","","","","",$objNamedValueSet)
$objStdRegProv = $objServices.Get("StdRegProv")
# Obtain an InParameters object specific to the method.
$Inparams = ($objStdRegProv.Methods_ | where {$_.name -eq "EnumKey"}).InParameters.SpawnInstance_()
# Add the input parameters
$regkey = "SOFTWARE\\Microsoft\\SMS\\Mobile Client\\Software Distribution\\Execution History\\System"
($Inparams.Properties_ | where {$_.name -eq "Hdefkey"}).Value = $HKLM
($Inparams.Properties_ | where {$_.name -eq "Ssubkeyname"}).Value = $regkey
#Execute the method
$Outparams = $objStdRegProv.ExecMethod_("EnumKey", $Inparams, "", $objNamedValueSet)
#Get the Subkeys
$SubKeys = ($Outparams.Properties_ | where {$_.name -eq "sNames"}).Value
Foreach($subkey in $subkeys){
If ($packageID -eq ($subkey)){
$Inparams2 = ($objStdRegProv.Methods_ | where {$_.name -eq "EnumKey"}).InParameters.SpawnInstance_()
($Inparams.Properties_ | where {$_.name -eq "Hdefkey"}).Value = $HKLM
($Inparams2.Properties_ | where {$_.name -eq "Ssubkeyname"}).Value = $regkey+"\\"+$subkey
$Outparams2 = $objStdRegProv.ExecMethod_("EnumKey", $Inparams2, "", $objNamedValueSet)
$SubKeys2 = ($Outparams2.Properties_ | where {$_.name -eq "sNames"}).Value
Foreach ($subkey2 in $subkeys2){
$Inparams3 = ($objStdRegProv.Methods_ | where {$_.name -eq "GetStringValue"}).InParameters.SpawnInstance_()
($Inparams.Properties_ | where {$_.name -eq "Hdefkey"}).Value = $HKLM
($Inparams3.Properties_ | where {$_.name -eq "Ssubkeyname"}).Value = "$regkey\\$subkey\\$subkey2"
($Inparams3.Properties_ | where {$_.name -eq "Svaluename"}).Value = "_ProgramID"
$Outparams3 = $objStdRegProv.ExecMethod_("GetStringValue", $Inparams3, "", $objNamedValueSet)
$registryProg = ($Outparams3.Properties_ | where {$_.name -eq "sValue"}).Value
#$registryProg = $registryProg -replace '\s', ''
$registryProg = $registryProg -replace '\s', ''
$registryProg = $registryProg -replace '\n', ''
If($ProgramName -eq $registryProg){
$Inparams4 = ($objStdRegProv.Methods_ | where {$_.name -eq "DeleteKey"}).InParameters.SpawnInstance_()
($Inparams.Properties_ | where {$_.name -eq "Hdefkey"}).Value = $HKLM
($Inparams4.Properties_ | where {$_.name -eq "Ssubkeyname"}).Value = $regkey+"\\"+$subkey+"\\"+$subkey2
$Outparams4 = $objStdRegProv.ExecMethod_("DeleteKey", $Inparams4, "", $objNamedValueSet)
$return = ($Outparams.Properties_ | where {$_.name -eq "ReturnValue"}).Value
$success = "Deleted key:$subkey2"
}
}
}
}
}else{
#$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ComputerName)
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ComputerName)
$RegKey= $Reg.OpenSubKey("SOFTWARE\\Microsoft\\SMS\\Mobile Client\\Software Distribution\\Execution History\\System")
foreach($subkeyName in $RegKey.GetSubKeyNames()){
If ($packageID.contains($subKeyName)){
$RegKey2 = $RegKey.OpenSubKey($subKeyName,$true)
foreach($subkeyName2 in $RegKey2.GetSubKeyNames()){
write-host $subkeyName2
$subkey2 = $RegKey2.OpenSubKey($subkeyName2,$true)
$registryProg = $subkey2.GetValue("_ProgramID")
$registryProg = $registryProg -replace '\s', ''
write-host $ProgramName
write-host $registryProg
If($ProgramName -contains $registryProg ){
$RegKey2.DeleteSubKeyTree($subkeyName2)
$success = "Deleted key:$subkeyName2"
}
}
}
}
}
}
#Delete the history from the WMI
$wmiObject = Get-WmiObject -class CCM_Scheduler_History -NameSpace "root\ccm\scheduler" -ComputerName $ComputerName -filter "ScheduleID like '%$DepID%-%$PackageID%'"
If($wmiObject){
Remove-WmiObject -InputObject $wmiObject
$rError = ""
$success = "Deleted from wmi history"
} else {
if($success -ne ""){
$success = "Failed to find in WMI, but found in registry. " + $success
}
}
}else{
$rError = "Failed to Ping"
}
If ($success -eq ""){
If($rError -eq ""){
$rError = "No Advertisement Found"
}
}else{
$rError = $success
#restart SMS Agent service
$service = get-service -ComputerName $ComputerName -Name ccmexec
Restart-Service -InputObject $service
}
$resultrow = @(($ComputerName),($rError))
$table.Rows.Add($resultrow)
$rform1.refresh()
}
Get-CMDevice -CollectionName $ColName | Where-object {$_.IsClient -ne $True} | Select-Object -Property Name | Foreach {
$resultrow = @(($_.Name),("No Client"))
$table.Rows.Add($resultrow)
$rform1.refresh()
}
$rForm1.Text="Complete"
$rForm1.Refresh()
})
$rform1.ShowDialog() | Out-Null
break