Friday, June 10, 2016

PowerShell & SCCM 2012: Portable Script for Adding Computers to Collections

Being a field technician, the SCCM Console is not always nearby. The script in this post works from a thumb drive and has no need for the SCCM PowerShell module. Run this script with SCCM level credentials or you can add your username/password ($admcred) and add -credential $admcred to your SCCM WMI calls.


$SCCMserver = "SCCMServer01"
$namespace = "root\sms\site_xx1"
$computername = read-host "Enter Computer Name"
$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 filtered out collections
$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} #adding line numbers to each collection entry
}else{write-host "No SCCM Server connection, ending script" -f yellow;start-sleep 10;exit}

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

do{
cls
write-host "Available SCCM Software Packages" -f white
$collectionlist|? {$InstalledSoftwareList.name -notcontains $_.name}|format-table line,name
write-host "SCCM Software already installed on $computername" -f white
$InstalledSoftwareList|format-table line,name -hidetableheaders
if ($newsoftware){
write-host "Software to be added" -f yellow
$newsoftware|format-table line,name -hidetableheaders
}
write-host "Enter " -nonewline;write-host "line number" -f white -nonewline;write-host " to add SCCM software to $computername"
write-host "Enter " -nonewline;write-host "DONE" -f white -nonewline;write-host " if satisfied with install list"
$addpackages = read-host "(Line number or Done)"
if ($addpackages -ne "done"){
$newsoftware += $collectionlist|? line -eq $addpackages
}
} while ($addpackages -ne "done")

Here's a sample of this menu after selecting line item 7 and typing DONE at the prompt:
Available SCCM Software Packages

line name
---- ----
   1 Adobe Acrobat Pro DC
   3 Adobe Acrobat Standard DC
   4 Base Camp
   5 Google Earth Pro

SCCM Software already installed on PCReception01

   2 Adobe Acrobat Reader DC
   6 7 Zip

Software to be added

7 Windows Mobile Device Center

Enter line number to add SCCM software to PCReception01
Enter DONE if satisfied with installation list
(Line number or DONE): DONE

Now we add $computername to the selected collection via the $newsoftware variable. Let's create a new rule that adds the computer it to the collection:
foreach ($c in $newsoftware){
$ID = $c.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
$AddComputer = get-wmiobject -ComputerName $SCCMServer -Namespace $Namespace -Class "SMS_R_System" -Filter "Name = '$($computername)'"
$NewRule = $RuleClass.CreateInstance()     
$NewRule.RuleName = $($AddComputer.name)
$NewRule.ResourceClassName = "SMS_R_System" 
$NewRule.ResourceID = $($AddComputer.resourceid)
$Collection.AddMembershipRules($newrule)
$Collection.requestrefresh()
}

And here's how that looks:
___GENUS               : 2
___CLASS               : ___PARAMETERS
___SUPERCLASS          :
___DYNASTY             : ___PARAMETERS
___RELPATH             :
___PROPERTY_COUNT      : 2
___DERIVATION          : {}
___SERVER              :
___NAMESPACE           :
___PATH                :
QueryIDs               : {0}
ReturnValue            : 0
PSComputerName         :

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


You want the returnvalue to equal 0.  This means the rule successfully added your computer to the collection.

No comments: