2014-04-05 51 views
0

PowerShell使用多個文本文件的主題非常少,只發現了一些需要使用一個列表並在主列表中運行的列表。無論如何...PowerShell:使用Get-Content從多個文本文件發送消息

我的問題來自需要結合兩組數據,相等的行數。 Server.txt & SessionID.txt。這兩個文件都是從另一個Get-XASession查詢創建的。 我想在Send-XAMessage中組合這些。

Servers.txt = "Server1","Server2","Server3",etc.

SessionIds.txt = "2","41","18",etc.

下面是我嘗試過失敗的代碼... 順便說一句,「ServerX」,是XA遠程計算所需靜態連接的服務器。

$Server = Get-Content .\Server.txt

$SessionIds = Get-Content .\SessionIds.txt

ForEach ($s in $Servers -And $i in $SessionIds) { Send-XASession -ComputerName ServerX -ServerName $s -SessionId $i -MessageTitle "MsgTitle" -MessageBody "MsgBody" }

對於正常的可用性,我們可以使用Get-Service切換Stop-XASession,並使用$ s作爲-ComputerName。 併爲-ServiceName切換SessionId。

這將是這個樣子......

ForEach ($s in $Servers -And $i in $Sevices) { Get-Service -ComputerName $s -Name $i } | FT Name,Status

  • 唯一重要的事情,就是在兩個文本文件的每一行是通過同時運行。 沒有重複。將Servers.txt中的第1行與SessionIds.txt中的第1行相匹配,並在每個命令中使用它。 任何幫助將不勝感激。

回答

0

你可以做這樣的事情:

$Server = Get-Content .\Server.txt 

$SessionIds = Get-Content .\SessionIds.txt 

$i=0 

ForEach ($s in $Servers) 

{ 
    Send-XASession -ComputerName ServerX -ServerName $s -SessionId $SessionIds[$i++] -MessageTitle "MsgTitle" -MessageBody "MsgBody" 
} 

這將循環在同步的$ SessionIds元素與$服務器元素。 $ SessionIds [$ i ++]上的postincrement運算符將在每次經過循環時增加$ i。

+0

巧妙的想法索引文本文件,但現在我得到一個錯誤... 錯誤: 無法索引到一個空數組。 – user3499962

+0

我甚至試圖設置「_ $我= 1_」,仍然沒有去,相同的錯誤味精。 – user3499962

+0

好的,所以我仍然在學習...... MessageBody上缺少結尾的'''圓括號。 這個解決方案工作的非常好,謝謝。 – user3499962

相關問題