Monday, November 30, 2015

Create AD Sites Collection via PowerShell

The PowerShell script below will create collection according to the AD Site Collection csv. 

Here's the content of the csv file:
ADSiteName,Collection
1U,1U
AU2,AU2
BKT,BKT
KB,KB
KK,KK
KTN,KTN
KUC,KUC
Lot28,Lot28
MLK,MLK
SBN,SBN
SWY,SWY
TPC,TPC
BTS,BTS
CSS,COTC
IPH,IPH TOC
JHR,JHR TOC

PNG,PNG TOC

The ADSiteName is basically the Active Directory Sites in your AD. The Collection is the collection name will be created.

Here's the PowerShell script:
Import-Module "D:\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"

#SCCMSiteCode
cd L28:

ForEach ($x in Import-csv "D:\Script\AD Site Collection\AD Site Collection.csv")
{  
 $collname=$x.Collection  
 $collname  
 $rule="AD Site"+$x.ADSiteName  
 $query= "select *  from  SMS_R_System where SMS_R_System.ADSiteName = '"+$x.ADSiteName+"'"  
 $Schedule = New-CMSchedule –RecurInterval Days –RecurCount 1  
 New-CMDeviceCollection –Name $collname –LimitingCollectionName "All Systems" –RefreshSchedule $Schedule -RefreshType "Periodic"  
 Add-CMDeviceCollectionQueryMembershipRule -RuleName $rule -Collectionname $collname -QueryExpression $query  
 }  

The script above will run through each line in the csv file and create a collection named "Collection" and query for "ADSiteName"

Regards,
Hau

No comments:

Post a Comment