powershell command?

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Craig Redfern Craig Redfern 1 month ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #51828
    Profile photo of Craig Redfern
    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
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    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
    Profile photo of Dave Wyatt
    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
    Profile photo of Craig Redfern
    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
    Profile photo of Craig Redfern
    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
    Profile photo of Craig Redfern
    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?

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.