3

I have a text file of every machine in our Windows Active Directory domain, and I would like to determine the currently logged in user (if any) and operating system of each machine using WMIC:

WMIC /NODE:<machine_name> COMPUTERSYSTEM GET USERNAME
WMIC /NODE:<machine_name> OS GET caption

Does anyone know how this could be scripted to read my list of computers from a text file and execute the two WMIC commands for each and output the results in the format:

ComputerName, OperatingSystem, CurrentUserName
tbone
  • 436
  • 3
  • 8
  • 17

1 Answers1

5

I wouldn't recomend using wmic for this, (although you could use a for lop in a batch file) , as it's far easier in powershell (which is the way the question is tagged). Off the top of my head I'd do this:

clear-Host
$File = "Machines.txt"
get-Content $File | foreach-object { 
$uname = (get-wmiobject win32_computersystem -computername $_).username ;
$os = (get-wmiobject win32_operatingsystem -computername $_).caption ;
"$_ $os $uname"
}

the only bits that aren't fairly obvious is that on line 6 simply typing the names of variables outputs them to the console and the special variable "$_" means "This current object"

Jim B
  • 23,938
  • 4
  • 35
  • 58