locked
Windows 7 and Offline Files - Sync at logoff RRS feed

  • Question

  • All,

    I have set up folder redirection in my organisation (testing purposes) The problem I am facing is Windows 7 will not automatically sync offline files back to the share.

    There is a gpo for Windows XP to do this when a user is either is logging off on on. I am trying to accomplish the same when a user logs off and a script can be run to initiate a sync.

    Please help.

    This is the VBS script I am using, but does nothing when I run it. IS there something missing? A better solution?

    '
    ' 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

    *This code was pulled from: http://blogs.technet.com/b/filecab/archive/2007/04/26/cscsyncall-vbs-sync-the-entire-offline-files-cache.aspx

    Friday, January 31, 2014 8:07 PM

Answers

  • Hi,

    Please know that Offline Files in Windows 7 uses Bitmap Differential Transfer. Bitmap Differential Transfer tracks which blocks of a file in the local cache are modified while you are working offline and then sends only those blocks to the server. In Windows XP, Offline Files copies the entire file from the local cache to the server, even if only a small part of the file was modified while offline.

    How the synchronization in Windows 7 Offline Files works
    http://blogs.technet.com/b/netro/archive/2010/04/09/how-the-synchronization-in-windows-7-offline-files-works.aspx

    That should be the cause of the issue that your script didn't work.

    Considering that the issue should be related to the script, I suggest to post the question on Script Center forum for further help.

    Script Center forum:
    http://social.technet.microsoft.com/Forums/scriptcenter/en-US/home
    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. 

    Thanks for understanding.


    Kate Li
    TechNet Community Support



    Monday, February 3, 2014 1:20 AM