Managing Services

Active Directory uses the concept of a connection point to allow services to advertise themselves throughout a network. A connection point is a simple object that represents an instance of a particular service. Many features of Windows 2000 take advantage of connection points, including print queues, shared folder volumes, Windows Sockets, and the Remote Procedure Call (RPC) naming service. Here are some more details about managing the most common services, printer and file sharing.

Managing Print Queues

Listing 10-7 shows the PrintOps script, available on the companion CD, which is a complete printer control tool. The code is self-explanatory, but with it you can list all the print queues and print jobs on a computer and optionally pause and resume printing. You can also purge all print jobs. Here are some examples of its usage:

 cscript printops.wsf coppersoftware\copper1 /list
cscript printops.wsf coppersoftware\copper1 /pause
cscript printops.wsf coppersoftware\copper1 /resume
cscript printops.wsf coppersoftware\copper1 /flush

 <job >
<reference gu/>
<script language="VBScript">
`
` PrintOps
` List, pause, resume, and purge print queues
`
Set wshArguments = WScript.Arguments

` Get parameters based on number of arguments
Select Case wshArguments.Count

    Case 1
        strComputer = wshArguments(0)
        
    Case 2
        strComputer = wshArguments(0)
        strAction = wshArguments(1)
    
End Select

` Check for no group name or help request
If strComputer = "" Or InStr(1, strGroup, "?", vbTextCompare) > 0 Then

    ` Show usage And quit
    strUsage = "Usage: PrintOps `domain\computername'"
    strUsage = strUsage & vbCrLf & "            [ /list   ]"
    strUsage = strUsage & vbCrLf & "            [ /flush  ]"
    strUsage = strUsage & vbCrLf & "            [ /pause  ]"
    strUsage = strUsage & vbCrLf & "            [ /resume ]"
    WScript.Echo strUsage
    WScript.Quit (1)
End If If strAction = "" Then
    strAction = "/list"
