Jump to content


  • 0
wmmayms

vbs for creating collections

Question

Hi Guys!

 

Long time no see!

Been to busy with other projects smile.gif

 

Does anybody have a vbs script for creating collection with support of adding membershiprules?

 

I just looked through my script library and could only find one script which dident support the membership rules feature.

 

Thought id ask here before starting to modify

 

Cheers

Marcus

Share this post


Link to post
Share on other sites

6 answers to this question

Recommended Posts

  • 0

what about this one ?

'©2008 Rick Bennett {rick-bennett@uiowa.edu}
'DISCLAIMER: This script is provided "as is" with all faults. The author cannot be held liable for any indirect, special, incidental, consequential, or exemplary damages arising out of or in any way relating to the use of this script, including without limitation damages for loss of goodwill, work stoppage, lost profits, loss of data, and computer failure or malfunction. You bear the entire risk as to the quality and performance of this script.


'~$~----------------------------------------~$~
Option Explicit
Dim objWshShell, strSiteCode, strSCCMServerFQDN, strUserName, strPassword
Dim objLocator, objSMS, returnValue, strMessage, StartProcess
Dim Collection, objCollection, strCollectionCheck, newCollection, strCollectionID, newCollectionRelation, Token, objNewCollection, objNewRight
Dim strParentCollectionID, strNewCollectionName, strCollectionComment, strSecurityUserName, intSecurityPermissions, intHourlyMembershipRefreshTime

Set objWshShell = WScript.CreateObject("WScript.Shell")



' Provide the needed configuration information here.
strSiteCode = "XXX" :'Site code for the SCCM environment
strSCCMServerFQDN = "XXXXXXX.XXXXX.XXXX.XXX" :'Name of the SCCM server
strNewCollectionName = "XXXXXXXXXX" : 'Name of the collection to be created
strCollectionComment = "XXXXXXXXXXXXXXXXX" : 'Comment field shown in the collection
strParentCollectionID = "XXXXXXX" : 'ID of the collection to contain the collection to be created
strSecurityUserName = "YourDomain\XXXXXXXX" : 'Name of the domain group or user to be granted access
intSecurityPermissions = "6887" : 'Permission setting to be granted to the domain group or user
intHourlyMembershipRefreshTime = "2" :'Amount of time in hours that the collection should update its membership
strUserName = "XXXXXXXXXXXXXXXXXXXXXXX" :'Domain user account that has rights to the SCCM environment
strPassword = "XXXXXXXXXXXXXXXXXXXXXXXX" :'Accompanying password to the domain user account




strMessage = "This script will attempt to create the " & strNewCollectionName & " collection with the information provided within the script." + (Chr(13)& Chr(13)& Chr(13)) + "Do you wish to continue with this process now?"

StartProcess=objWshShell.Popup(strMessage,, "SCCM Collection Creation Script . . .", 4 + 32)

If StartProcess = 7 Then
' Clicked the NO button.
Wscript.Quit
End If

' Attempts to connect to the SCCM environment.
returnValue = ConnectToSCCM
If returnValue = "NoAccess" Then
wscript.echo "Unable to connect to the SCCM provider. Either the user credentials entered are incorrect, or else this account does not have the needed rights and permissions to access the SCCM environment."
End If

' Attempts to check if the collection already exists.
Set Collection = objSMS.ExecQuery ("select * from SMS_Collection where Name='" & strNewCollectionName & "'")
For Each objCollection in Collection
strCollectionCheck = "This collection already appears to exist with the collection ID of: " & objCollection.CollectionID
Wscript.Echo strCollectionCheck
Next
If strCollectionCheck = "" Then
' Attempts to create the new collection
Set newCollection = objSMS.Get("SMS_Collection").SpawnInstance_()
newCollection.Name = strNewCollectionName
newCollection.OwnedByThisSite = True
newCollection.Comment = strCollectionComment
newCollection.Put_
Else
Wscript.Quit
End If
' Attempts to obtain the collection ID of the newly created collection.
Set Collection = objSMS.ExecQuery ("select * from SMS_Collection where Name='" & strNewCollectionName & "'")
For each objCollection in Collection
strCollectionID=objCollection.CollectionID
Next
If strCollectionID = "" Then
Wscript.Echo "Unable to obtian a collection ID for the newly created collection."
Else
' Attempts to move the newly created collection into the desired parent collection.
Set newCollectionRelation = objSMS.Get("SMS_CollectToSubCollect").SpawnInstance_()
newCollectionRelation.parentCollectionID = strParentCollectionID
newCollectionRelation.subCollectionID = strCollectionID
newCollectionRelation.Put_

