3 Replies - 8577 Views - Last Post: 03 May 2012 - 01:22 AM Rate Topic: -----

#1 bikerguy_2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 02-May 12

Powershell: Offline Files Scripting Issue

Posted 02 May 2012 - 01:30 AM

Hi all,

I'm in the proces of writing a script which pins folders into the offline files cache (CSC) on Windows 7 x64 machines. It takes an array of strings (the paths to sync), pins them to the cache and then once everything's pinned it performs a full synchronisation of the CSC on the local box.

Only it doesn't.

I've scoured the MSDN libraries on "Win32_OfflineFiles*" and everything looks correct, when the script runs it appears to run successfully, the output to the console seems to indicate that each command has completed ok and then it logs that it's all good. When you look at Sync Center on the local machine the paths specified in the script aren't pinned anywhere and no synchronisation of the cache has taken place.

If anyone's got any clues or can offer any help I'd be much obliged, this is driving me nuts now!

Ok, the code (I'll just include the two functions that aren't working rather than everything):

This function (poorly named I'll admit) performs the pin action on a single path that is passed to it. It is called from a ForEach loop which checks to see that each path exists before calling this function.
Function EnableCSC ($syncpath) {
    $localCSC = [wmiclass]"Win32_OfflineFilesCache"
    $localCSC.Pin($syncpath[0],0x00010801,$true)
    Logging "2" "$syncpath pinned."
    }



This second function performs a full synchronisation (or should...) of the CSC on the local machine. The $paths variable passed to it contains only paths which both exist and have previously been pinned by the enableCSC function.
Function SynchCSC ($paths) {
    if (! $paths) {
        # No arguments, log and quit.        
        Logging "2" "No arguments passed to sync function. Nothing to do!"
        }
    Else {
        Logging "2" "Synchronisation Process Starting . . ."
        $localCSC = [wmiclass]"Win32_OfflineFilesCache"
        $localCSC.Synchronize($paths,0x00000002)
        Logging "2" "Synchronised."
        }
    }



The call to "Logging" is another function which performs simple logging to a text file.

If anyone's got any clue as to why these two functions don't work I'd be much obliged!

Many thanks,

Paul

PS. I've attached the full script for anyone who's interested.

Is This A Good Question/Topic? 1
  • +

Replies To: Powershell: Offline Files Scripting Issue

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 408
  • View blog
  • Posts: 882
  • Joined: 27-June 09

Re: Powershell: Offline Files Scripting Issue

Posted 02 May 2012 - 10:06 AM

Shouldn't the pin just be
$localCSC.Pin($syncpath,0x00010801,$true)

instead of $syncpath[0]

Why do you only pass the first item of syncpath?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: Powershell: Offline Files Scripting Issue

Posted 02 May 2012 - 10:11 AM

Only do this once:
$localCSC = [wmiclass]"Win32_OfflineFilesCache"



You're creating an instance of an object every single time. You probably only need one and doing it over and over can confuse things. By the same logic, you probably only need to issue the Synchronize once.
Was This Post Helpful? 0
  • +
  • -

#4 bikerguy_2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 02-May 12

Re: Powershell: Offline Files Scripting Issue

Posted 03 May 2012 - 01:22 AM

View Postmojo666, on 02 May 2012 - 10:06 AM, said:

Shouldn't the pin just be
$localCSC.Pin($syncpath,0x00010801,$true)

instead of $syncpath[0]

Why do you only pass the first item of syncpath?

Yup, I should remove the [0] really, had it there for testing when I was originally sending an array of paths to the pin routine and looping through inside that, its a lot cleaner this way though (looping through the paths outside the function, testing for path existence and then passing a single path to the funtion to pin). When tested it still works as there's only a single string in the variable so it still returns the same thing. (I've stored it in the variable within quotes so it returns the whole string, not just the first character.)

Thanks for the input though, appreciated :)
(And I will tidy that up!)

View Postbaavgai, on 02 May 2012 - 10:11 AM, said:

Only do this once:
$localCSC = [wmiclass]"Win32_OfflineFilesCache"



You're creating an instance of an object every single time. You probably only need one and doing it over and over can confuse things. By the same logic, you probably only need to issue the Synchronize once.

Its been a while since I did any proper coding but doesn't a variable defined within the scope of a function get removed when the function terminates? So the $localCSC variable is only valid within that function for the duration the function is running?

I'll certainly try defining it as a global variable the once though and see if it makes the difference :)

The synchronise does indeed only run once, it's called at the end of the loop which pins the various paths. I guess it doesn't really need to be sat as a function call but it makes it a bit easier to test.

Thanks for the input - appreciated! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1