powershell command?

Welcome Forums General PowerShell Q&A powershell command?

Viewing 2 reply threads
  • Author
    Posts
    • #51828
      Participant
      Topics: 1
      Replies: 3
      Points: 0
      Rank: Member

      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
      Moderator
      Topics: 2
      Replies: 525
      Points: 24
      Team Member
      Rank: Member

      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
      }
      
    • #51836
      Member
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      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
        Participant
        Topics: 1
        Replies: 3
        Points: 0
        Rank: Member

        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
        Participant
        Topics: 1
        Replies: 3
        Points: 0
        Rank: Member

        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
        Participant
        Topics: 1
        Replies: 3
        Points: 0
        Rank: Member

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

Viewing 2 reply threads
  • The topic ‘powershell command?’ is closed to new replies.