PowerCLI

 View Only
  • 1.  Ping Multiple VMs and save the ping results to a text file

    Posted Jan 25, 2019 02:45 PM

    Hi

    I need to record some of the ping results of VMs

    My requirements

    When i execute the script I need to ping multiple VMs and record the ping results to a text file and when i need to stop I can press CTRL C and break the pings

    I have done this script

    @ECHO

    START CMD ping NLAMSV00xxx -t -w 5000 >c:\pingresults\NLAMSV00xx0.txt

    START CMD ping NLFRAV0xxx -t -w 5000 >c:\pingresults\NLFRAV001xxx.txt

    START CMD ping NLAMSV0xxx -t -w 5000 >c:\pingresults\NLAMSV000xxx.txt

    START CMD ping NLFRAV0xxx -t -w 5000 >c:\pingresults\NLFRAV00xxx.txt

    START CMD ping NLAMSV00xxx -t -w 5000 >c:\pingresults\NLAMSV00xxx.txt

    START CMD ping NLFRAxxx -t -w 5000 >c:\pingresults\NLFRAV003xxx.txt

    When I execute the script file I get multiple windows open and the ping works, but when i check the saved text files it's all blank

    I need to save the results in test file thats my main aim

    can someone help me out pleas



  • 2.  RE: Ping Multiple VMs and save the ping results to a text file

    Posted Jan 25, 2019 03:26 PM

    The separate windows are there because you start multiple DOS sessions with the cmd comand.

    The -t parameter says that the ping should go on forever, until interrupted with Ctrl-C.

    That could be a problem for capturing the output.

    I don't know how you got the hostnames, but assume they are for VMs.
    Then you could for example do

    $folder = 'C:\Temp'

    Get-VM -PipelineVariable vm |

       ForEach-Object -Process {

       Invoke-Command -ScriptBlock ([scriptblock]::Create("ping $($vm.Name) -n 10 -w 5000")) | Out-File -FilePath "$folder\$($vm.Name).txt"

    }

    If the hosts are not coming from VMs, you can easily change that the names are for example obtained from a list.

    $folder = 'C:\Temp'

    $names = @('name1', 'name2')

    foreach ($name in $names) {

       Invoke-Command -ScriptBlock ([scriptblock]::Create("ping $($name) -n 10 -w 5000")) | Out-File -FilePath "$($folder)\$($name).txt"

    }



  • 3.  RE: Ping Multiple VMs and save the ping results to a text file

    Posted Jan 25, 2019 05:03 PM

    Hi LucD

    Thanks for the reply, my problem here is i need to execute the Ping at the same time i.e I need to run the ping results on multiple VMs at the same time and capture the results

    In the line below it will execute the ping one by one so if I have 50 vms on  a list the pins are done one after the other

    my requirement is to ping all the VMs at the same time and savethe ping results

    Invoke-Command -ScriptBlock ([scriptblock]::Create("ping $($vm.Name) -n 10 -w 5000")) | Out-File -FilePath "$folder\$($vm.Name).txt"

    Is there a different way to do that ?

    Many thanks

    Rxjoseph



  • 4.  RE: Ping Multiple VMs and save the ping results to a text file
    Best Answer

    Posted Jan 25, 2019 05:09 PM

    Yes, you can use background jobs.
    They are started in sequence, but they don't wait for the end of the ping before statring the next one.

    $names = @('vm1', 'vm2')

    $code = {

       param([string]$Name)


       $folder = 'C:\Temp'


       Invoke-Command -ScriptBlock ([scriptblock]::Create("ping $($Name) -n 10 -w 5000")) | Out-File -FilePath "$($folder)\$($Name).txt"

    }


    foreach ($name in $names) {

       Start-Job -Name Ping -ScriptBlock $code -ArgumentList $name

    }


    Wait-Job -Name Ping



  • 5.  RE: Ping Multiple VMs and save the ping results to a text file

    Posted Jan 25, 2019 05:40 PM

    Ho LucD

    Thank you sir the script is now working the way i want, you are absolutely a powershell super star

    Have a good weekend

    Many thanks

    Rxjoseph