alanrenouf
 New Member Posts:25
 |
| 03 Oct 2008 01:13 PM |
|
Hi, Im trying to get the OS installed on each of my VM's using WMI but some of the machines I dont have Admin access to, thats fine but I dont want it to stop I want it to keep going through the list, any ideas why this doesnt keep going ?
$ErrorActionPreference = "SilentlyContinue"
Connect-VIServer myviserver
get-vm | Foreach-object {Get-WmiObject -class Win32_OperatingSystem -computername $_}|Select-Object CSName, Caption
I ask here as its a WMI query and not a VI Toolkit issue.
Thanks |
|
|
|
|
Shay
 Basic Member Posts:281

 |
| 03 Oct 2008 01:39 PM |
|
I can't test against VMWare but you can try to set the -errorAction param on the Get-WmiObject cmdlet: Get-WmiObject -class Win32_OperatingSystem -errorAction SilentlyContinue |
|
Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic |
|
|
alanrenouf
 New Member Posts:25
 |
| 03 Oct 2008 01:57 PM |
|
Yeah tried that too, that didnt work for some reason :( |
|
|
|
|
Shay
 Basic Member Posts:281

 |
| 03 Oct 2008 02:01 PM |
|
I see that you pass $_ as computer name. Try you pass the full member name (i dont know what get-vm output as the machine name, so I guess its name), try to pass $_.name. If still don't work, can you try without get-vm and with a bogus names, doe's this output any error: Get-WmiObject -class Win32_foo -computer dontExist -errorAction SilentlyContinue |
|
Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic |
|
|
alanrenouf
 New Member Posts:25
 |
| 03 Oct 2008 02:06 PM |
|
I dont think the name is the issue as it works for about the first 5 machines which I have access to until it gets to one of the machines I dont get access to :(
Thanks for the idea though ! |
|
|
|
|
Shay
 Basic Member Posts:281

 |
| 03 Oct 2008 02:29 PM |
|
Silly me, access denied is a terminating error, add a trap statment: get-vm | Foreach-object {trap {continue}; Get-WmiObject -class Win32_OperatingSystem -computername $_}|Select-Object CSName, Caption |
|
Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic |
|
|
alanrenouf
 New Member Posts:25
 |
| 03 Oct 2008 02:43 PM |
|
You are truly amazing, that worked a treat.
Thanks very much ! |
|
|
|
|
edm365f31
 New Member Posts:39

 |
| 06 Oct 2008 03:35 PM |
|
Will this also work for WMI hangs? |
|
|
|
|
Shay
 Basic Member Posts:281

 |
| 06 Oct 2008 03:45 PM |
|
If it reports an error :-) or until the timeout period has finished. |
|
Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic |
|
|
edm365f31
 New Member Posts:39

 |
| 06 Oct 2008 05:36 PM |
|
Shay I have this script that hangs when I try to get owner..how do I set the trap and time-out, thanks for your help Ed Foreach ($strComputer in Get-Content C:\Powershell\Host\btsp.txt) { $query = "select * from win32_pingstatus where address = '$strcomputer'" $result = Get-WmiObject -query $query if ($result.protocoladdress) { #$Proc = gwmi -query "select * from win32_process where name='Proc.exe'" -comp $strComputer # If ($Proc.processname -eq "Proc.exe") foreach ($Proc in (gwmi -query "select * from win32_process where name='Proc.exe'" -comp $strComputer)) { Trap { Throw "Could not get owner" Return $_ | continue } $c.Cells.Item($intRow,1) = $strComputer $c.Cells.Item($intRow,2) = $Proc.name $c.Cells.Item($intRow,3) = $Proc.ProcessID $c.Cells.Item($intRow,5) = $Proc.ExecutablePath $c.Cells.Item($intRow,6) = $Proc.ConvertToDateTime($Proc.CreationDate) $Proc1 = ($Proc).getowner() $c.Cells.Item($intRow,4) = ($Proc1).user $intRow = $intRow + 1 } Else { $c.Cells.Item($intRow,1) = $strComputer $c.Cells.Item($intRow,2) = "No Proc Process" $intRow = $intRow + 1 } } Else { $c.Cells.Item($intRow,1) = $strComputer $c.Cells.Item($intRow,2) = "Not Responding" $intRow = $intRow + 1 } |
|
|
|
|
Shay
 Basic Member Posts:281

 |
| 19 Oct 2008 10:36 AM |
|
What do you get for this: Foreach ($strComputer in Get-Content C:\Powershell\Host\btsp.txt){ $query = "select * from win32_pingstatus where address = '$strcomputer'" $result = Get-WmiObject -query $query if ($result.protocoladdress){ trap { continue } gwmi -query "select * from win32_process where name='Proc.exe'" -comp $strComputer | ft } } |
|
Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic |
|
|
edm365f31
 New Member Posts:39

 |
| 21 Oct 2008 03:33 PM |
|
Shay - My script hangs when I am trying to get the owner of a process..intermittently..I ran it on 200 + servers I was wondering how do I set up a trap so the script get the error and continue on? $Proc = gwmi -query "select * from win32_process where name='Proc.exe'" -comp $strComputer $proc2=($Proc).getowner() write-host $proc2.user Thanks for ur help |
|
|
|
|
Shay
 Basic Member Posts:281

 |
| 21 Oct 2008 04:12 PM |
|
First, I would ping each machine beofre trying to get the owner. Second, your command can get multiple process objects so it's safer to use foreach:
$procName = "proc.exe"
$strComputer = "serverName"
$ping = gwmi win32_pingstatus -filter "address='$strComputer'"
if($ping.statusCode -eq 0){
# uncomment the next line if you get terminating errors
#trap {continue}
$procs= gwmi win32_process -filter "name='$procName'" -computer $strComputer
$procs | foreach { $_.getowner() } | select __server,user
} |
|
Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic |
|
|