Sunday, May 22, 2016

PowerShell & SCCM 2012: Get Old Computer SCCM Software Packages, add more software, and Export CSV out for New Computer

Using PowerShell and WMI to access the SCCM server, this post shows how to transfer a software list from the old computer and export it for the new one. In another post I describe how to automate the new computers software package collection addition process after its registered itself with the SCCM server and installed the SCCM client software.

$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

$OldComputerSoftware = Get-WmiObject -Namespace $namespace -Class SMS_fullcollectionmembership -ComputerName $SCCMServer -filter "Name = '$SCCMoldcomputer'" -credential $admcred
$OldComputerCollectionMemberList = $CollectionList|? {$OldComputerSoftware.collectionID -contains $_.collectionID}

Above I declare my variables, query the SCCM server for all the software on the old computer, and compare it to the $CollectionList collections filtering array. Next I create $NewPCSoftwareList using the old computer software list. Add line numbers and filter unwanted collections via $XList. The $AddedSoftware variable displays the existing software from the old computer and will be updated with software selections in the read-host menu below:

$NewPCSoftwareList = $OldComputerCollectionMemberList|select Name,CollectionID
foreach ($C in $NewPCSoftwareList){$i++;$C|add-member -notepropertyname line -notepropertyvalue $i}
$NewPCSoftwareList = $NewPCSoftwareList|? {$xlist.name -notcontains $_.name}
$addedsoftware = $collectionlist|? {$NewPCSoftwareList.name -contains $_.name}


Available SCCM Software Packages
 1 Adobe Acrobat Pro
 2 Adobe Acrobat Standard
 3 Base Camp
 5 Topography Software
 6 Google Earth Pro
 7 Windows Mobile Device Center

OldComputerName SCCM Software will be installed on NewComputerName 

 4 7-Zip
 8 Microsoft Office 2013

Enter line number to add SCCM software to NewComputerName
Enter DONE if satisfied with install list
(Line number or Done): _


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")
The starting menu displays the full available software list minus those software already pre-selected to be added to the new computer. I input the number for each additional software I want added to the new computer then type DONE when finished adding software. Note that the line numbers disappear from the top list when selected on the bottom one. After DONE is typed, the $addedsoftware array gets exported to a CSV that the new computer will access in order to install that software. That part of the process will be in another post.

if ($addedsoftware){$addedsoftware|export-csv .\NewComputerName-SCCMsoftware.csv}

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}