2017-07-06 54 views
0

如何獲得沒有運行使用此腳本的計算機的PC的列表?檢索未運行特定進程的PC的列表

<# 
Searches AD for all computers that can ping and checks to see if a process 
is running 
#> 

Import-Module active* 

$PingTest = $null 
$Clist = @() 

Get-ADComputer -Filter * -Properties * | ? {$_.operatingsystem -like "*windows 7*"} | 
    ForEach-Object { 

     # test to see if the computer is on the network 
     $PingTest = Test-Connection -ComputerName $_.name -Count 1 -BufferSize 16 -Quiet 

     # If test is $true adds each computer to the array $Clist 
     If ($PingTest) {$Clist += $_.name} 
     Else {} 

}#ForEach 

#check for process running on each computer in the array $Clist 

Invoke-Command -ComputerName $Clist -ScriptBlock {Get-Process -Name mcshield} 
+0

只是一個側面說明一個電子表格,如果你打算爲簡潔,你也可以使用的foreach對象的別名:'%'旁邊位置對象的'? ' – TheIncorrigible1

回答

1

使用Get-ProcessIf語句中。如果一個進程返回,它將評估爲true。然後,您可以從導出列表,使用Export-Csv

$Computers = Get-ADComputer -Filter "OperatingSystem -like '*Windows 7*'" 
$ProcessRunning = $Computers | 
    ForEach-Object { 
     If (Test-Connection -ComputerName $_.name -Count 1 -BufferSize 16 -Quiet) { 
      If (Get-Process -ComputerName $_.name -Name mcshield -ErrorAction SilentlyContinue) { 
       [pscustomobject]@{ 
        'ComputerName' = $_.name 
        'Process Running' = $True 
       } 
      } Else { 
       [pscustomobject]@{ 
        'ComputerName' = $_.name 
        'Process Running' = $False 
       } 
      } 
     } 
    } 

$ProcessRunning | Export-Csv C:\example\path.csv -NoTypeInformation 
+0

謝謝,我會試試看,並告訴你它是否可以工作 –

+0

很酷我不知道pscustomobject ...非常高興學習。 –

+0

@WrightRobert確認一個好答案的最佳方式是投票和/或選中標記作爲答案。請參閱[爲什麼投票](https://stackoverflow.com/help/why-vote)和[什麼是聲譽?](https://stackoverflow.com/help/whats-reputation) – LotPings