2
$MachineList = Get-Content -Path "E:\ps\comp list\Test Computers.txt"; # One system name per line 
foreach ($Machine in $MachineList)
{ 
($Machine + ": " + @(Get-WmiObject -ComputerName $Machine -Namespace root\cimv2 -Class Win32_ComputerSystem -erroraction silentlycontinue)[0].UserName); 

Write-Output ($Machine + ": " + @(Get-WmiObject -ComputerName $Machine -Namespace root\cimv2 -Class Win32_ComputerSystem -erroraction silentlycontinue)[0].UserName) | Out-File "E:\ps\comp output\Test Computers.txt" -Append
}

更新:这是工作脚本,谢谢大家的帮助!:) 它将计算机打印列表拉入屏幕,然后将它们写入文件。

我找到了这个 powershell 代码并且它可以工作,但我希望它在显示时在用户名前面显示机器名称。我怎样才能让它做到这一点?

就像——

机器名:用户名

我是一个powershell新手......任何帮助将不胜感激!谢谢!:)

4

2 回答 2

4

尝试这个

$MachineList = Get-Content -Path c:\ListOfMachines.txt; # One system name per line
foreach ($Machine in $MachineList){
    ($Machine + ": " + @(Get-WmiObject -ComputerName $Machine -Namespace root\cimv2 -Class Win32_ComputerSystem)[0].UserName);
}
于 2013-11-14T16:36:53.233 回答
1

尝试这个:

Get-Content -Path c:\ListOfMachines.txt | % {
  Get-WmiObject -ComputerName $_ -Namespace root\cimv2 -Class Win32_ComputerSystem -erroraction silentlycontinue | % {
    "$($_.Name): $($_.username)"
  }
}
于 2013-11-14T16:41:19.520 回答