Tuesday, April 7, 2015

Manual force of WSUS via automation

Have you ever wanted to have an odd Schedule or found that your site will not longer sync on the schedule but a full sync will work. 

Well here is some code to kick off a Full sync on your site.  All you have to do is create a scheduled task on the Central or primary site.
The code modifies the control file.  Because of some timing issues I did add a few seconds on to the request. 


Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
swbemLocator.Security_.AuthenticationLevel = 6 'Packet Privacy
Set swbemServices= swbemLocator.ConnectServer("SERVER", "root\sms\site_XXX")

Set context = CreateObject("WbemScripting.SWbemNamedValueSet")
'context.Add "LocaleID", "MS\1033"
'context.Add "MachineName", "SERVER"
Context.Add "SessionHandle", swbemServices.ExecMethod("SMS_SiteControlFile", "GetSessionHandle").SessionHandle

SynchronizeSoftwareUpdatePoint swbemServices,context,"XXX"

Sub SynchronizeSoftwareUpdatePoint(swbemServices,swbemContext,siteCode)
    ' Load site control file and get the SMS_WSUS_SYNC_MANAGER component section.
    swbemServices.ExecMethod "SMS_SiteControlFile.Filetype=1,Sitecode=""" & siteCode & """", "Refresh", , , swbemContext
    ' Calculate the current timestamp (number of seconds from 1/1/1970 to current time UTC).
    calculatedUTCOffsetinSeconds = (8 * 60 * 60)
    currentTimestamp = datediff("s", "1/1/1970 12:00:00 AM", now()) + calculatedUTCOffsetinSeconds
    Query = "SELECT * FROM SMS_SCI_Component " & _
            "WHERE ComponentName = 'SMS_WSUS_SYNC_MANAGER' " & _
            "AND SiteCode = '" & siteCode & "'"
    Set SCIComponentSet = swbemServices.ExecQuery(Query, ,wbemFlagForwardOnly Or wbemFlagReturnImmediately, swbemContext)
    ' Only one instance is returned from the query.
    For Each SCIComponent In SCIComponentSet
        ' Loop through the array of embedded SMS_EmbeddedProperty instances.
        For Each vProperty In SCIComponent.Props        
            ' Setting: Sync Now
            If vProperty.PropertyName = "Sync Now" Then
                ' Modify the value.
                vProperty.Value = currentTimestamp
               ' wscript.echo "New value " & currentTimestamp
                ' Output success message.
               ' wscript.echo " "
               ' wscript.echo "Reset 'Sync Now' property with current timestamp. "              
            End If
             ' Update the component in your copy of the site control file. Get the path
             ' to the updated object, which could be used later to retrieve the instance.
             Set SCICompPath = SCIComponent.Put_(wbemChangeFlagUpdateOnly, swbemContext)
    ' Commit the change to the actual site control file.
    Set InParams = swbemServices.Get("SMS_SiteControlFile").Methods_("CommitSCF").InParameters.SpawnInstance_
    InParams.SiteCode = siteCode
    swbemServices.ExecMethod "SMS_SiteControlFile", "CommitSCF", InParams, , swbemContext
    ' Release copy of the site control file.
    swbemServices.Get("SMS_SiteControlFile").ReleaseSessionHandle swbemContext.Item("SessionHandle").Value
End Sub


I know this works on SCCM 2007, I am sure it will work on SCCM 2012 but I never tried it: