Separate elements in a text file ForEach??

This topic contains 5 replies, has 3 voices, and was last updated by  random commandline 1 year, 5 months ago.

  • Author
  • #63776



    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
    Salle Bât01-Eta01-B11
    Salle Bât02-Eta00-B01

    Here is what I realized:



    #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
    $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

    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, of course, the Ping command 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.

  • #63782

    Olaf Soyk

    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:


    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
                $Input.Salle, $Address, 'unreachable' | Export-Csv -Path $OutputData -Delimiter ';' -Append

    ps : I'm German sorry for bad english. 😉

  • #63788

    random commandline

    Does each line in POSTES.TXT look like this?

    Salle Bât01-Eta00-B01
    Salle Bât01-Eta01-B11

    Or does each line in POSTES.TXT look like this?

    Salle Bât01-Eta00-B01
    Salle Bât01-Eta01-B11
  • #63805


    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
    Salle Bât01-Eta01-B11
    Salle ...


  • #63899

    random commandline


  • #63901

    random commandline

    Ok, this will export ping info to csv. I am sure someone can improve this.

    Function Test-Room {
        [ValidateScript({test-path $_})]
        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,
            } 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,
    # 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:
    # Room            address   buffersize IPV4Address 
    # ----            -------   ---------- ----------- 
    # Bât01-Eta00-B01 localhost         32
    # Bât01-Eta00-B01      32
    # Bât02-Eta00-B02      32    

You must be logged in to reply to this topic.