Friday, June 10, 2016

PowerShell & SCCM 2012: Portable Script for Removing Computers from Collections

While in the field away from your SCCM Console, you can still remove collections from a computer via this script. Run as a user with SCCM rights or input your own $admcred and add -credential $admcred to each SCCM WMI call.

Here's an example of removing collection 4:
$ComputerName = read-host "Enter Computer Name"
$SCCMserver = "SCCMServer01"
$namespace = "root\sms\site_xx1"
$i = 0

#SCCM
#Getting list of current software collections and then filtering out unneeded selection items
if (test-connection $SCCMServer){
$xlist = import-csv \\server\share\offlist.csv #list of collection exclusions
$xlist|add-member -notepropertyname line -notepropertyvalue ""
$CollectionList = get-wmiobject -query "Select * from SMS_Collection" -namespace $namespace -computername $SCCMserver
$CollectionList = $CollectionList|? {$xlist.name -notcontains $_.name}
foreach ($C in $CollectionList){$i++;$C|add-member -notepropertyname line -notepropertyvalue $i}
}

$CurrentApps = Get-WmiObject -Namespace $namespace -Class SMS_fullcollectionmembership -ComputerName $SCCMServer -filter "Name = '$computername'"
$InstalledSoftwareList = $CollectionList|? {$CurrentApps.collectionID -contains $_.collectionID}

do{
write-host "SCCM Software already installed on $computername" -f white
$InstalledSoftwareList|format-table line,name -hidetableheaders
if ($removesoftware){
write-host "Software to be removed" -f yellow
$removesoftware|format-table line,name -hidetableheaders

}
write-host "Enter " -nonewline;write-host "line number" -f white -nonewline;write-host " to remove SCCM software from $computername"
write-host "Enter " -nonewline;write-host "DONE" -f white -nonewline;write-host " if satisfied with removal list"
$removepackages = read-host "(Line number or Done)"
if ($removepackages -ne "done"){
[array]$removesoftware += $collectionlist|? line -eq $removepackages
}
} while ($removepackages -ne "done")

Creates the menu:
Enter Computer Name: PCReception01
SCCM Software already installed on PCReception01

  1 Adobe Acrobat Standard DC
  2 7 Zip
  3 Google Earth Pro
  4 Microsoft Office 2013
  5 Windows Mobile Device Center

Enter line number to remove SCCM software from PCReception01 
Enter DONE if satisfied with removal list
(Line number or DONE): 4

SCCM Software already installed on PCReception01

  1 Adobe Acrobat Standard DC
  2 7 Zip
  3 Google Earth Pro
  5 Windows Mobile Device Center

Software to be removed

4 Microsoft Office 2013

Enter line number to remove SCCM software from PCReception01 
Enter DONE if satisfied with removal list
(Line number or DONE): 

And now we remove each selected collection via the $removesoftware variable:
foreach ($r in $removesoftware){
$ID = $r.collectionID
$Collection = get-wmiobject -class SMS_Collection -namespace $namespace -computername $SCCMserver|? CollectionID -eq $ID
$ruleclass = get-wmiobject -namespace $namespace -class "SMS_CollectionRuleDirect" -computername $SCCMserver -list
$RemoveComputer = get-wmiobject -ComputerName $SCCMServer -Namespace $Namespace -Class "SMS_R_System" -Filter "Name = '$($computername)'"
$DelRule = $RuleClass.CreateInstance()     
$DelRule.RuleName = $($RemoveComputer.name)
$DelRule.ResourceClassName = "SMS_R_System" 
$DelRule.ResourceID = $($RemoveComputer.resourceid)
$Collection.DeleteMemberShipRule($DelRule)
$Collection.requestrefresh()
}

Here's what you should see:
___GENUS               : 2
___CLASS               : ___PARAMETERS
___SUPERCLASS          :
___DYNASTY             : ___PARAMETERS
___RELPATH             :
___PROPERTY_COUNT      : 1
___DERIVATION          : {}
___SERVER              :
___NAMESPACE           :
___PATH                :
ReturnValue            : 0
PSComputerName         :

___GENUS               : 2
___CLASS               : ___PARAMETERS
___SUPERCLASS          :
___DYNASTY             : ___PARAMETERS
___RELPATH             :
___PROPERTY_COUNT      : 1
___DERIVATION          : {}
___SERVER              :
___NAMESPACE           :
___PATH                :
ReturnValue            : 0
PSComputerName         :

You want to see ReturnValue's of 0. Anything else and the removal wasn't successful.

No comments: