通过用户名获取计算机名

通过用户名获取计算机名

我正在尝试查找特定用户使用的计算机名称。

为此,我通过 Get-ADComputer 获取计算机列表,然后询问所有计算机是否有我正在寻找的用户登录。

这是我的脚本:

$pcs = Get-ADComputer -filter {name -like "prg1-7100002421" -and enabled -eq "true"} |  Select-Object name
foreach($pc In $pcs)
{
    if (@(Get-WmiObject -ComputerName $pc.name -Namespace root\cimv2 -Class Win32_ComputerSystem)[0].UserName -eq "ANT\username")
    {
        $pc.name
    }
}

如果 -like 是我的计算机:“prg1-7100002421”那么它的输出正常:

PRG1-7100002421

但是如果我用 * 设置一个范围,如下所示:

$pcs = Get-ADComputer -filter {name -like "prg1-710000242*" -and enabled -eq "true"} |  Select-Object name
foreach($pc In $pcs)
{
    if (@(Get-WmiObject -ComputerName $pc.name -Namespace root\cimv2 -Class Win32_ComputerSystem)[0].UserName -eq "ANT\username")
    {
        $pc.name
    }
}

那么输出是:

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\lbartuse\Desktop\user to pc.ps1:4 char:8
+     if (@(Get-WmiObject -ComputerName $pc.name -Namespace root\cimv2 -Class Win32_C ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

PRG1-7100002421
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\lbartuse\Desktop\user to pc.ps1:4 char:8
+     if (@(Get-WmiObject -ComputerName $pc.name -Namespace root\cimv2 -Class Win32_C ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\lbartuse\Desktop\user to pc.ps1:4 char:8
+     if (@(Get-WmiObject -ComputerName $pc.name -Namespace root\cimv2 -Class Win32_C ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\lbartuse\Desktop\user to pc.ps1:4 char:8
+     if (@(Get-WmiObject -ComputerName $pc.name -Namespace root\cimv2 -Class Win32_C ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\lbartuse\Desktop\user to pc.ps1:4 char:8
+     if (@(Get-WmiObject -ComputerName $pc.name -Namespace root\cimv2 -Class Win32_C ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

它仍然可以工作但是充满了错误。

顺便输出:

$pcs = Get-ADComputer -filter {name -like "prg1-710000242*" -and enabled -eq "true"} |  Select-Object name
foreach($pc In $pcs)
{
    $pc.name
}

是:

PRG1-7100002420
PRG1-7100002421
PRG1-7100002422
PRG1-7100002423
PRG1-7100002424
PRG1-7100002425
PRG1-7100002426
PRG1-7100002427
PRG1-7100002428
PRG1-7100002429

我是不是漏掉了什么?或者有没有比询问所有计算机是否有某个用户登录更直接的方法来通过用户名查找计算机名?这种方法相当慢。

答案1

您收到来自无法访问的计算机的错误,例如已关闭、防火墙或未运行 WMI(Winmgmt) 服务的计算机。使用 try/catch 可以很好地捕获错误,方法是添加-ErrorAction 停止您的查询:

foreach($pc In $pcs)
{   
    try{
        if (@(Get-WmiObject -ComputerName $pc.name -Namespace root\cimv2 -Class Win32_ComputerSystem -ErrorAction Stop )[0].UserName -eq "ANT\username")
        {
            $pc.name
        }
    }
    catch{
        Write-Host ($pc.name + " is inaccessible")
    }
}

相关内容