我正在使用PowerShell连接到Hyper-V虚拟机。但是,由于某些原因,当我尝试启动PSSession时,我收到错误消息“输入的VMID不能解析到单个虚拟机”。
代码如下所示:
$HyperVServerName = "TheServerName"
$VMName = "AValidVMName"
$SnapshotName = "ASnapshotName"
$creds = Get-Credential
Restore-VMSnapshot -ComputerName $HyperVServerName -VMName $VMName -Name $SnapshotName -Confirm:$false
Start-VM -ComputerName $HyperVServerName -Name $VMName
#Do some checks to make sure the VM is running...
$VMID = (Get-VM -ComputerName $HyperVServerName -Name $VMName).ID
If(-Not ($VMID -eq $null))
{
$psSession = New-PSSession -VMId $VMID -Credential $creds
If(-Not ($psSession -eq $null))
{
Enter-PSSession -Session $psSession
#Do some stuff here...
}
}
我在$psSession = New-PSSession -VMId $VMID -Credential $creds
行上得到了错误。
有关错误的详细信息:
你知道是什么导致了这个错误,以及我该如何纠正它吗?
我以前可以在VM上运行命令,但我尝试修改代码,以便在使用Invoke-Command
时不必提供VM的主机名。
发布于 2019-05-31 08:08:40
这里只有一些想法,并不是很确定你为什么要瞄准VMId,但这是一个选择。
我只是在这里动态调用东西来避免,嗯,你知道的.
$HyperVServerName = $env:COMPUTERNAME
$VMName = (Get-VM)[3].Name
$creds = Get-Credential -Credential "$env:USERDOMAIN\$env:USERNAME"
# I am using variable squeezing to assign and output info. In prod you'd remove those parens.
($VMID = (Get-VM -ComputerName $HyperVServerName -Name $VMName).ID)
<#
When it comes to PSRemotig, it's one or the other
- Implicit = New-PSSession - remote command stuff - Invoke-Command, etc...
- Explicit = Enter-PSSssion - user ineractive stuff
they cannot be use on the same target at the same time, it's redundant.
Also, the $VMID would never be not null, becasue you are directly populating it above.
#>
If(-Not ($VMID -eq $null))
{
<#
There is going to be an Implicit PSRemoting session when you do this
You can see this, if you output the info as you call it. Well, in testing.
I am using variable squeezing to assign and output info. In prod you'd remove those parens.
#>
($psSession = New-PSSession -VMId $VMID -Credential $creds)
<#
So this is moot. The session wll never be -Not null, becasue of the previous command,
Unless the session connection failed, then you should be catching that error anyway.
so, don't do this Explicit command, if you are directly setting an Implicit one.
Even in your code below, it's still moot, as you are trying to use that $pssession
which does not exist, via the ENter-PSSession.
So, if $pssession is null, the Enter-PSSEssion is also moot.
#>
<#
If(-Not ($psSession -eq $null))
{
Enter-PSSession -Session $psSession
#Do some stuff here...
}
#>
}
# So, you code could be something like.
$HyperVServerName = "TheServerName"
$VMName = "AValidVMName"
$SnapshotName = "ASnapshotName"
$creds = Get-Credential
Restore-VMSnapshot -ComputerName $HyperVServerName -VMName $VMName -Name $SnapshotName -Confirm:$false
Start-VM -ComputerName $HyperVServerName -Name $VMName
#Do some checks to make sure the VM is running...
($VMHost = Get-VM -ComputerName $HyperVServerName -Name $VMName)
Try
{
($psSession = New-PSSession -VMId $VMHost.Name -Credential $creds)
# Do some stuff here
}
Catch
{
Write-Warning -Message "Error calling the implicit remote session for $($VMHost.Name). Starting explicit session for $($VMHost.Name)."
$Error | Format-List -Force | Out-String | clip | notepad
Start-Sleep -Seconds 1
[void][reflection.assembly]::loadwithpartialname("system.windows.forms")
[system.windows.forms.sendkeys]::SendWait('^v')
Enter-PSSession -ComputerName $($VMHost.Name) -Credential $creds
#Do some stuff here...
}
发布于 2020-02-03 21:40:28
对于任何VM,只运行$psSession = New-PSSession -VMId $VMID -Credential $creds
或$psSession = New-PSSession -VMName $VMNAME -Credential $creds
时,我都会遇到相同的错误。以管理员身份启动powershell为我解决了这个错误。
https://stackoverflow.com/questions/56379923
复制相似问题