CscSyncAll.vbs – Sync the entire Offline Files cache

CscSyncAll.vbs – Sync the entire Offline Files cache

  • Comments 1
  • Likes
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
' ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
' THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'
' Copyright (c) Microsoft Corporation. All rights reserved.
'
'
' Usage: CscSyncAll.vbs [/Direction:in|out|inout] [/PinNewFiles:1:0] [/Conflicts:local|remote|latest] [/Machine:value] [/User:value] [/Password:value]
'
' 
' Demonstrates how to sync the entire Offline Files cache.
'
'

const cComputerName = "LocalHost"
const cWMINamespace = "root\cimv2"


'
' Process commandline arguments
'
strComputerName = WScript.Arguments.Named("Machine")
If Len(strComputerName) = 0 Then strComputerName = cComputerName

strUserID = WScript.Arguments.Named("User")
If Len(strUserID) = 0 Then strUserID = ""

strPassword = WScript.Arguments.Named("Password")
If Len(strPassword) = 0 Then strPassword = ""

'
' Sync control flags from Win32_OfflineFilesCache.Synchronize
'
const fFillSparse        = &H00000001
const fSyncIn            = &H00000002
const fSyncOut           = &H00000004
const fPinNewFiles       = &H00000008
const fPinLinkTargets    = &H00000010
const fPinForUser        = &H00000020
const fPinForUser_Policy = &H00000040
const fPinForAll         = &H00000080 
const fLowPriority       = &H00000200 
const fAsyncProgress     = &H00000400 
const fInteractive       = &H00000800 
const fConsole           = &H00001000 
const fSkipSuspendedDirs = &H00002000 
const fBackground        = &H00010000
const fCrKeepLocal       = &H10000000 
const fCrKeepRemote      = &H20000000 
const fCrKeepLatest      = &H30000000 

const wbemFlagSendStatus = &H00000080

SyncControlFlags = fSyncIn + _
                   fSyncOut + _
                   fPinNewFiles + _
                   fPinForUser + _
                   fConsole + _
                   fInteractive

strDirection = WScript.Arguments.Named("Direction")
If Len(strDirection) <> 0 Then
    if LCase(strDirection) = "in" Then 
        SyncControlFlags = SyncControlFlags - fSyncOut
    Elseif LCase(strDirection) = "out" Then 
        SyncControlFlags = SyncControlFlags - fSyncIn
    Elseif LCase(strDirection) = "inout" Then
        '
        ' Do nothing.  Flags already indicate in/out
        '
    Else
        Wscript.Echo "Invalid direction value [" & strDirection & "]"
        Err.Raise 507 ' "an exception occured" error
    End if
End if

strPinNewFiles = WScript.Arguments.Named("PinNewFiles")
If Len(strPinNewFiles) <> 0 And strPinNewFiles = "0" Then
    SyncControlFlags = SyncControlFlags - fPinNewFiles
End if

strConflicts = WScript.Arguments.Named("Conflicts")
If Len(strConflicts) <> 0 Then
    If LCase(strConflicts) = "local" Then
        SyncControlflags = SyncControlFlags + fCrKeepLocal
    Elseif LCase(strConflicts) = "remote" Then
        SyncControlFlags = SyncControlFlags + fCrKeepRemote
    Elseif LCase(strConflicts) = "latest" Then
        SyncControlFlags = SyncControlFlags + fCrKeepLatest
    End if
End if

Set objWMILocator  = WScript.CreateObject("WbemScripting.SWbemLocator")
Set objWMIServices = objWMILocator.ConnectServer(strComputerName, _
                                                 cWMINameSpace, _
                                                 strUserID, _
                                                 strPassword)
Set objCache         = objWMIServices.Get("Win32_OfflineFilesCache=@")
Set objParams        = objCache.Methods_("Synchronize").InParameters.SpawnInstance_
Set objSink          = WScript.CreateObject("WbemScripting.SWbemSink", "SINK_")
Set objItemInstances = objWMIServices.InstancesOf("Win32_OfflineFilesItem")

'
' Set up the API arguments
'
Dim ItemPaths(0)

objParams.Paths = ItemPaths
objParams.Flags = SyncControlFlags

'
' Constants for cache item types
'
const cFile      = 0
const cDirectory = 1
const cShare     = 2
const cServer    = 3

'
' Execute the sync action on each share in the Offline Files cache.
'
For Each objItem in objItemInstances
    If cShare = objItem.ItemType Then
        ItemPaths(0) = objItem.ItemPath
        objParams.Paths = ItemPaths
        objWMIServices.ExecMethodAsync objSink, "Win32_OfflineFilesCache", "Synchronize", objParams, wbemFlagSendStatus 
    End If
Next

'
' Loop until we receive an OnCompleted event
'
bDone = False
While Not bDone
    wscript.sleep 1000
Wend


Sub SINK_OnProgress(UpperBound, Current, Message, objContext)

    DIM PART_REASON, PART_RESULT, PART_RESULTMSG, PART_PATH
    DIM REASON_BEGIN, REASON_END, REASON_ITEMBEGIN, REASON_ITEMRESULT
    DIM Reasons(3)

    '
    ' The message is composed as follows:
    '
    '     <reason>:<result>:<result msg>:<path>
    '
    '     <reason>
    '         Integer indicates reason for progress callback.
    '            Values are:
    '                0 - "Begin" overall operation
    '                1 - "End" overall operation
    '                2 - "Begin Item" operation
    '                3 - "Item result"
    '
    '     <result> 
    '         Integer result code; HRESULT.
    '
    '     <result msg>
    '         Text describing the result.  In most cases this is translated using
    '         the Win32 function FormatMessage.  In some cases a more descriptive
    '         message is provided that is better aligned with Offline Files.
    '
    '     <path>
    '         UNC path string associated with the progress notifcation.  This is
    '         empty for the "Begin" and "End" notifications.
    '

    '
    ' Define indexes of the various parts in the message.
    '
    PART_REASON    = 0
    PART_RESULT    = 1 
    PART_RESULTMSG = 2
    PART_PATH      = 3

    '
    ' The reason codes present in the <reason> part.
    '
    REASON_BEGIN      = 0
    REASON_END        = 1
    REASON_ITEMBEGIN  = 2
    REASON_ITEMRESULT = 3

    '
    ' split the message into the 4 parts and extract those parts.
    '
    Parts = Split(Message, ":", -1, 1)

    Reason = CInt(Parts(PART_REASON))
    Path = Parts(PART_PATH)
    Result = CLng(Parts(PART_RESULT))
    ResultMsg = Parts(PART_RESULTMSG)

    Select Case Reason
        Case REASON_ITEMRESULT
            WScript.Echo Path
            if 0 <> Result then
                Wscript.Echo "  Error: " & Hex(Result) & "  " & ResultMsg
            end if

        Case REASON_END
            if 0 <> Result then
                Wscript.Echo "Error: " & Hex(Result) & "  " & ResultMsg
            end if
    End Select

End Sub

'
' Called when the operation is complete.
'
Sub SINK_OnCompleted(HResult, objLastError, objContext)
    bDone = True
End Sub


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • A blog reader recently asked us how to synchronize Offline Files from the command prompt instead of from