Previous Section Table of Contents Next Section

Another Example

This business of using associator classes is complicated, so I'm including an additional example of how they work. For this example, let's say you want to list all of the shared folders on a particular file server, along with the physical file path that each share represents. For each of those physical folders (or directories), you want to enable NTFS file compression. Here's what you need to do.

  • Connect to WMI on a specified server.

  • Retrieve a list of Win32_Share class instances that represent file shares (as opposed to printer or other shares)

  • For each instance, retrieve the physical folder as a Win32_Directory class.

  • For each physical folder, use the Compress method.

graphics/arrow.gif Compressing All Shared Folders

Listing 19.2 shows the entire script you'll need to use.

Listing 19.2. CompressAll.vbs. This script compresses all shared folders on a specified file server.

'get server name

strComputer = InputBox("Server name?")



'connect to WMI

Set objWMIService = GetObject("winmgmts:" & _

 "\\" & strComputer & "\root\cimv2")



'retrieve the list of shares

Set colShares = objWMIService.ExecQuery _

 ("SELECT * FROM Win32_Share WHERE " & _

 "Type = 0")



'for each share returned...

For Each objShare In colShares



 'retrieve the associated folders

 Set colFolders = objWMIService.ExecQuery _

  ("ASSOCIATORS OF {Win32_Share.Name='" & _

  objShare.Name & "'} WHERE " & _

  "AssocClass=Win32_ShareToDirectory")



 'for each folder returned...

 For Each objFolder in colFolders



  'is it already compressed?

  If objFolder.Compressed Then



   'yes - message

   Wscript.Echo objFolder.Name & " is already compressed."



  Else



   'no - message & compress it

   WScript.Echo "Compressing " & objFolder.Name

   objFolder.Comrpess



  End If



 Next



Next

You shouldn't need to make any modifications to this script to run it, and it should work with NT 4.0 and later servers.

graphics/arrow.gif Compressing All Shared Folders-Explained

The script starts by simply asking for the server name. Provide the name of any NT 4.0 or later file server that's already running WMI.


'get server name

strComputer = InputBox("Server name?")

Next, the script connects to the WMI service on the remote computer.


'connect to WMI

Set objWMIService = GetObject("winmgmts:" & _

 "\\" & strComputer & "\root\cimv2")

The script now executes a simple WMI query to return all shares of type 0, which are shared folders. The WMI documentation for the Win32_Share class lists other types, including printers (1), devices (2), IPC shares (3), and administrative shares.


'retrieve the list of shares

Set colShares = objWMIService.ExecQuery _

 ("SELECT * FROM Win32_Share WHERE " & _

 "Type = 0")

A For Each…Next loop iterates through each file share.


 'for each share returned...

For Each objShare In colShares

An ASSOCIATORS OF query is used to retrieve the associated folder (Win32_Directory) instances for the current Win32_Share instance. Notice that the associator class, Win32_ShareToDirectory, is specified.


 'retrieve the associated folders

Set colFolders = objWMIService.ExecQuery _

 ("ASSOCIATORS OF {Win32_Share.Name='" & _

 objShare.Name & "'} WHERE " & _

 "AssocClass=Win32_ShareToDirectory")

A For Each…Next loop iterates through each folder returned. Under current Windows operating systems, this will be only one folder per share (although you might theorize that some future version would allow multiple, load-balanced physical folders per share, which is why WMI requires you to write the script this way).


'for each folder returned...

For Each objFolder in colFolders

The script checks to see if the folder is already compressed, and behaves accordingly.


'is it already compressed?

If objFolder.Compressed Then



 'yes - message

 Wscript.Echo objFolder.Name & " is already compressed."



Else



 'no - message & compress it

 WScript.Echo "Compressing " & objFolder.Name

 objFolder.Comrpess



End If

Finally, the script closes the two open For Each…Next loops.


 Next



Next

The powerful and easy ASSOCIATORS OF query makes scripts like this easier to write. Without it, you'd be stuck with many more For…Next loops and a much-harder-to-maintain script.

    Previous Section Table of Contents Next Section