Hey, Scripting Guy! How Can I Work with the Offline Files Feature in Windows?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have some critical files that I need to work with on a regular basis. In the office, the files are stored on a server. I need to be able to take the files home and work on them while working on my laptop. When I get back to work, I need the files to synchronize with the server. I know I need to use the offline files feature of my laptop, but I am not sure where the offline cache is located or whether or not it is enabled. Can I determine this information with a Windows PowerShell script?

– WB

SpacerHey, Scripting Guy! Answer

Hi WB,

Scripting Guy Ed Wilson is feeling a little sluggish right now. He is foundered on Dungeness crab while he is visiting Seattle. He has been teaching a Windows PowerShell class to a group of Microsoft engineers, and he has been sampling the local cuisine. They offered to take him to a barbeque restaurant; however, being from an area where they claim to have invented barbeque, he politely declined and opted instead for a local seafood establishment. He is glad he made that choice—sort of. Anyway, if we can get his attention (he is sitting in the corner groaning right now), we will ask him about your situation.

Okay, just talked to Dungen-Ed. He suggested using a WMI class, win32_OfflineFilesCache, to get the information you require. He also wrote the GetOffLineFiles.ps1 script for you. (Dude, he is groaning again).

GetOffLineFiles.ps1

param($computer=”localhost”, $help)

function funline ($strIN) { $num = $strIN.length for($i=1 ; $i -le $num ; $i++) { $funline += “=” } Write-Host -ForegroundColor yellow $strIN Write-Host -ForegroundColor darkYellow $funline }

function funHelp() { $helpText=@” DESCRIPTION: NAME: GetOffLineFiles.ps1 Prints the offline files config on a local or remote machine.

PARAMETERS: -computer Specifies name of the computer upon which to run the script -help prints help file

SYNTAX: GetOffLineFiles.ps1 -computer MunichServer

Lists offline files config on a computer named MunichServer

GetOffLineFiles.ps1

Lists offline files config on local computer

GetOffLineFiles.ps1 -help ?

Displays the help topic for the script

“@ $helpText exit }

if($help){ funline(“Obtaining help …”) ; funhelp }

$outtxt = Get-WmiObject -Class win32_OfflineFilesCache ` -computername $computer funline(“Offline files configuration $env:computername”)

format-table -Property active, enabled,location -autosize ` -inputobject $outtxt

WB, offline file functionality provides automatic synchronization between files stored on a server and those stored on a laptop or other portable computing device. This provides a high level of robustness for the user and solves the backup problem of critical files. Additionally, it gives the user the chance to check in and to work with files. There is only one instance of the offline files cache. This offline file cache is seen in the following image, where we directly query the Win32_OfflineFilesCache WMI class.

Image of the offline file cache

 

However, there are a number of WMI classes supported by the WMI provider called, imaginatively enough, the OfflineFilesWmiProvider. On a computer running Windows 7, the following are the WMI classes related to offline files.

  • Win32_OfflineFilesCache
  • Win32_OfflineFilesAssociatedItems
  • Win32_OfflineFilesChangeInfo
  • Win32_OfflineFilesItem
  • Win32_OfflineFilesPinInfo
  • Win32_OfflineFilesFileSysInfo
  • Win32_OfflineFilesSuspendInfo
  • Win32_OfflineFilesConnectionInfo
  • Win32_OfflineFilesDirtyInfo
  • Win32_PerfFormattedData_OfflineFiles_ClientSideCaching
  • Win32_PerfRawData_OfflineFiles_ClientSideCaching
  • Win32_PerfFormattedData_OfflineFiles_OfflineFiles
  • Win32_PerfRawData_OfflineFiles_OfflineFiles

In the script, we wrote for you, we use the WIN32_OfflineFilesCache WMI class to determine three pieces of information. The first thing we want to know is if the offline files feature is enabled or not. If it is enabled, we need to know if it is active. If the offline files feature is enabled and active, we want to know where the files are stored.

