Creating the NTComputerManagement Class Module

   

Creating the NTComputerManagement Class Module

In this section, you will continue the creation of the NTAdmin.DLL COM server application started in Chapter 3.

Just as we have done in all previous chapters, the manipulation of the IADsComputer, IADsService, and IADsServiceOperations interfaces will be handled by a class module within an ActiveX DLL.

Example 6.1 Creating NTAdmin.DLL: Computer and Service Management Functions
  1. Open the NTAdmin ActiveX DLL Visual Basic project that was started in Chapter 3. You can also download the project from http://www.newriders.com/adsi.

  2. If you are adding to the NTAdmin project, add a new class module to the project. If this is a new project, make sure to set a reference to Active DS Type Library.

  3. Name the new module NTComputerManagement .

  4. Enter the following code into the General Declarations section of the class module:

      Public Function QueryComputerProperty(ByVal TargetDomain As String, ByVal TargetComputer graphics/ccc.gif As String, ByVal PropertyToQuery As String) As String   On Error Resume Next   Dim Computer As IADsComputer   Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", graphics/ccc.gif computer")   Select Case UCase(PropertyToQuery)   Case "DIVISION"   QueryComputerProperty = Computer.Division   Case "OPERATINGSYSTEM"   QueryComputerProperty = Computer.OperatingSystem  Case "OPERATINGSYSTEMVERSION"  QueryComputerProperty = Computer.OperatingSystemVersion   Case "OWNER"               QueryComputerProperty = Computer.Owner   Case "PROCESSOR"               QueryComputerProperty = Computer.Processor   Case "PROCESSORCOUNT"               QueryComputerProperty = Computer.ProcessorCount   End Select   End Function   Public Function QueryServiceProperty(ByVal TargetDomain As String, ByVal TargetComputer graphics/ccc.gif As String, ByVal TargetService As String, ByVal PropertyToQuery As String) As String   On Error Resume Next   Dim Computer As IADsComputer   Dim Service As IADsService   Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", graphics/ccc.gif computer")   Set Service = Computer.GetObject("service", TargetService)   Select Case UCase(PropertyToQuery)   Case "STARTTYPE"   Dim StartType As String   Select Case Service.StartType   Case 0   StartType = "Boot_Start"   Case 1   StartType = "System_Start"   Case 2   StartType = "Automatic"   Case 3   StartType = "Manual"   Case 4   StartType = "Disabled"   End Select   QueryServiceProperty = StartType   Case "DISPLAYNAME"   QueryServiceProperty = Service.DisplayName   Case "HOSTCOMPUTER"   QueryServiceProperty = Service.HostComputer   Case "PATH"   QueryServiceProperty = Service.Path   Case "SERVICEACCOUNTNAME"   QueryServiceProperty = Service.ServiceAccountName   End Select   End Function   Public Function QueryServiceDependencies(ByVal TargetDomain As String, ByVal graphics/ccc.gif TargetComputer As String, ByVal TargetService As String) As Variant   On Error Resume Next   Dim Computer As IADsComputer   Dim Service As IADsService   Dim NewElement() As Variant   Dim i As Long      Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", graphics/ccc.gif computer")   Set Service = Computer.GetObject("service", TargetService)   If IsArray(Service.Dependencies) = True Then   Dim Entry As Variant   For Each Entry In Service.Dependencies   i = UBound(NewElement) + 1   ReDim Preserve NewElement(i)   NewElement(i) = Entry   Next   QueryServiceDependencies = NewElement   Else   QueryServiceDependencies = Array(Service.Dependencies)   End If   End Function   Public Function SetServiceProperty(ByVal TargetDomain As String, ByVal TargetComputer As graphics/ccc.gif String, ByVal TargetService As String, ByVal PropertyToSet As String, ByVal NewValue As graphics/ccc.gif Variant) As Boolean   Dim Computer As IADsComputer   Dim Service As IADsService   Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", graphics/ccc.gif computer")   Set Service = Computer.GetObject("service", TargetService)   Select Case UCase(PropertyToSet)   Case "STARTTYPE"   Service.StartType = NewValue   Service.SetInfo   Case "DISPLAYNAME"   Service.DisplayName = NewValue   Service.SetInfo   Case "PATH"   Service.Path = NewValue   Service.SetInfo   Case "SERVICEACCOUNTNAME"   Service.ServiceAccountName = NewValue   Service.SetInfo   End Select   If Err.Number = 0 Then           SetServiceProperty = True   Else   SetServiceProperty = False   End If   End Function   Public Function AddServiceDependency(ByVal TargetDomain As String, ByVal TargetComputer graphics/ccc.gif As String, ByVal TargetService As String, ByVal NewDependency As String) As Boolean   On Error Resume Next   Dim Computer As IADsComputer   Dim Service As IADsService   Dim NewElement() As Variant   Dim i As Long   Dim EmptyArray As Integer   Dim DependencyAlreadyExists As Integer   Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", graphics/ccc.gif computer")   Set Service = Computer.GetObject("service", TargetService)   If IsArray(Service.Dependencies) = True Then   Dim Entry As Variant   For Each Entry In Service.Dependencies   i = UBound(NewElement) + 1                ReDim Preserve NewElement(i)   NewElement(i) = Entry   If Entry = "" Then EmptyArray = 1   If Entry = NewDependency Then DependencyAlreadyExists = 1   Next   If EmptyArray = 1 Then   Debug.Print "empty"   Service.Dependencies = Array(NewDependency)   Service.SetInfo   Else   If DependencyAlreadyExists <> 1 Then   i = UBound(NewElement) + 1   ReDim Preserve NewElement(i)   NewElement(i) = NewDependency   Service.Dependencies = NewElement   Service.SetInfo   End If   End If   Else   If Service.Dependencies <> NewDependency Then   Service.Dependencies = Array(Service.Dependencies, NewDependency)   Service.SetInfo           End If   End If   Debug.Print Err.Number & " " & Err.Description   If Err.Number = 0 Or Err.Number = 9 Or Err.Number = 92 Then   AddServiceDependency = True      Else   AddServiceDependency = False      End If   End Function   Public Function RemoveServiceDependency(ByVal TargetDomain As String, ByVal graphics/ccc.gif TargetComputer As String, ByVal TargetService As String, ByVal DependencyToRemove As graphics/ccc.gif String) As Boolean   On Error Resume Next   Dim Computer As IADsComputer   Dim Service As IADsService   Dim NewElement() As Variant   Dim i As Long   Dim EntryCounter As Integer   Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", graphics/ccc.gif computer")   Set Service = Computer.GetObject("service", TargetService)   If IsArray(Service.Dependencies) = True Then   Dim Entry As Variant   For Each  Entry In Service.Dependencies  If Entry <> DependencyToRemove Then   EntryCounter = EntryCounter + 1   i = UBound(NewElement) + 1   ReDim Preserve NewElement(i)   NewElement(i) = Entry   End If   Next   Select Case EntryCounter   Case 0   Service.Dependencies = Array("")   Case 1   Service.Dependencies = Entry   Case EntryCounter > 1   Service.Dependencies = NewElement   End Select   Service.SetInfo   Else   If Service.Dependencies = DependencyToRemove Then   Service.Dependencies = Array("")   Service.SetInfo   End If   End If   If Err.Number = 0 Or Err.Number = 9 Then   RemoveServiceDependency = True      Else   RemoveServiceDependency = False      End If   End Function   Public Function EnumerateServices(ByVal TargetDomain As String, ByVal TargetComputer As graphics/ccc.gif String, ByVal WithStatus As Boolean) As Variant   On Error Resume Next   Dim Computer As IADsComputer   Dim Service As IADsService   Dim ServiceStatus As String   Dim NewElement() As Variant   Dim i As Long   Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", graphics/ccc.gif computer")   Computer.Filter = Array("service")   For Each Service In Computer   i = UBound(NewElement) + 1   ReDim Preserve NewElement(i)   If WithStatus = True Then   Select Case Service.Status   Case 1   ServiceStatus = "Stopped"   Case 2   ServiceStatus = "Start Pending"   Case 3   ServiceStatus = "Stop Pending"   Case 4   ServiceStatus = "Running"   Case 5   ServiceStatus = "Continue Pending"   Case 6   ServiceStatus = "Pause Pending"   Case 7                          ServiceStatus = "Paused"   Case 8   ServiceStatus = "Error"   End Select   NewElement(i) = Service.Name & vbTab & ServiceStatus   Else   NewElement(i) = Service.Name           End If   Next   EnumerateServices = NewElement   End Function   Public Function ServiceOperations(ByVal TargetDomain As String, ByVal TargetComputer As graphics/ccc.gif String, ByVal TargetService As String, ByVal ServiceOperation As String) As Boolean   Dim Computer As IADsComputer   Dim Service As IADsServiceOperations   Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", graphics/ccc.gif computer")   Set Service = Computer.GetObject("service", TargetService)   Select Case UCase(ServiceOperation)   Case "START"   If Service.Status = 1 Then   Service.Start   End If   Case "STOP"   If Service.Status = 4 Then   Service.Stop   End If   Case "CONTINUE"   If Service.Status = 7 Then   Service.Continue   End If   Case "PAUSE"   If Service.Status = 4 Then   Service.Pause   End If   End Select   If Err.Number = 0 Then   ServiceOperations = True   Else   ServiceOperations = False   End If   End Function   Public Function SetServiceAccountPassword(ByVal TargetDomain As String, ByVal graphics/ccc.gif TargetComputer As String, ByVal TargetService As String, NewPassword As String) As graphics/ccc.gif Boolean   Dim Computer As IADsComputer   Dim Service As IADsServiceOperations   Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", graphics/ccc.gif computer")   Set Service = Computer.GetObject("service", TargetService)   Call Service.SetPassword(NewPassword)   If Err.Number = 0 Then   SetServiceAccountPassword = True   Else   SetServiceAccountPassword = False   End If   End Function  
  5. Compile the code as NTAdmin.DLL.

  6. Save and close the NTAdmin project.

