Estou tentando encontrar um nome de computador que seja usado por determinado usuário.
Para fazer isso, recebo uma lista de computadores pelo Get-ADComputer e, em seguida, pergunto a todos eles se houver um usuário que eu esteja procurando conectado.
Este é o meu 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
}
}
Se -like é o meu computador: "prg1-7100002421" então funciona bem com a saída:
PRG1-7100002421
Mas se eu definir um intervalo por * em, como assim:
$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
}
}
a saída é:
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
ainda funciona, mas está cheio de erros.
A propósito, a saída de:
$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
Estou faltando alguma coisa? Ou existe uma maneira mais direta de encontrar nome de computador por nome de usuário do que perguntar a todos os computadores se existe algum usuário logado neles? Essa abordagem é bem lenta.