Showing posts with label SMS. Show all posts
Showing posts with label SMS. Show all posts

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.

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.

Saturday, February 22, 2014

PowerShell: Downloading SCCM 2012 ReportServer Report CSV's

The SCCM 2012 Reports interface is cumbersome for quickly pulling information together.  Instead, I updated my SCCM 2007 SQL server web-reporting technique.  I found it worked great and now can download reports as CSV's and use them in PowerShell scripts.

Here's an example:

invoke-webrequest "http://sccmserver/ReportServer?%2fConfigMgr_NR1%2fAsset+Intelligence%2fSoftware+02E+-+Installed+software+on+a+specific+computer&Name=$ComputerName&rs:Command=Render&rs:Format=CSV&rc:Toolbar=False" -credential $creds -outfile $destination 

This will download a report about all of the software loaded on one computer and place it into a CSV file.  All you'd have to do is $software = import-csv $destination 

Another example reports about all the software that starts when the computer is powered on:
iwr "http://sccmserver/ReportServer?%2fConfigMgr_NR1%2fAsset+Intelligence%2fSoftware+04C+-+Software+configured+to+automatically+run+on+a+specific+computer&Name=$ComputerName&rs:Command=Render&rs:Format=CSV&rc:Toolbar=False" -credential $creds -outfile $autodestination

A third example reports about what computers and IPs are currently on a subnet:

iwr -credential $creds "http://sccmserver/ReportServer?%2fConfigMgr_NR1%2fNetwork%2fIP+-+Computers+in+a+specific+subnet&variable=192.168.0.0&rs:Command=Render&rc:toolbar=false&rs:Format=CSV" -outfile $oursubnet

Here I get the network card information for a computer:
iwr -credential $creds "http://sccmserver/ReportServer?%2fConfigMgr_NR1%2fHardware+-+Network+Adapter%2fNetwork+adapter+information+for+a+specific+computer&variable=$ComputerName&rs:Command=Render&rc:toolbar=false&rs:Format=CSV&quo t; -outfile $NICDestination

A report about the users of a certain computer:
iwr -credential $creds "http://sccmserver/ReportServer?%2fConfigMgr_NR1%2fAsset+Intelligence%2fHardware+05A+-+Console+users+on+a+specific+computer&Name=$ComputerName&rs:Comm and=Render&rc:toolbar=false&rs:Format=CSV" -outfile $cache

A computer hardware report about a certain computer:
iwr -credential $creds "http://sccmserver/ReportServer?%2fConfigMgr_NR1%2fHardware +-+General%2fComputer+information+for+a+specific+computer&variable=$ComputerName&rs:Command=Render&rc:toolbar=false&rs:Format=CSV" -outfile $HWDestination

Disk information report about a certain computer:

iwr -credential $creds "http://sccmserver/ReportServer?%2fConfigMgr_NR1%2fHardware +-+Disk%2fDisk+information+for+a+specific+computer+-+Physical+disks&variable=$ComputerName&rs:Command=Render&rc:toolbar=false&rs:Format=CSV" -outfile $diskdestination


Thursday, July 25, 2013

PowerShell: Downloading CSV's from SCCM-SMS 2007 Reporting Websites

I needed to bundle the SCCM/SMS 2007 ConfigMgr web reporting results into PowerShell or Excel.  The SMS Web Reporting has a great export-to-CSV tool on each page but runs Java in the background and requires a confirmation click and a subsequent filename before being able to download the CSV file.  Once downloaded, it quickly becomes a PowerShell object via import-csv.  After lots of searching, I compiled a method to automatically download and save the CSV file so it can be used by PowerShell.


#Set the variables

$xhttp = $nul
$stream = $nul
$username = "domain\username"
$password = "password"
$url = 'http://sccmserver/SMSReporting_bob/Report.asp?ReportID=386&Name=MyPcName'
$destination = "c:\users\myname\desktop\MyPcName.csv"

#Start XML

$xhttp = new-object -com msxml2.xmlhttp

$xhttp.open("Post",$url,$false,$username,$password)

$xhttp.setrequestheader("Content-Type","application/x-www-form-urlencoded")

#this sends the request to perform a CSV export

$xhttp.send("export=yes")

#This line's not needed but I use it to get confirmation
#that the download is queued correctly.  If not OK then
#I know to check the above part of this script

$xhttp.statustext

#Now grab the file in the buffer and save it to disk

$stream = new-object -com ADODB.Stream

$stream.open()

$stream.type = 1

#Connect the buffer to the downloaded file

$stream.write($xhttp.responsebody)

$stream.savetofile($destination,2)

$stream.close()

$stream = $nul

References:
http://myitforum.com/cs2/blogs/jnelson/archive/2008/04/02/114527.aspx
http://www.greyhathacker.net/?p=500
http://scriptolog.blogspot.com/2007/08/query-http-status-codes-and-headers.html