Tip

You can download the Visual Basic 6.0 project or a pre-compiled version of NTAdmin.DLL from http://www.newriders.com/adsi.


Using the Functions in NTComputerManagement

With the NTComputerManagement class module created, you can access this function from any programming language that supports OLE automation, including Visual Basic, VBScript, and JavaScript.

Tip

To instantiate the object, follow the appropriate syntax found in Chapter 3. Substitute the NTComputerManagement class name where necessary .


Use Table 6.3 to help you use the proper syntax for each of the methods of the NTComputerManagement interface.

Table 6.3. NTComputerManagement Method Syntax.
Action Syntax
Query Computer Division
 QueryComputerProperty("Domain_Name", graphics/ccc.gif "Computer_Name", "Division") 
Query Registered Organization
 QueryComputerProperty("Domain_Name", graphics/ccc.gif "Computer_Name", "Owner") 
Query Operating System Name
 QueryComputerProperty("Domain_Name", graphics/ccc.gif "Computer_Name", "OperatingSystem") 
Query Operating System Version
 QueryComputerProperty("Domain_Name", graphics/ccc.gif "Computer_Name", "OperatingSystemVersion") 
Query Processor Type
 QueryComputerProperty("Domain_Name", graphics/ccc.gif "Computer_Name", "Processor") 
