Sunday, May 22, 2016

PowerShell & SCCM 2012: Retrieving and Filtering Collections

In order to expedite my computer replacement process, I decided to automate the software transfer from the old computer to the new one. This post goes over the first part of that process. I thank the many great people that have posted how to retrieve information from SCCM via WMI. I built upon their success and customized it for my needs. In this first snippet, I declare my variables, poll the SCCM server, then query the SCCM server for all its collections ($CollectionList). I also import my collection filtering CSV file ($xlist).

$SCCMserver = SCCMServer01
$namespace = "root\sms\site_xx1"
$adminname = "Domain\AdmUser"
$password = get-content .\adminpassword.txt|convertto-securestring -key (1..16)
$admcred = new-object system.management.automation.pscredential($adminname,$password)
$i = 0

if (test-connection $SCCMServer){
$xlist = import-csv \\server\share\SCCMscripts\CollectionFilter.csv
$CollectionList = get-wmiobject -query "Select * from SMS_Collection" -namespace $namespace -computername $SCCMserver -credential $admcred
$CollectionList = $CollectionList|? {$xlist.name -notcontains $_.name}
foreach ($C in $CollectionList){$i++;$C|add-member -notepropertyname line -notepropertyvalue $i}
}

With the above, I've filtered through the complete collection list using my collection filtering CSV, added a new array property named line, and have run the resulting list through a line numbering loop so I can present the collection list for further filtering as in the example below.

Available SCCM Software Packages
 1 Adobe Acrobat Pro
 2 Adobe Acrobat Standard
 3 7 Zip
 4 Google Earth
 5 AutoCAD LT

Enter software package line number to remove from availability list
Enter NONE if no changes are needed
(Line number or NONE:): 5
Available SCCM Software Packages - Updated
 1 Adobe Acrobat Pro
 2 Adobe Acrobat Standard
 3 7 Zip
 4 Google Earth

Enter software package line number to remove from availability list
Enter NONE if no changes are needed
(Line number or NONE:): NONE

In the above example, my filtered $CollectionList displayed five software packages. I typed in number 5 to remove AutoCAD LT from my software list, press Enter, and the list now only has the four remaining packages. Once finished updating the software list, I will use the filtered list to apply against the old computer's software list. This helps me select software for its replacement without wading through all the other arbitrary collections and packages. I'll show that software selection process in another post.

do{
if (!($remove)){write-host "Available SCCM Software Packages" -f white}
if ($remove){write-host "Available SCCM Software Packages" -f white -nonewline;write-host " - Updated" -f green}
$CollectionList|format-table line,name -hidetableheaders
write-host "Enter software package " -nonewline;write-host "line number" -f white -nonewline;write-host " to remove from availability list"
write-host "Enter " -nonewline;write-host "NONE" -f white -nonewline;write-host " if no changes are needed"
$addtoxlist = read-host "(Line number or NONE)"
if ($addtoxlist -ne "none"){
[array]$remove += $CollectionList|? line -eq $addtoxlist|select name,collectionID
$CollectionList = $CollectionList|? {$remove.name -notcontains $_.name}
}
} while ($addtoxlist -ne "none")

After "None" is typed, the $remove list is applied to $xlist then exported back out to the filter list.

if ($remove){
$xlist = $xlist|select Name,CollectionID #removing the line number object
$xlist += $remove|select Name,CollectionID #adding the selected collection items to the filter list
$xlist = $xlist|sort CollectionID -unique #removing duplicate entries
$xlist|export-csv \\server\share\SCCMscripts\CollectionFilter.csv}

No comments: