2

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 a result of the second 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 available but wmi blocked. Is there a better way to perform the test of getting a list of systems that the WMI query will work?

4

1 回答 1

2

我从 powershell v1 到 v3 发现的一个简单的解决方法是:

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
     out-string -inputobject "" #this line does the trick or just out-default
     Get-WmiObject Win32_DiskDrive -cn $_.dnshostname;
   }
  else { write-host "$($_.dnshostname) unavailable }    
}

为了测试可用性服务器,我使用此功能 ,它使用 ping 和其他协议来查看服务器是否已启动,您只需要true知道服务器是否还活着。(作为系统管理员建议在域(而不是 DMZ 域)中启用icmp协议作为第一个诊断工具)

于 2012-12-06T19:51:36.007 回答