3

I am trying to find a computername that is used by certain user.

To do it I get a list of computers by Get-ADComputer and then ask all of them if there is a user I am looking for logged on.

This is my script:

$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
    }
}

If -like is my computer: "prg1-7100002421" then it works fine with output:

PRG1-7100002421

But if I set a range by * in -like like this:

$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
    }
}

then output is:

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

it still works but it is full of errors.

By the way output of:

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

is:

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

Am I missing something? Or is there a more direct way to find computername by username than asking all computers if there is a certain user logged on them? This approach is quite slow.

1 Answer 1

1

You are getting the errors from computers which inaccessible, ie switched off, firewalled or don't have the WMI(Winmgmt) service running. Use try/catch to nicely catch the errors by adding -ErrorAction Stop to your query:

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")
    }
}
1
  • Right, they were offline. For some reason I thought it is a list of online computers. Thanks.
    – LukAss741
    Apr 25, 2014 at 12:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .