CscSync.vbs – Sync a particular UNC path


‘ 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: CscSync.vbs /ItemPath:<path> [/Direction:in|out|inout] [/PinNewFiles:1:0] [/Conflicts:local|remote|latest] [/Machine:value] [/User:value] [/Password:value]


‘ Demonstrates how to sync an item or tree in the Offline Files cache.

const cComputerName = “LocalHost”
const cWMINamespace = “root\cimv2″


‘ Process commandline arguments

strItemPath = WScript.Arguments.Named(“ItemPath”)
if Len(strItemPath) = 0 Then
Wscript.Echo “ItemPath parameter required”
Err.Raise 449 ‘ “argument not optional” error
End if

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

‘ Flags already configured for 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 up the API arguments

Dim ItemPaths(0)
ItemPaths(0) = strItemPath

objParams.Paths = ItemPaths
objParams.Flags = SyncControlFlags


‘ Execute the sync action

objWMIServices.ExecMethodAsync objSink, “Win32_OfflineFilesCache”, “Synchronize”, objParams, wbemFlagSendStatus


‘ 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