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.
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.
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.
Name the new module NTComputerManagement .
Enter the following code into the General Declarations section of the class module:
Public Function QueryComputerProperty(ByVal TargetDomain As String, ByVal TargetComputer As String, ByVal PropertyToQuery As String) As String On Error Resume Next Dim Computer As IADsComputer Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", 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 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 & ", 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 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 & ", 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 String, ByVal TargetService As String, ByVal PropertyToSet As String, ByVal NewValue As Variant) As Boolean Dim Computer As IADsComputer Dim Service As IADsService Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", 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 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 & ", 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 TargetComputer As String, ByVal TargetService As String, ByVal DependencyToRemove 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 EntryCounter As Integer Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", 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 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 & ", 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 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 & ", 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 TargetComputer As String, ByVal TargetService As String, NewPassword As String) As Boolean Dim Computer As IADsComputer Dim Service As IADsServiceOperations Set Computer = GetObject("WinNT://" & TargetDomain & "/" & TargetComputer & ", 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
Compile the code as NTAdmin.DLL.
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.
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.
Action | Syntax |
---|---|
Query Computer Division | QueryComputerProperty("Domain_Name", "Computer_Name", "Division") |
Query Registered Organization | QueryComputerProperty("Domain_Name", "Computer_Name", "Owner") |
Query Operating System Name | QueryComputerProperty("Domain_Name", "Computer_Name", "OperatingSystem") |
Query Operating System Version | QueryComputerProperty("Domain_Name", "Computer_Name", "OperatingSystemVersion") |
Query Processor Type | QueryComputerProperty("Domain_Name", "Computer_Name", "Processor") |
Query Installed HAL | QueryComputerProperty("Domain_Name", "Computer_Name", "ProcessorCount") |
Enumerate Installed Services | For Each Item In EnumerateServices("Domain_Name", "Computer_Name", False) Debug.Print Item Next |
Query Service Start Type | QueryServiceProperty("Domain_Name", "Computer_Name", "Service_Name","StartType") |
Query Service Display Name | QueryServiceProperty("Domain_Name", "Computer_Name", "Service_Name","DisplayName") |
Query Service Host Computer | QueryServiceProperty("Domain_Name", "Computer_Name", "Service_Name","HostComputer") |
Query Service Executable Path | QueryServiceProperty("Domain_Name", "Computer_Name", "Service_Name","Path") |
Query Service Account Name | QueryServiceProperty("Domain_Name", "Computer_Name", "Service_Name", "ServiceAccountName") |
Query Service Dependencies | For Each Item In QueryServiceDependencies("Domain_Name", "Computer_Name", "Service_Name") Debug.Print Item Next |
Set Service Startup Type | SetServiceProperty("Domain_Name", "Computer_Name", "Service_Name", "StartType", 2) Valid values for the integer are: 0 Boot_Start, 1 System_Start, 2 Automatic_Start, 3- Manual_Start, 4 Disabled |
Set Service Display Name | SetServiceProperty("Domain_Name", "Computer_Name", "Service_Name", "DisplayName", "New_Display_Name") |
Set Service Executable Path | SetServiceProperty("Domain_Name", "Computer_Name", "Service_Name", "Path", "New_Path") |
Set Service Account Name | SetServiceProperty("Domain_Name", "Computer_Name", "Service_Name", "ServiceAccountName", "Service_Account_Name") |
Add Service Dependency | AddServiceDependency("Domain_Name", "Computer_Name", "Service_Name", "New_Dependency") |
Remove Service Dependency | RemoveServiceDependency("Domain_Name", "Computer_Name", "Service_Name", "Dependency_To_Remove") |
Enumerate Services | For Each Item In EnumerateServices("Domain_Name", "Computer_Name", False) Debug.Print Item Next |
Enumerate Services with Service Status | For Each Item In EnumerateServices("Domain_Name", "Computer_Name", True) Debug.Print Item Next |
Start a Stopped Service | ServiceOperations("Domain_Name", "Computer_Name", "Service_Name", "Start") |
Stop a Running Service | ServiceOperations("Domain_Name", "Computer_Name", "Service_Name", "Stop") |
Pause a Running Service | ServiceOperations("Domain_Name", "Computer_Name", "Service_Name", "Pause") |
Continue a Paused Service | ServiceOperations("Domain_Name", "Computer_Name", "Service_Name", "Continue") |
Change Service Password | SetServiceAccountPassword ("Domain_Name", "Computer_Name", "Service_Name", "New_Password") |
Top |