user3499962
user3499962

Reputation: 1

PowerShell: Use Get-Content from multiple text files to send a message

There was very little on the topic of using multiple text files for PowerShell, only found stuff that would take one list and run it against the primary list. Anyway...

My question comes from a need to combine 2 sets of data, equal in the number of rows. Server.txt & SessionID.txt. Both files are created from another Get-XASession query. I wanted to combine these in a Send-XAMessage.

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

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

Here's the code I've tried unsuccessfully... BTW, "ServerX", is a static connection server required for XA Remote computing.

$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" }

For normal usability, we can switch the Stop-XASession, with Get-Service, and use the $s for -ComputerName. And switch SessionId for -ServiceName.

That would look something like this...

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

Upvotes: 0

Views: 3865

Answers (1)

mjolinor
mjolinor

Reputation: 68243

You can do something like this:

$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" 
}

That will cycle the $SessionIds elements in synch with the $server elements. The postincrement operator on $SessionIds[$i++] will increment $i each time it goes through the loop.

Upvotes: 1

Related Questions