' Attempts to set the membership update schedule on the collection.
Set Token = objSMS.Get("SMS_ST_RecurInterval")
Token.HourSpan = intHourlyMembershipRefreshTime
Token.StartTime = ConvertToWMIdate(Now())
Set objNewCollection = objSMS.Get ("SMS_Collection.CollectionID='" & strCollectionID & "'")
objNewCollection.RefreshSchedule = Array(Token)
objNewCollection.RefreshType = 2
objNewCollection.Put_

' Attempts to set the needed security permissions on the newly created collection.
Set objNewRight = objSMS.Get ("SMS_UserInstancePermissions").SpawnInstance_()
objNewRight.UserName = strSecurityUserName
objNewRight.ObjectKey = 1
objNewRight.InstanceKey = strCollectionID
objNewRight.InstancePermissions = intSecurityPermissions
objNewRight.Put_
End If

Wscript.Echo "Finished creating the " & strNewCollectionName & " collection. Please use the Configuration Manager console and create any needed membership queries that may be required."
Wscript.Quit



' ~$~----------------------------------------~$~
'            FUNCTIONS & SUBROUTINES
' ~$~----------------------------------------~$~
Function ConnectToSCCM
' Attempts to use the provided information to connect to the SCCM environment.
On Error Resume Next
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSMS = objLocator.ConnectServer(strSCCMServerFQDN, "root\sms\site_" & strSiteCode, strUserName, strPassword)
If Err then
Err.Clear
ConnectToSCCM = "NoAccess"
Exit Function
End If
objSMS.Security_.ImpersonationLevel = 3
objSMS.Security_.AuthenticationLevel = 6
ConnectToSCCM = "Accessed"
End Function

' ~$~----------------------------------------~$~
Function ConvertToWMIdate(strDate)
' Attempts to convert the date into a WMI date-time.
Dim strYear, strMonth, strDay, strHour, strMinute

strYear=year(strDate)
strMonth=month(strDate)
strDay=day(strDate)
strHour=hour(strDate)
strMinute=minute(strDate)

If len(strMonth) = 1 Then
strMonth = "0" & strMonth
End If
If len(strDay) = 1 Then
strDay = "0" & strDay
End If
If len(strHour) = 1 Then
strHour = "0" & strHour
End If
If len(strMinute) = 1 Then
strMinute = "0" & strMinute
End If
ConvertToWMIdate = strYear & strMonth & strDay & strHour & strMinute & "00.000000+***"
End Function

 

