# Separate elements in a text file ForEach??

Hello,

I am currently on a project that involves testing the connectivity of posts in a company by sending a ping request.

More precisely, from a POSTES.TXT file containing all the information, the script must receive as argument the name of a room to be tested and the number of tests to be carried out. It then generates a time-stamped text file containing the resulting information. Knowing that if all rooms are tested, the script receives the argument ALL instead of the name of the room. We can use the names and rooms we want but the structure of the file is as follows:
(Room, list of the post of the room [, room, list of the positions of the room, …]).

(Examples: “Script ALL 2” allows to test all the rooms, so ALL the posts, 2 times, or “Script Bât01-Eta00-B01 1” allows to test ONLY the posts present in the room Bât01-Eta00- B01, 1 time)

Here is the POSTES.TXT file:

Salle Bât01-Eta00-B01 172.16.100.20 172.16.100.21 Salle Bât01-Eta01-B11 172.16.100.22 Salle Bât02-Eta00-B01 172.16.100.23 172.16.100.24

Here is what I realized:

Param( [Parameter(Mandatory=$True)] [string]$testSalle,

[Parameter(Mandatory=$True)] [int]$test
)

#Création des variables
$date = Get-Date -Format “yyyyMMdd-HHmmss”$Salle = Get-Content -Path C:\users\louni\Desktop\POSTES.txt | Select-String -Pattern “salle”
$nomSalle =$Salle -replace “salle”,""
$nomPoste = Get-Content -Path C:\Users\louni\Desktop\POSTES.txt | Select-String -Pattern “salle” -NotMatch$ping = ping -n “$test”$nomPoste

#Si $testSalle = ALL alors ping$nomPoste n fois sinon fait le reste

#Séparation des postes par salles
ForEach(
$element in get-content -path C:\users\louni\Desktop\POSTES.txt ) { # If$testSalle is present in $element ??? Then retrieve the following lines and put it in ->$namePoste
# Ping $nomPoste as soon as you cross the word salle stop it  ping -n 1$nomPoste | Out-File -FilePath C:\users\louni\Desktop\"$date"-"$testSalle".txt -Append
}</blockquote>


As you can see the most complex is the separation of posts by rooms and I blocked on … I do not necessarily try the answer but a track of response will be very appreciable :))

For more, I also encountered another error that I did not have: the variable $namePost contains the name of all the posts but when I run ping$ namePost I get: Invalid parameter 172.16.10.20, of course, the Ping command 172.16.10.20 out of the script works.

I practice powershell for a very short time, I really appreciate your help anyway.
Thanks you,

ps : i’m french sorry for bad english.

I didn’t really get what you try to accomplish but instead ot using ping command you could use Test-Connection and instead of a text file you could use a csv. This way it would be easier to have a relation between the room and the according ip addresses. When your csv looks a little bit like this:

Salle;IPAdresses
Bât01-Eta00-B01;172.16.100.20,172.16.100.21
Bât01-Eta01-B11;172.16.100.22
Bât02-Eta00-B01;172.16.100.23,172.16.100.24
your code could be like this:
$InputData = Import-Csv -Delimiter ‘;’ -Path ‘C:\sample\POSTES.csv’$OutputData = ‘C:\sample\POSTES-Results.csv’
Foreach($Input in$InputData){
Foreach($Address in$Input.IPAdresses){
If(Test-Connection -ComputerName $Address -Count 1 -Quiet){$Input.Salle, $Address, ‘reachable’ | Export-Csv -Path$OutputData -Delimiter ‘;’ -Append
}
Else{
$Input.Salle,$Address, ‘unreachable’ | Export-Csv -Path $OutputData -Delimiter ‘;’ -Append } } } ps : I’m German sorry for bad english. Does each line in POSTES.TXT look like this? Salle Bât01-Eta00-B01 172.16.100.20 172.16.100.21 Salle Bât01-Eta01-B11 172.16.100.22  Or does each line in POSTES.TXT look like this? Salle Bât01-Eta00-B01 172.16.100.20 172.16.100.21 Salle Bât01-Eta01-B11 172.16.100.22  Hello thanks for your answer. Sorry Olaf, but I can’t use csv file And POSTES.TXT obligatory look like this : Salle Bât01-Eta00-B01 172.16.100.20 PC3125 Salle Bât01-Eta01-B11 172.16.100.22 Salle ... Ip/hostname ... Thanks [Delete] Ok, this will export ping info to csv. I am sure someone can improve this. Function Test-Room { [CmdletBinding()] Param( [Parameter(Mandatory=$true)]
[ValidateScript({test-path $_})] [string]$Path,
[Parameter()][string[]]$Room, [Parameter()][int]$Number,
[switch]$All ) PROCESS {$file = Get-ChildItem $Path # Make object for each room$myobj = switch -regex -File $file { '^Salle (B.*)' {$m = $Matches[1]; continue } '[^Salle]' {[PSCustomObject]@{Room =$m ; Position = $_}} }$group = $myobj | Group-Object -Property Room # Get a room or all If ($All){
foreach ($g in$group){
Write-Verbose "Testing $($g.Name)"
$ip =$g.Group.Position
Test-Connection -ComputerName $ip -Count$Number |
Select-Object @{n='Room';exp={$g.Name}},address, buffersize,ipv4address } } Else { foreach ($r in $Room){ [void]($r -match '^Salle (B.*)') ; $r =$Matches[1]
$mygroup =$group | Where-Object {$_.Name -eq$r}
Write-Verbose "Testing $($mygroup.Name)"
$ip =$mygroup.Group.Position
Test-Connection -ComputerName $ip -Count$Number |
Select-Object @{n='Room';exp={$r}},address, buffersize,ipv4address } } } }  # Examples # Test-Room -Path \\path\to\folder\POSTES.TXT -Room 'Salle Bât01-Eta00-B01' -Number 1 -Verbose | # Export-Csv "\\path\to\folder\$(Get-Date -Format "yyyyMMdd-HHmmss").csv" -NoTypeInformation

# Test-Room -Path \\path\to\folder\POSTES.TXT -Room 'Salle Bât01-Eta00-B01','Salle Bât02-Eta00-B02' -Number 2 -Verbose |
# Export-Csv "\\path\to\folder\$(Get-Date -Format "yyyyMMdd-HHmmss").csv" -NoTypeInformation # Test-Room -Path \\path\to\POSTES.TXT -All -Number 2 -Verbose | # Export-Csv "\\path\to\folder\$(Get-Date -Format "yyyyMMdd-HHmmss").csv" -NoTypeInformation
# Results: