[英]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; 更新 -最初的问题声称我能够成功执行Invoke-Command,但不久之后无法执行; I thought it was due to processes going on during login after a windows upgrade.
我认为这是由于Windows升级后登录期间正在进行的过程所致。
It turns out the PC was actually starting, running a quick batch/cmd file, and then restarting. 事实证明,PC实际上正在启动,运行快速批处理/ cmd文件,然后重新启动。 This is what was leading to being able to do PS Remoting and then suddenly not.
这就是导致能够执行PS Remoting,然后突然无法执行的原因。 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. 出于好奇,计算机由于Microsoft Deployment Toolkit就地升级过程的残留而重新启动。 The way MDT completes its task-sequence post-upgrade is problematic for many reasons, and now I've got another to count.
由于多种原因,MDT完成升级后任务序列的方式是有问题的,现在我还需要数一数。
Old details (no longer relevant, with incorrect assumption that machine was not restarting after first successful Invoke-Command): 旧的详细信息(不再相关,错误地假设机器在首次成功执行Invoke-Command之后没有重新启动):
I'm automating various things with VMs in Hyper-V using powershell and powershell remoting. 我正在使用powershell和powershell远程处理功能在Hyper-V中使VM自动化。 I'll start up a VM and then want to run some commands on it via powershell.
我将启动一个VM,然后想通过Powershell在其上运行一些命令。
I'm struggling with determining when I can safely start running the remote commands via things like Invoke-Command. 我正在努力确定何时可以通过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
: 现在,我在两次调用之间睡眠一秒钟,以轮询虚拟机,直到以下函数返回
$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.
但是,在Windows升级之后,出现了问题。 I'll get Hyper-V remoting errors of various sorts even after
VMIsReady
returns $true
. 即使
VMIsReady
返回$true
我也会遇到各种Hyper-V远程处理错误。
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. 这些错误是在VM在升级后首次登录用户的过程中发生的(Windows正在经历“嗨;我们为您的PC进行了一些更新;这可能需要几分钟的时间-请勿关闭PC的电源)。”在此序列开始时会正确返回true-我想我可能应该等到序列完成后,但是我不知道如何知道何时开始。
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. 您可以使用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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.