| ...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.
| VB |
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
|
| PowerShell |
$computer = Get-WmiObject -Class Win32_ComputerSystem
"System Name: {0}" -f $computer.name
"Domain : {0}" -f $computer.domain
|
| C# |
using Microsoft.Management.Infrastructure;
...
CimSession session = CimSession.Create("localHost");
IEnumerable<CimInstance> queryInstance = session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_ComputerSystem");
foreach (CimInstance cimObj in queryInstance)
{
Console.WriteLine(cimObj.CimInstanceProperties["Name"].ToString());
Console.WriteLine(cimObj.CimInstanceProperties["Domain"].ToString());
}
|
|
| ...determine whether a computer is a server or a workstation? |
Use the Win32_ComputerSystem class and the DomainRole property.
| VB |
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
|
| PowerShell |
$Computer = Get-WmiObject -Class Win32_ComputerSystem
"Computer "{0}.{1}" is a: "-f $Computer.Name,$computer.domain
switch ($computer.DomainRole) {
0 {"Standalone Workstation"}
1 {"Member Workstation"}
2 {"Standalone Server"}
3 {"Member Server"}
4 {"Backup Domain Controller"}
5 {"Primary Domain Controller"}
}
|
|
| ...determine the computer name? |
Use the Win32_ComputerSystem class and the Name property. You can also use the DNSHostName property in Win32_NetworkAdapterConfiguration.
| VB |
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
|
| PowerShell |
$Computer = Get-WmiObject -Class Win32_ComputerSystem
"Computer Name is: {0}" -f $Computer.Name
|
| C# |
using Microsoft.Management.Infrastructure;
...
CimSession session = CimSession.Create("localHost");
IEnumerable<CimInstance> queryInstance = session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_ComputerSystem");
foreach (CimInstance cimObj in queryInstance)
{
Console.WriteLine(cimObj.CimInstanceProperties["Name"].ToString());
}
|
|
| ...find the name of the person currently logged on to a computer? |
Use the Win32_ComputerSystem class and the UserName property.
| VB |
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
|
| PowerShell |
$computers = Get-WmiObject -Class Win32_ComputerSystem
"Logged on user(s):"
foreach($computer in $computers) {
"User: {0}" -f $computer.UserName
}
|
| C# |
using Microsoft.Management.Infrastructure;
...
CimSession session = CimSession.Create("localHost");
IEnumerable<CimInstance> queryInstance = session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_ComputerSystem");
foreach (CimInstance cimObj in queryInstance)
{
Console.WriteLine("User Name: " + cimObj.CimInstanceProperties["UserName"].ToString());
}
|
|
| ...rename a computer? |
Use the Win32_ComputerSystem class, and the Rename method.
| VB |
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
|
| PowerShell |
param (
[$String] $NewName = 'NewName',
[$string] $Comp = "."
}
<# Get computer object #>
$Computer = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $comp
<# Rename the Computer #>
$Return = $Computer.Rename($NewName)
if ($return.ReturnValue -eq 0) {
"Computer name is now: $NewName"
" but you need to reboot first"
} else {
" RenameFailed, return code: {0}" -f $return.ReturnValue
}
|
|
| ...retrieve only local groups using WMI? |
Use the Win32_Group class and include the following WHERE clause in your WQL query.
Where LocalAccount = True
| VB |
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
|
| PowerShell |
$Accts=Get-WMIObjectWin32_Group|where {$_.LocalAccount}
$accts |ftName, Sid, SidType, Status-autosize
|
|