Previous Section Table of Contents Next Section

Using the WinNT Provider

With Active Directory several years old, and now available in its second version (Wind2003), why would you bother using the WinNT provider? Ease of use. Although the WinNT provider is definitely less functional than the LDAP provider is, it's easier to use, and there are certain functions that you cannot easily do with the LDAP provider, such as connecting to a file server service. You can do some of those things with WMI, but again…ease of use. There are just some things, as you'll see, that the WinNT provider makes easy. For example, in Chapter 10, I showed you how the WinNT provider can be used to connect to a file server and find out which users have a particular file open.

Here's an example of how the WinNT provider can be used to connect to a file server and list its available shares.


ServerName = InputBox("Enter name of server " & _

 "to list shares for.")



set fs = GetObject("WinNT://" & ServerName & _

 "/LanmanServer,FileService")

For Each sh In fs

     'do something with the share

Next

You can do the same thing in WMI.


'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

 'do something with the share

Next

The ADSI method is obviously easier. Notice something about how the ADSI call is written.


set fs = GetObject("WinNT://" & ServerName & _

 "/LanmanServer,FileService")

The first part, as I noted earlier, is the provider: WinNT. Next is the server name, which in this case is provided in a string variable. Next is the name of the object you want to connect to, a comma, and the type of object that is. The comma and type are optional. For example, the following would usually work fine.


set fs = GetObject("WinNT://" & ServerName & _

 "/LanmanServer")

This method lets ADSI pick the object based solely on its name. If you have a user or group named LanmanServer, ADSI might pick one of those, which is why I usually specify the object type. Doing so restricts ADSI's options to the type of object I'm expecting. Connecting to a user object would be similar.


set fs = GetObject("WinNT://" & ServerName & _

 "/DonJ,user")

Or a group:


set fs = GetObject("WinNT://" & ServerName & _

 "/Guests,group")

What do you specify for the server name? If you want a domain user or group, specify either the domain name or the name of a domain controller. If you want a local user or group, or a service, specify a server name. Keep in mind that all of these techniques will work perfectly with NT, 2000, XP, and 2003 computers in either an NT domain or an AD domain.

Examples

Here's an example of how to start a service by using the WinNT provider.


Set objService = GetObject("WinNT://Server1/browser")

objService.Start

Set objService = Nothing

Obviously, you can change the service name to anything valid on the computer. You can stop the service by using the Stop method instead of Start.

Here's an example of how to output all members of a group to a text file. This example uses the FileSystemObject to create the text file and the WinNT provider to access the group membership list.


Dim oGroup

  Dim sGroupName

  Dim sGroupDomain

  Dim oMember

  Dim oTS

  Dim oFSO



  const ForReading = 1

  const ForWriting = 2

  const ForAppending = 8

  Const TristateFalse = 0



  sGroupDomain = "DomainName"

  sGroupName = InputBox ("Group name?")

  Set oFSO = CreateObject ("Scripting.FileSystemObject")

  Set oTS = oFSO.OpenTextFile ("C:\Scripts\" & _

 GroupName & " members.txt")



Set oGroup = GetObject("WinNT://" & GroupDomain & "/" & _

 GroupName & ",group")



  For Each oMember in oGroup.Members

   oTS.WriteLine oMember.Name

  Next



WScript.Echo "Complete"

The following script connects to a domain, iterates through each object, and for the user objects it finds, outputs the total size of the user's home directory.


Dim oDomain, oFolder

Dim oFSO, oTS, oUser



Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oTS = oFSO.CreateTextFile("c:\homedirs.txt")

Set oDomain = GetObject("WinNT://DOMAIN")



For Each oUser in oDomain

 If oUser.Class = "User" Then

  Set oFOlder = oFSO.GetFolder(oUser.HomeDirectory)

  oTS.WriteLine( _

   oFolder.Name & "," & oUser.HomeDirectory & "," & _

    oFolder.Size)

 End IF

 Set oFolder = Nothing

Next

You can see in each of these examples how the WinNT provider makes the task a bit easier by providing ready access to the necessary information.

    Previous Section Table of Contents Next Section