by duonut at 2012-12-06 08:40:21
Hiby coderaven at 2012-12-06 11:17:22
I am just starting out with powershell and I have the following script:Get-ADComputer -Filter * |
ForEach-Object {
if(Test-Connection -CN $.dnshostname -Count 1 -BufferSize 16 -Quiet)
{
write-host $.dnshostname
Get-WmiObject Win32_Process -cn $.dnshostname | Select ProcessID,ProcessName,ThreadCount
Get-WmiObject Win32_DiskDrive -cn $.dnshostname;
}
ELSE { write-host $.dnshostname unavlible }
}
Basically what I am trying to achieve is pull from my DC a list of every computer, test if they are up and then poll them with multiple WMI queries. The problem I am having is that when I am doing my select on the first Wmi object is that I then dont get the result of the new wmi object as I am assuming the select is being applied to the second object causing no data to be returned.
Any idea how I can do this. Also right now I am using a ping to test a machine avalibility but this is not the best as ICMP is blocked on some systems but WMI is still possible. The same goes for ICMP being avalible but wmi blocked. Is there a better way to perform the test of getting a list of systems that the WMI query will work?
I am thinking that you are saying that the select is applying to the $ object.by RichardSiddaway at 2012-12-06 12:29:41
If that is the case, try this(Get-WmiObject Win32_Process -ComputerName $.dnshostname) | Select-Object ProcessID,ProcessName,ThreadCount
You have tripped over a problem that PowerShell has when trying to output two different object types. The first is created when you do the select on the processes. The second is created when you get the Win32_DiskDrive object. The disk object doesn’t have any fields that match the selection from the processes so doesn’t display anything.
Either create a new object that combines the process and disk info or for an easier solution use format-list to control the output like thisGet-ADComputer -Filter * | where DNSHostName -eq "Server02.Manticore.org" |
ForEach-Object {
if(Test-Connection -CN $.dnshostname -Count 1 -BufferSize 16 -Quiet)
{
write-host $.dnshostname
Get-WmiObject Win32_Process -cn $.dnshostname | Select ProcessID,ProcessName,ThreadCount | Format-List
Get-WmiObject Win32_DiskDrive -cn $.dnshostname | Format-List
}
ELSE { write-host $.dnshostname unavlible }
}