2016-09-29 32 views
0

我有一個服務器列表(server.txt),我想要獲得所有服務器的「Nimsoft Robot Watcher」的服務狀態。在Get-Service中使用Get-Content

我試過代碼:

$text1 = Get-Content server.txt 
foreach ($text1 in Get-Content server.txt) 
{ 
    $text=$text1 
} 
Get-Service -ComputerName $text -DisplayName "Nimsoft Robot Watcher" 

但只有狀態的第一臺服務器被顯示。

回答

0

您當前擁有的代碼將在每次迭代中替換$text的值,在循環完成後僅留下姓氏。

要麼把Get-Service循環

foreach ($text in Get-Content server.txt) { 
    Get-Service -ComputerName $text -DisplayName "Nimsoft Robot Watcher" 
} 

或內斂的主機在一個數組,並傳遞到Get-Service

$text = Get-Content server.txt 
Get-Service -ComputerName $text -DisplayName "Nimsoft Robot Watcher" 
+0

非常感謝@Asgar,即工作! :) –

+0

我需要添加服務器名稱,以及在服務之前,你能爲此提出建議嗎? –

+0

'Get-Service'返回的對象有一個屬性'MachineName'。 –

相關問題