Query Installed HAL
 QueryComputerProperty("Domain_Name", graphics/ccc.gif "Computer_Name", "ProcessorCount") 
Enumerate Installed Services
 For Each Item In EnumerateServices("Domain_Name", graphics/ccc.gif "Computer_Name", False)      Debug.Print Item Next 
Query Service Start Type
 QueryServiceProperty("Domain_Name", graphics/ccc.gif "Computer_Name", "Service_Name","StartType") 
Query Service Display Name
 QueryServiceProperty("Domain_Name", graphics/ccc.gif "Computer_Name", "Service_Name","DisplayName") 
Query Service Host Computer
 QueryServiceProperty("Domain_Name", graphics/ccc.gif "Computer_Name", "Service_Name","HostComputer") 
Query Service Executable Path
 QueryServiceProperty("Domain_Name", graphics/ccc.gif "Computer_Name", "Service_Name","Path") 
Query Service Account Name
 QueryServiceProperty("Domain_Name", graphics/ccc.gif "Computer_Name",  "Service_Name", graphics/ccc.gif "ServiceAccountName") 
Query Service Dependencies
 For Each Item In graphics/ccc.gif QueryServiceDependencies("Domain_Name", graphics/ccc.gif "Computer_Name", "Service_Name")     Debug.Print Item Next 
Set Service Startup Type
 SetServiceProperty("Domain_Name", "Computer_Name", graphics/ccc.gif "Service_Name", "StartType", 2) Valid values for the integer are: 0  Boot_Start, 1  System_Start, 2 graphics/ccc.gif Automatic_Start, 3- Manual_Start, 4  Disabled 
Set Service Display Name
 SetServiceProperty("Domain_Name", "Computer_Name", graphics/ccc.gif "Service_Name", "DisplayName", graphics/ccc.gif "New_Display_Name") 
Set Service Executable Path
 SetServiceProperty("Domain_Name", "Computer_Name", graphics/ccc.gif "Service_Name", "Path", "New_Path") 
Set Service Account Name
 SetServiceProperty("Domain_Name", "Computer_Name", graphics/ccc.gif "Service_Name", "ServiceAccountName", graphics/ccc.gif "Service_Account_Name") 
Add Service Dependency
 AddServiceDependency("Domain_Name", graphics/ccc.gif "Computer_Name", "Service_Name", graphics/ccc.gif "New_Dependency") 
Remove Service Dependency
 RemoveServiceDependency("Domain_Name", graphics/ccc.gif "Computer_Name", "Service_Name", graphics/ccc.gif "Dependency_To_Remove") 
Enumerate Services
 For Each Item In EnumerateServices("Domain_Name", graphics/ccc.gif "Computer_Name", False)      Debug.Print Item Next 
Enumerate Services with Service Status
 For Each Item In EnumerateServices("Domain_Name", graphics/ccc.gif "Computer_Name", True)     Debug.Print Item Next 
Start a Stopped Service
 ServiceOperations("Domain_Name", "Computer_Name", graphics/ccc.gif "Service_Name", "Start") 
Stop a Running Service
 ServiceOperations("Domain_Name", "Computer_Name", graphics/ccc.gif "Service_Name", "Stop") 
Pause a Running Service
 ServiceOperations("Domain_Name", "Computer_Name", graphics/ccc.gif "Service_Name", "Pause") 
Continue a Paused Service
 ServiceOperations("Domain_Name", "Computer_Name", graphics/ccc.gif "Service_Name", "Continue") 
Change Service Password
 SetServiceAccountPassword ("Domain_Name", graphics/ccc.gif "Computer_Name", "Service_Name", "New_Password") 

   
Top


Windows NT. 2000 ADSI Scripting for System Administration
Windows NT/2000 ADSI Scripting for System Administration
ISBN: 1578702194
EAN: 2147483647
Year: 2000
Pages: 194
Authors: Thomas Eck

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