Export (0) Print
Expand All

WMI Tasks: Accounts and Domains

Account and domain administrative tasks obtain information such as the computer domain or the currently logged-on user. Many of these tasks are best performed with ADSI scripts. For more information and other examples, see the TechNet ScriptCenter Script Repository.

The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.

The following procedure describes how to run a script.

Aa394586.wedge(en-us,VS.85).gifTo run a script

  1. Copy the code and save it in a file with a .vbs extension. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript scriptfile.vbs at the command prompt.

Note  By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript scriptfile.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.

The following table lists script examples that can be used to obtain various types of data from the local computer.

How do I...WMI classes or methods
...determine the domain in which a computer belongs? Use the Win32_ComputerSystem class and check the value of the Domain property. You can also use the DNSDomain property in Win32_NetworkAdapterConfiguration.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings 
    Wscript.Echo "System Name: " & objComputer.Name
    Wscript.Echo "Domain: " & objComputer.Domain
Next
...determine whether a computer is a server or a workstation?Use the Win32_ComputerSystem class and the DomainRole property.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery _
    ("Select DomainRole from Win32_ComputerSystem")
For Each objComputer in colComputers
    Select Case objComputer.DomainRole 
        Case 0 
            strComputerRole = "Standalone Workstation"
        Case 1        
            strComputerRole = "Member Workstation"
        Case 2
            strComputerRole = "Standalone Server"
        Case 3
            strComputerRole = "Member Server"
        Case 4
            strComputerRole = "Backup Domain Controller"
        Case 5
            strComputerRole = "Primary Domain Controller"
    End Select
    Wscript.Echo strComputerRole
Next
...determine the computer name?Use the Win32_ComputerSystem class and the Name property. You can also use the DNSHostName property in Win32_NetworkAdapterConfiguration.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objItem in colItems
    Wscript.Echo "Computer Name: " & objItem.Name
Next
...find the name of the person currently logged on to a computer?Use the Win32_ComputerSystem class and the UserName property.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _ 
    & strComputer & "\root\cimv2") 
Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 
For Each objComputer in colComputer
    Wscript.Echo "User Name = " & objComputer.UserName _
        & VBNewLine & "Computer Name = " & objComputer.Name
WScript.Echo objComputer.UserName
Next
...rename a computer? Use the Win32_ComputerSystem class, and the Rename method.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
    errReturn = ObjComputer.Rename("NewName")
    WScript.Echo "Computer name is now " & objComputer.Name
Next
...retrieve only local groups using WMI? Use the Win32_Group class and include the following WHERE clause in your WQL query.

Where LocalAccount = True

strComputer = "."
Set objWMIService = GetObject( _
    "winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_Group  Where LocalAccount = True")
For Each objItem in colItems
    Wscript.Echo "Local Account: " & objItem.LocalAccount & VBNewLine _
        & "Name: " & objItem.Name & VBNewLine _
        & "SID: " & objItem.SID & VBNewLine _
        & "SID Type: " & objItem.SIDType & VBNewLine _
        & "Status: " & objItem.Status & VBNewLine
Next

 

Related topics

WMI Tasks for Scripts and Applications
WMI C++ Application Examples
TechNet ScriptCenter

 

 

Show:
© 2014 Microsoft