Friday, December 6, 2013

Powershell SCCM Device Collections by Physical Location (IP Subnet)

This Script generates device collections by Physical location based on IP Subnet.

 ForEach ($x in Import-csv "C:\subnetcol.csv"){  
 $z=$x.Coll  
 $z  
 $rule="IP Subnet Like"+$x.subnet  
 $query= "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.IPSubnets like '"+$x.subnet+"'"  
 $Schedule = New-CMSchedule –RecurInterval Days –RecurCount 1  
 New-CMDeviceCollection –Name $z –LimitingCollectionName "All Systems" –RefreshSchedule $Schedule -RefreshType "periodic"  
 Add-CMDeviceCollectionQueryMembershipRule -RuleName $rule -Collectionname $z -QueryExpression $query  
 }  

the input CSV should be formatted with one line per subnet and its associated location. the code is sloppy and when run, will error if a collection already exists, but will add the additional subnets to it.

 Subnet,coll  
 10.2.230.0,New_york-2575_Hwy_206                                                              ,  
 10.2.202.0,Chicago-626_College_Rd                                                              ,  
 10.1.137.0,Ottawa-291_Peeter_Douglas_Rd  

1 comment: