简体   繁体   中英

Determining when machine is in good state for Powershell Remoting?

Update - the original question claimed that I was able to successfully perform an Invoke-Command and then shortly after was unable to; I thought it was due to processes going on during login after a windows upgrade.

It turns out the PC was actually starting, running a quick batch/cmd file, and then restarting. This is what was leading to being able to do PS Remoting and then suddenly not. The restart was quick enough after first boot that I didn't realize it was happening. Sorry for the bad question.

For the curious, the machine was restarting because of a remnant of the Microsoft Deployment Toolkit in-place upgrade process. The way MDT completes its task-sequence post-upgrade is problematic for many reasons, and now I've got another to count.

Old details (no longer relevant, with incorrect assumption that machine was not restarting after first successful Invoke-Command):

I'm automating various things with VMs in Hyper-V using powershell and powershell remoting. I'll start up a VM and then want to run some commands on it via powershell.

I'm struggling with determining when I can safely start running the remote commands via things like Invoke-Command. I can't start immediately as I need to let the machine start up.

Right now I poll the VM with a one second sleep between calls until the following function returns $true :

function VMIsReady {
[CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True)][object]$VM
    )
    $heartbeat = $vm.Heartbeat
    Write-Host "vm heartbeat is $heartbeat"
    if (($heartbeat -eq 'OkApplicationsHealthy') -or ($heartbeat -eq 'OkApplicationsUnknown'))
    {
        try
        {
            Invoke-Command -VMName $vm.Name -Credential $(GetVMCredentials) {$env:computername} | out-null
        }
        catch [System.Management.Automation.RuntimeException]
        {
            Write-Host 'Caught expected automation runtime exception'
            return $false
        }
        Write-Host 'remoting ready'
        return $true
    }
}

This usually works well; however, after a windows upgrade has happened, there are issues. I'll get Hyper-V remoting errors of various sorts even after VMIsReady returns $true .

These errors are happening while the VM is in the process of first user login after upgrade (Windows going through "Hi;We've got some updates for your PC;This might take several minutes-Don't turn off your PC). VMIsReady returns true right as this sequence starts - I imagine I probably should be waiting until the sequence is done, but I've no idea how to know when that is.

Is there a better way of determining when the machine is in a state where I can expect remoting to work without issue? Perhaps a way to tell when a user is fully logged on?

You can use Test-WSMan.

Of run a script on the invoke that will receive a response from the server.

[bool]$Response | Out-Null
try{
    $Response = Invoke-Command -ComputerName Test-Computer -ScriptBlock {return $true} 
}catch{
    return $false
}
if ($Response -ne $true){
    return $false
}else{
    return $true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM