powershell command?

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

  • Author
    Posts
  • #51828

    Craig Redfern
    Participant

    When I ping a IP in powershell it does the pings followed by a ping statistics. I use it to ping guest OS (hyper-v VM) from my host OS ie ping 37.221.***.36 with ping destination Host unreachable If the guest VM is offline. When a Destination Unreachable is created can it trigger a pipe chain command | Save-vm -Name (***); Stop-vm -force -Name (***); Start-vm -asjob -Name (***). I just want a chain pipe command that when the host is unreachable the guest OS is shutdown and restarted?

  • #51832

    Daniel Krebs
    Moderator

    I think it would be better to use the PowerShell native cmdlet Test-Connection instead of the ping.exe command.

    Example:

    $ComputerName = 'vmguest01'
    if (-not (Test-Connection -ComputerName $ComputerName -Quiet)) {
        Save-VM -Name $ComputerName
        Stop-VM -Name $ComputerName -Force
        Start-VM -Name $ComputerName -AsJob
    }
    
    • This reply was modified 1 year, 3 months ago by  Daniel Krebs.
  • #51836

    Dave Wyatt
    Moderator

    Instead of using ping.exe for this, I'd recommend the Test-Connection cmdlet. It does the same thing, but is easier to script around. In this case, I'd use the -Quiet switch (which just returns $true if the host is online, $false if it's not). Something like this:

    if (-not (Test-Connection $yourIPAddress -Quiet))
    {
        # Your VM commands here if it's offline
    }
    
    • #51855

      Craig Redfern
      Participant

      Is there a command where I do Test-Connection 192.168.(***).104 it shows results and make a pipe chain command ie Test-Connection 192.168.(***).104 | if (Test-Connection -Quiet) | Save-vm -Name New; Stop-vm -Name New; Start-vm -Name New

      This command does not work but I want to see the test connection results

    • #51862

      Craig Redfern
      Participant

      Do you know a commanf=d that tests hung guest OS I think a test-connection coild be good even in a hung server

    • #51864

      Craig Redfern
      Participant

      Do you know a command that tests hung guest OS I think a test-connection coild be good even in a hung server?

You must be logged in to reply to this topic.