End If
` Figure out action
` Take the left-most 2 characters of the parameter and
` check whether they match into the argument list
nAction = InStr(1, "/f/p/r/l", Left(strAction, 2), vbTextCompare)
` Use NameTranslate to look up the computer in the directory
Set adsNameTranslate = CreateObject("NameTranslate")
` Specify the GC for quick lookups
adsNameTranslate.Init ADS_NAME_INITTYPE_GC, vbNull
` Set the computer name into NameTranslate
` Specify unknown format to have object determine
` Add a dollar sign to indicate a computer account
adsNameTranslate.Set ADS_NAME_TYPE_UNKNOWN, strComputer & "$"
` Get the DN of the computer
strComputerDN = adsNameTranslate.Get(ADS_NAME_TYPE_1779)
` Bind to computer object
Set adsComputer = GetObject("LDAP://" & strComputerDN)
` Enumerate the print queues of this computer
adsComputer.Filter = Array("PrintQueue")
WScript.Echo "Print queues on " & adsComputer.Get("name") & "..."
For Each varPrintQueue In adsComputer
       
    Set adsPrintQueue = GetObject(varPrintQueue.ADsPath)
    
    strQueue = adsPrintQueue.Get("Name")
    strQueue = strQueue & vbCrLf & "Model: " & vbTab & _
        adsPrintQueue.Model
    strQueue = strQueue & vbCrLf & "Description: " & vbTab & _
        adsPrintQueue.Description
    strQueue = strQueue & vbCrLf & "Location: " & vbTab & _
        adsPrintQueue.Location
    strQueue = strQueue & vbCrLf & "Path: " & vbTab & _
        adsPrintQueue.PrinterPath
    WScript.Echo strQueue
         ` Get an interface to the queue ops
    Set adsPrintQueueOps = adsPrintQueue
    
    ` Perform an action
    Select Case nAction
        
        Case 1
            ` Flush printer queues
            WScript.Echo "Removing all print jobs from queue..."
            
            adsPrintQueueOps.Purge
            
        Case 3
            ` Pause the print queue
            WScript.Echo "Pausing print jobs..."
            
            adsPrintQueueOps.Pause
        
        Case 5
            ` Resume the print queue
            WScript.Echo "Resuming print jobs..."
            
            adsPrintQueueOps.Resume
            
        Case Else
            ` Some other action, list jobs in queue
            strAction = "Listing all jobs in queues " & adsComputer.Name
            WScript.Echo strAction
            
            ` Display separator
            WScript.Echo String(Len(strAction), "-")
            
            ` Skip to the next line on errors
            On Error Resume Next
            For Each adsPrintJob In adsPrintQueueOps.PrintJobs
            
                strJob = "Job: " & adsPrintJob.Description
                strJob = strJob & vbCrLf & "User: " & adsPrintJob.User
                strJob = strJob & vbCrLf &  "Priority: " & _
                    adsPrintJob.Priority
                strJob = strJob & vbCrLf &  "Pages: " & _
                    adsPrintJob.TotalPages
                strJob = strJob & vbCrLf &  "Size: " & adsPrintJob.Size
                WScript.Echo strJob
                             Next
            
            ` Turn error handling back on
            On Error GoTo 0
    End Select
    
    ` Display Queue Status
    Select Case adsPrintQueueOps.status
        Case 0
            strStatus = "Normal"
        Case 1
            strStatus = "Paused "
        Case 2
            strStatus = "Error "
        Case 3
            strStatus = "Pending Deletion "
        Case 4
            strStatus = "Paper Jam "
        Case 5
            strStatus = "Paper Out "
        Case 6
            strStatus = "Manual Feed "
        Case 7
            strStatus = "Paper Problem "
        Case 8
            strStatus = "Offline "
        Case &H100
            strStatus = "I/O Active "
        Case &H200
            strStatus = "Busy "
        Case &H400
            strStatus = "Printing "
        Case &H800
            strStatus = "Output Bin Full "
        Case &H1000
            strStatus = "Not Available "
        Case &H2000
            strStatus = "Waiting "
        Case &H4000
            strStatus = "Processing "
        Case &H8000
            strStatus = "Initializing "
        Case &H1000
            strStatus = "Warming Up "
        Case &H2000
            strStatus = "Toner Low "         Case &H4000
            strStatus = "No Toner "
        Case &H8000
            strStatus = "Page Punt"
        Case &H100000
            strStatus = "User Intervention Required"
        Case &H200000
            strStatus = "Out Of Memory "
        Case &H400000
            strStatus = "Door Open "
        Case &H800000
            strStatus = "Server Unknown "
        Case &H1000000
            strStatus = "Power Save "
        Case Else
            strStatus = "Unknown status (" & adsPrintQueueOps.status & ")"
    End Select
    
WScript.Echo "Status: " & strStatus
Next
    
WScript.Echo "Finished."
</script>
</job>

Listing 10-7 PrintOps.wsf shows how to list, pause, resume, and purge jobs in print queues.

Volumes

A feature in Active Directory that doesn't get the attention it deserves is volume objects. These objects represent shared folders. For example, you can tell someone to access a shared folder on a particular computer using a UNC name such as \\server1\applications. This path works great until \\server1 is taken off line and replaced with \\server2. Users could browse and search the network for shared folders, but that might become incredibly tedious in a large organization with dozens, or even hundreds, of computers.

Just as with printers, Active Directory allows the publishing of shared folders. Unlike printers, however, a shared folder is not contained in a computer object hierarchy. Shared folders are represented in Active Directory with objects of the volume class. Objects of the volume class are very simple, containing just six attributes beyond those specified by the top class. Three of the attributes are inherited from the connectionPoint class, and three are specified by the volume class. Table 10-9 lists these attributes.

Volume Attribute Source Class Description

cn

connectionPoint

Mandatory single-valued common name

keywords

connectionPoint

Multivalued strings indicating keywords to be associated with this volume

managedBy

connectionPoint

A DN string pointing to the user managing this volume

contentIndexing-Allowed

volume

Boolean value indicating whether content is indexed on this volume

lastContentIndexed

volume

Timestamp for when volume content was last indexed

uNCName

volume

Mandatory string value containing the UNC path of the volume, for example: \\servername\foldername

Table 10-9 Attributes of the volume class.

Publishing and Using Shared Folders

Unlike printers, there is no provision to automatically publish a shared folder in Active Directory. The volume objects representing shared folders can be created only at the domain root or within an organizational unit. This makes sense because administrators can group all the required resources for a department into a single organizational unit. Using the Find option in the Directory folder of My Network Places, users can query for available shared folders. Users can also browse the Directory namespace looking for shared folders, which is much faster than scanning lists of servers. Once a shared folder is found you can open a window or map a drive letter to the folder. You can map a drive letter to a shared folder programmatically by using the WshNetwork object provided by Windows Script.


Windows Management Instrumentation

Although ADSI was designed for directory management, it also includes a number of network management features. Network management is usually concerned with enumeration of the devices connected to the network and the administration of those devices. To aid network management, Microsoft is focusing on a technology, similar in architecture to ADSI, called Windows Management Instrumentation (WMI).

WMI is Microsoft's implementation of the Web-Based Enterprise Management (WBEM) initiative of the Distributed Management Task Force (DMTF), an industry coalition formed to provide industry standards and recommendations to reduce enterprise management costs. What that means for developers is an object model that enables easier management of network resources. While primarily targeted to network administrators, WMI is useful to all developers of directory-aware products.

With WMI, you can access and manipulate not only network device information on servers but on individual workstations as well. With the object model, you can access information about a computer's hard disks, getting information about file formats, free space, and security information, for example. Setting security on computer and network objects programmatically is made much easier. As an example, network administrators can use WMI to enumerate all the computers in the enterprise that have a particular video driver that needs updating.

WMI is extremely powerful and, while outside the scope of this book, it warrants review by developers of network and directory-aware applications.


Mapping Drive Letters to Shared Folders

Listing 10-8 shows a script, available on the companion CD, that enumerates shared folders that are published in Active Directory and maps them to drive letters. The sample is not picky; it maps all the shared folders it can find in the root of the directory. The script uses the uNCName attribute of the volume object to discover the UNC pathname of the shared folder. The WshNetwork object uses this pathname to perform the mapping operation between a drive letter and the folder.

 <job >
<reference gu/>
<script language="VBScript">
'
' MapSharedFolders - Enumerates all the volume objects at
' the root of the directory and maps them to drive letters
`
' Create the WSH Network object that will perform the mapping
Set objWshNetwork = WScript.CreateObject("WScript.Network")
` Connect to the LDAP server's root object
Set objADsRootDSE = GetObject("LDAP://RootDSE")
` Form an ADsPath string to the name of the default domain
strPath = "LDAP://" + objADsRootDSE.Get("defaultNamingContext")
` Connect to the directory specified in the path
Set objADsContainer = GetObject(strPath)
` Only enumerate shared folders
objADsContainer.Filter = Array("volume")
` For performance reasons, retrieve only the uNCName attribute
objADsContainer.Hints = Array("uNCName")
` Display ADsPath being used
WScript.Echo "Enumerating all shared folders in " & objADsContainer.Name
` Enumerate through all the existing drives to find the last drive letter
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
For Each objDrive in objFileSystem.Drives
    strLastDriveLetter = objDrive.DriveLetter
Next
` Loop through each object in the container
For Each objADs In objADsContainer
    ` Get the UNC path and store it
    strSharePath = objADs.Get("uNCName")
    ` Display the name of the object and the path
    WScript.Echo objADs.Name & vbTab & strSharePath
    ` Increment the drive letter 
    ` BUGBUG:  Will fail after Z!
    strLastDriveLetter = chr( 1 + Asc(strLastDriveLetter) )
    ` Append a colon to the drive letter
    strDriveLetter = strLastDriveLetter & ":"
    ` Map the share to a drive letter
    objWshNetwork.MapNetworkDrive strDriveLetter, strSharePath
Next
` Display all current network drive mappings
WScript.Echo "Current network drive mappings:"
` Enumerate drives (0 is letter, 0+1 is UNC path)
Set objWshNetworkDrives = objWshNetwork.EnumNetworkDrives
For numDrive = 0 to objWshNetworkDrives.Count - 1 Step 2
` Display the drive letter and path
    WScript.Echo objWshNetworkDrives.Item(numDrive) & vbTab & _
        objWshNetworkDrives.Item(numDrive + 1)
Next
` Finished
WScript.Echo "Finished."
</script>
</job>

Listing 10-8 MapSharedFolder.wsf shows how to enumerate shared folders and map them to drive letters.

The Net Use command allows you to map a shared folder to the next available drive letter using an asterisk (*). In the MapSharedFolder script, however, you must supply a drive letter and colon character (i.e., F:) when calling the MapNetworkDrive method.



MicrosoftR WindowsR 2000 Active DirectoryT Programming
MicrosoftR WindowsR 2000 Active DirectoryT Programming
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 108

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net