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
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());
}
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");
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
<# 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
}