In the GetOffLineFiles.ps1 script, we first need to define a couple of parameters. The first parameter we will use is the –computer parameter. We use the $computer variable to hold the value supplied to the –computer parameter. Localhost is assigned by default to enable the script to run against the local machine. The second parameter we define is –help. We do not set the $help variable that receives values supplied to the –help parameter from the command line to a default value because we are not interested in running Help all the time. The param statement is used to declare both of the command-line parameters. This line of code is seen here:

param($computer=”localhost”, $help)

Next, we need to define a couple of functions. The first function is the funline function. We will use the funline function to underline the header line for our output from the script. The funline function accepts a single input variable, which is called $strIN. The $strIN variable will hold the string value that is passed to it. It then uses the length property to determine how long the string is. We store this value in the $num variable. We next use the for statement to enter a loop that builds up a variable called $funline that will hold a series of equal signs (“=”) that is as long as the string contained in the $strIN variable. AFter we have created this line separator, we use two Write-Host cmdlets to display the string contained in the $strIN variable and the line separator contained in the $funline variable. We use contrasting colors for our two different foregroundcolor parameters. The funline function is seen here:

function funline ($strIN)
{
 $num = $strIN.length
 for($i=1 ; $i -le $num ; $i++)
  { $funline += “=” }
    Write-Host -ForegroundColor yellow $strIN 
    Write-Host -ForegroundColor darkYellow $funline
}

After the funline function is defined, we next define a function that will be used to display online Help data when the script is run with the –help parameter specified. We begin the funhelp function by declaring the variable, –$helpText, and assigning a here-string to it. In the here-string, we assign sections of text for the description, parameters, and command-line syntax. After we have completed the here-string, we display the contents of the $helpText variable, and then exit the script. The funhelp function is seen here:

function funHelp()
{
$helpText=@”
DESCRIPTION:
NAME: GetOffLineFiles.ps1 
Prints the offline files config on a local or remote machine.

PARAMETERS: -computer Specifies name of the computer upon which to run the script -help prints help file

SYNTAX: GetOffLineFiles.ps1 -computer MunichServer

Lists offline files config on a computer named MunichServer

GetOffLineFiles.ps1

Lists offline files config on local computer

GetOffLineFiles.ps1 -help ?

Displays the help topic for the script

“@ $helpText exit }

To determine if we display the contents of the Help file, we use an if statement to check for the presence of the $help variable. If the $help variable is present, it means the script was run with the –help parameter specified. If this is the case, we first call the funline function to underline our status message, and then we call the funhelp function to display the Help file. This line of code is seen here:

if($help){ funline(“Obtaining help …”) ; funhelp }

Now we need to use the WIN32_OfflineFilesCache WMI class to retrieve the three pieces of information required for this script. To do this, we use the Get-Wmiobject cmdlet. We use the –class parameter to tell WMI to query the WIN32_OfflineFilesCache class. We use the –computername parameter to connect to a different computer, if required, and we store the resulting WMI object in the $outtext variable. This section of code is seen here:

$outtxt = Get-WmiObject -Class win32_OfflineFilesCache `
        -computername $computer

Once we have retrieved the offline files configuration information, we use the funline function to print out a status message about the $offline folder. We use the environmental variable computername to retrieve the name of the current computer from the envirionmental PS drive env:\. This line of code is seen here:

funline(“Offline files configuration $env:computername”)

Finally, we use the Format-Table cmdlet to format the output string. We choose the properties we need in the order we wish the data to be displayed. We use the –inputobject parameter and supply the management object stored in the $outtxt variable to this parameter. We then choose the –autosize parameter, so we will have a compact display on the screen. This section of code is seen here:

format-table -Property active, enabled,location -autosize `
       -inputobject $outtxt

WB, we hope you will find the GetOffLineFiles.ps1 script useful, and that it will assist you in determining the status of offline files on your computer. In addition, if you are ever in Seattle, Ed recommends that you do not attempt to eat a dozen Dungeness crabs for lunch by yourself. We hope to see you tomorrow. Until then keep on scripting.

Ed Wilson and Craig Liebendorfer, Scripting Guys

0 comments

Discussion is closed.

Feedback usabilla icon