I'm trying to write a script which displays some data from the devices in the domain.
I've already experienced a bit with AD and Get-WmiObject
with success. But when I try to incorporate this into a working script it fails.
$Computers = Get-ADComputer -filter * | Select-Object -First 1 | Select-Object Name
Write-Host $computers
Invoke-Command -ArgumentList $Computers -ScriptBlock {
(
Get-WmiObject -Class win32_process -ComputerName $computers | Where {
$_.name -Match "explorer*"
}
) | ForEach-Object {
@{ PC = $computers; User = $_.getowner().user}
} | Out-GridView
}
This code works when I set computers to any computer in my domain. But if I try to go through the list created by Get-ADComputer
, the program prints the computer's names and then
shows this:
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\admin_cubido\Scripts\WorksWIthOne.ps1:6 char:69
I can call Wmi methods normally from any computer if I specify one name without error message. I´ve alredy enabled WMI in group policy.
Edit: Would it be possible to incorporate this command?
Here are a few comments:
$Computers = Get-ADComputer -filter * | Select -first 1 | Select Name
# At this point $Computers contains a collection of objects having a Name attribute
echo $computers
Invoke-Command -ArgumentList $Computers -ScriptBlock {
# you don't need Invoke-Command, just call Get-WMIObject passing each compuer's name
# through -ComputerName parameter
(
Get-WmiObject -Class win32_process -ComputerName $computers | Where {
$_.name -Match "explorer*"
}
) | ForEach-Object {
@{ PC = $computers; User = $_.getowner().user}
} | Out-GridView
}
This should be the simplest fix for your code:
$Computers = Get-ADComputer -filter * |
Select-Object -first 1 |
Select-Object -expandProperty Name
# with -expandProperty, $Computers should contain an array of strings
# instead of an object with a .Name attribute
write-host $computers
foreach ($name in $computers) {
Get-WmiObject -Class win32_process -ComputerName $name | Where-Object {
$_.name -Match "explorer*"
} | ForEach-Object {
@{ PC = $name; User = $_.getowner().user}
} | Out-GridView
}
edit I've changed above to try to work-around the v2.0 issues, but I haven't a test environment to confirm with.
I think that should get the code doing something, but your Get-WMIObject
call may take a long time if there are many computers in your list. I'm not sure if Get-WMIObject
runs the query in parallel or sequentially when providing a list of computer names.
User contributions licensed under CC BY-SA 3.0