tell me what are you busy with (i'm on office communicator if you want to chat)

 

cheers

niall

Share this post


Link to post
Share on other sites

  • 0

Hi Niall

 

That is the one i had in my script library. But it does not support (native) the feature of adding membership rules (query based).

 

Atm im doing it manually, but if I find a better solution i will post it.

 

-Marcus

 

Ps: My communicator has stopped working..think it might be time for a reinstall soon...my machine is getting slower each day cool.gif

Share this post


Link to post
Share on other sites

  • 0

I have a simple script that can create collections from reading an excel sheet. All you is a sheet with

 

A=Collection

B=Comment

C=GlobalGroup

 

first row is for titles.

 

Change the first four strxxx files with your environment settings.

Normally I use this script for creating advertisements, packages, etc. I've cleaned up it a little bit. It is still dirty, but it works.

I've trouble to run it on x64 win7, so I run it from XP x86.

 

 

script:

'On Error Resume Next

Option Explicit

strServerNaam= "SERVER04"

strSoftwareCollection="P0100173"

strDomain1="DOMAINNAME"

strQueryName = "AD Global Group Query"

 

'--------------------------------------------------------------------------------------------------------------------------------

Dim strExcelPath, objExcel, objSheet, intRow, objExcel2, objSheet2

Dim strA, strB, strC

Dim strQuery, strQueryName, strSoftwareCollection, strServerNaam, strPackageFolder, strAdvertisementFolder, strSetupRegel, strDomain1

Dim objUser, objFSO, initFSO, intErrorFound, objPath, strPath

Dim objOSDetails(1)

Dim objNewConsoleFolderItem, objNewConsoleadFolderItem, FolderIdentifier, adFolderIdentifier

Dim objFolder, objadFolder, objSWbemLocator, objSWbemServices, ProviderLoc, Location, Site, newPackage, path, package, varPackageID, id, strGUID, newProgram, AllDPs, DP, newDP, Collection, varCollectionID, objloc, objSMS, results, Loc, strGetLine, instCollection, clsQueryRule, instQueryRule

Dim newCollection, newAdvertisement

 

adFolderIdentifier=0

FolderIdentifier=0

 

Set objPath = CreateObject ("Scripting.FileSystemObject")

strPath = objPath.GetFolder(".")

 

Set ObjFSO = CreateObject("UserAccounts.CommonDialog")

ObjFSO.Filter = "Excel Files|*.xls|All Files|*.*"

ObjFSO.InitialDir = strPath

ObjFSO.FilterIndex = 3

InitFSO = ObjFSO.ShowOpen

If InitFSO = False Then

Wscript.Echo "Script Error: Install Excel"

Wscript.Quit

End If

 

' Spreadsheet file for import.

strExcelPath = ObjFSO.Filename

 

' Bind to Excel object.

On Error Resume Next

Set objExcel = CreateObject("Excel.Application")

If Err.Number <> 0 Then

On Error GoTo 0

Wscript.Echo "Excel not found."

Wscript.Quit

End If

On Error GoTo 0

 

' Open spreadsheet.

On Error Resume Next

objExcel.Workbooks.Open strExcelPath

If Err.Number <> 0 Then

On Error GoTo 0

Wscript.Echo "Spreadsheet could not be opened: " & strExcelPath

Wscript.Quit

End If

On Error GoTo 0

 

' Bind to worksheet.

Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

 

'eerste regel bevat kolomtitels begin bij regel 2

intRow = 2

 

REM A=Collection

REM B=Comment

REM C=GlobalGroup

 

Do While objSheet.Cells(intRow, 1).Value <> ""

strA = objSheet.Cells(intRow, 1).Value

strB = objSheet.Cells(intRow, 2).Value

strC = objSheet.Cells(intRow, 3).Value

 

'Controleer de input van de Excel file

IntErrorFound = 0

If strA = "" Then IntErrorFound = 1

 

'Connect to provider namespace for computer.

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

Set objSWbemServices= objSWbemLocator.ConnectServer(strServerNaam, "root\sms")

Set ProviderLoc = objSWbemServices.InstancesOf("SMS_ProviderLocation")

 

For Each Location In ProviderLoc

If Location.ProviderForLocalSite = True Then

Set objSWbemServices = objSWbemLocator.ConnectServer _

(Location.Machine, "root\sms\site_" + Location.SiteCode)

Set Site = objSWbemServices.Get("SMS_Site='" & Location.SiteCode & "'")

End If

Next

'--------------------------------------------------------------------------------------------------------------------------------

'Maak de collection aan.

Set newCollection = objSWbemServices.Get("SMS_Collection").SpawnInstance_()

newCollection.Name = strA

newCollection.OwnedByThisSite = True

newCollection.comment = strB

path=newCollection.Put_

 

'Get collection ID

Set Collection=objSWbemServices.Get(path)

VarCollectionID= Collection.CollectionID

 

'Koppel Collection

Dim newCollectionRelation

Set newCollectionRelation = objSWbemServices.Get( "SMS_CollectToSubCollect" ).SpawnInstance_()

newCollectionRelation.parentCollectionID = strSoftwareCollection

newCollectionRelation.subCollectionID = VarCollectionID

newCollectionRelation.Put_

 

'--------------------------------------------------------------------------------------------------------------------------------

'AD Query

strQuery = "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.SystemGroupName = ' "& strDomain1 & "\\" & strC & "'"

Set objLoc = CreateObject("WbemScripting.SWbemLocator")

Set objSMS = objLoc.ConnectServer(strServerNaam, "root\sms")

Set Results = objSMS.ExecQuery("SELECT * From SMS_ProviderLocation WHERE ProviderForLocalSite = true")

For Each Loc In Results

If Loc.ProviderForLocalSite = True Then

Set objSMS = objLoc.ConnectServer(Loc.Machine, "root\sms\site_" & Loc.SiteCode )

End If

Next

 

strGetLine = "SMS_Collection.CollectionID='" & VarCollectionID & "'"

Set instCollection = objSMS.Get("SMS_Collection.CollectionID='" & VarCollectionID & "'")

Set clsQueryRule = objSMS.Get("SMS_CollectionRuleQuery")

Set instQueryRule = clsQueryRule.SpawnInstance_

instQueryRule.QueryExpression = strQuery

instQueryRule.RuleName = strQueryName

instCollection.AddMembershipRule instQueryRule

 

intRow = intRow + 1

Loop

 

'--------------------------------------------------------------------------------------------------------------------------------

 

'Close the workbook.

objExcel.ActiveWorkbook.Close

 

'Quit Excel input

objExcel.Application.Quit

 

'Clean up.

Set objUser = Nothing

Set objExcel = Nothing

Set objSheet = Nothing

 

Wscript.Echo "Done!."

Share this post


Link to post
Share on other sites

  • 0

what about this one ?

'©2008 Rick Bennett {rick-bennett@uiowa.edu}
'DISCLAIMER: This script is provided "as is" with all faults. The author cannot be held liable for any indirect, special, incidental, consequential, or exemplary damages arising out of or in any way relating to the use of this script, including without limitation damages for loss of goodwill, work stoppage, lost profits, loss of data, and computer failure or malfunction. You bear the entire risk as to the quality and performance of this script.


'~$~----------------------------------------~$~
Option Explicit
Dim objWshShell, strSiteCode, strSCCMServerFQDN, strUserName, strPassword
Dim objLocator, objSMS, returnValue, strMessage, StartProcess
Dim Collection, objCollection, strCollectionCheck, newCollection, strCollectionID, newCollectionRelation, Token, objNewCollection, objNewRight
Dim strParentCollectionID, strNewCollectionName, strCollectionComment, strSecurityUserName, intSecurityPermissions, intHourlyMembershipRefreshTime

Set objWshShell = WScript.CreateObject("WScript.Shell")



' Provide the needed configuration information here.
strSiteCode = "XXX" :'Site code for the SCCM environment
strSCCMServerFQDN = "XXXXXXX.XXXXX.XXXX.XXX" :'Name of the SCCM server
strNewCollectionName = "XXXXXXXXXX" : 'Name of the collection to be created
strCollectionComment = "XXXXXXXXXXXXXXXXX" : 'Comment field shown in the collection
strParentCollectionID = "XXXXXXX" : 'ID of the collection to contain the collection to be created
strSecurityUserName = "YourDomain\XXXXXXXX" : 'Name of the domain group or user to be granted access
intSecurityPermissions = "6887" : 'Permission setting to be granted to the domain group or user
intHourlyMembershipRefreshTime = "2" :'Amount of time in hours that the collection should update its membership
strUserName = "XXXXXXXXXXXXXXXXXXXXXXX" :'Domain user account that has rights to the SCCM environment
strPassword = "XXXXXXXXXXXXXXXXXXXXXXXX" :'Accompanying password to the domain user account




strMessage = "This script will attempt to create the " & strNewCollectionName & " collection with the information provided within the script." + (Chr(13)& Chr(13)& Chr(13)) + "Do you wish to continue with this process now?"

StartProcess=objWshShell.Popup(strMessage,, "SCCM Collection Creation Script . . .", 4 + 32)

If StartProcess = 7 Then
' Clicked the NO button.
Wscript.Quit
End If

' Attempts to connect to the SCCM environment.
returnValue = ConnectToSCCM
If returnValue = "NoAccess" Then
wscript.echo "Unable to connect to the SCCM provider. Either the user credentials entered are incorrect, or else this account does not have the needed rights and permissions to access the SCCM environment."
End If

' Attempts to check if the collection already exists.
Set Collection = objSMS.ExecQuery ("select * from SMS_Collection where Name='" & strNewCollectionName & "'")
For Each objCollection in Collection
strCollectionCheck = "This collection already appears to exist with the collection ID of: " & objCollection.CollectionID
Wscript.Echo strCollectionCheck
Next
If strCollectionCheck = "" Then
' Attempts to create the new collection
Set newCollection = objSMS.Get("SMS_Collection").SpawnInstance_()
newCollection.Name = strNewCollectionName
newCollection.OwnedByThisSite = True
newCollection.Comment = strCollectionComment
newCollection.Put_
Else
Wscript.Quit
End If
' Attempts to obtain the collection ID of the newly created collection.
Set Collection = objSMS.ExecQuery ("select * from SMS_Collection where Name='" & strNewCollectionName & "'")
For each objCollection in Collection
strCollectionID=objCollection.CollectionID
Next
If strCollectionID = "" Then
Wscript.Echo "Unable to obtian a collection ID for the newly created collection."
Else
' Attempts to move the newly created collection into the desired parent collection.
Set newCollectionRelation = objSMS.Get("SMS_CollectToSubCollect").SpawnInstance_()
newCollectionRelation.parentCollectionID = strParentCollectionID
newCollectionRelation.subCollectionID = strCollectionID
newCollectionRelation.Put_

' Attempts to set the membership update schedule on the collection.
Set Token = objSMS.Get("SMS_ST_RecurInterval")
Token.HourSpan = intHourlyMembershipRefreshTime
Token.StartTime = ConvertToWMIdate(Now())
Set objNewCollection = objSMS.Get ("SMS_Collection.CollectionID='" & strCollectionID & "'")
objNewCollection.RefreshSchedule = Array(Token)
objNewCollection.RefreshType = 2
objNewCollection.Put_

' Attempts to set the needed security permissions on the newly created collection.
Set objNewRight = objSMS.Get ("SMS_UserInstancePermissions").SpawnInstance_()
objNewRight.UserName = strSecurityUserName
objNewRight.ObjectKey = 1
objNewRight.InstanceKey = strCollectionID
objNewRight.InstancePermissions = intSecurityPermissions
objNewRight.Put_
End If

Wscript.Echo "Finished creating the " & strNewCollectionName & " collection. Please use the Configuration Manager console and create any needed membership queries that may be required."
Wscript.Quit



' ~$~----------------------------------------~$~
'            FUNCTIONS & SUBROUTINES
' ~$~----------------------------------------~$~
Function ConnectToSCCM
' Attempts to use the provided information to connect to the SCCM environment.
On Error Resume Next
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSMS = objLocator.ConnectServer(strSCCMServerFQDN, "root\sms\site_" & strSiteCode, strUserName, strPassword)
If Err then
Err.Clear
ConnectToSCCM = "NoAccess"
Exit Function
End If
objSMS.Security_.ImpersonationLevel = 3
objSMS.Security_.AuthenticationLevel = 6
ConnectToSCCM = "Accessed"
End Function

' ~$~----------------------------------------~$~
Function ConvertToWMIdate(strDate)
' Attempts to convert the date into a WMI date-time.
Dim strYear, strMonth, strDay, strHour, strMinute

strYear=year(strDate)
strMonth=month(strDate)
strDay=day(strDate)
strHour=hour(strDate)
strMinute=minute(strDate)

If len(strMonth) = 1 Then
strMonth = "0" & strMonth
End If
If len(strDay) = 1 Then
strDay = "0" & strDay
End If
If len(strHour) = 1 Then
strHour = "0" & strHour
End If
If len(strMinute) = 1 Then
strMinute = "0" & strMinute
End If
ConvertToWMIdate = strYear & strMonth & strDay & strHour & strMinute & "00.000000+***"
End Function

 

tell me what are you busy with (i'm on office communicator if you want to chat)

 

cheers

niall

 

Hi niall (and surely all others),

 

maybe you have an idea for me. I´m looking for a script which daily should set instance security rights on all subcollections of a dedicated collection.

Background for this script: we have one primary and a few secondary sites. On the secondary sites the admins are allowed to create collections, but unfortunately not always set the instance rights...

so i want to automate this procedure (for each secondary site other AD groups).

 

So, i think this script could be a good way. But i have no clue how to query the sub-collections and then set the rights. Maybe you can help me or give me a good tipp.

 

greets

Dennis

Share this post


Link to post
Share on other sites

  • 0

ok , these scripts should fit my request...

http://myitforum.com/cs2/blogs/sthompson/archive/2009/09/11/custom-sms-configmgr-collection-permissions.aspx

 

and this one should be the right one for differ between the Locations and the related AD groups:

http://myitforum.com/cs2/blogs/sthompson/archive/2009/09/11/custom-sms-configmgr-collection-permissions-part-2.aspx

 

thanks so far.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.