Ask a questionAsk a question
 

QuestionModify collection update schedule via vbscript

  • Tuesday, October 20, 2009 5:00 AMSuper Bean Flicker Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi, 

    I'm trying to write a vbscript to change all collections on our SCCM server to update hourly.

    This is what I've got so far... but it fails. Anyone have this working?


    '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 = "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 = "1" :'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
    
    ' 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")
    For Each objCollection in Collection
    	objCollection.hourspan = 1 'hourly update
    	objCollection.Put_
    	    If Err.Number<>0 Then
            WScript.Echo "Couldn't commit the package"
    	    End If
    Next
    
    
    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
    


All Replies

  • Tuesday, October 20, 2009 12:00 PMGarth JonesMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Can I recommend against doing that! Particularly on many collections. In these forums (or on myITforum.com) we have see time and time again where Admins set up their collection to update too frequently and cause themselves no ends of headaches.


    http://www.enhansoft.com/
  • Tuesday, November 10, 2009 3:54 PMKim Oppalfens [MVP]MVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Without commenting on whether this is a good idea or not, your script can't work.
    the sms_collection class doesn't have an hourspan property.
    The schedule is stored in the refreshschedule attribute which is stored ad a scheduletoken.
    "Everyone is an expert at something" Kim Oppalfens Configmgr expert for lack of any other expertise. http://www.scug.be/blogs/sccm