Powershell 为什么会出现输入VMID无法解析为单个虚拟机的错误?

Powershell 为什么会出现输入VMID无法解析为单个虚拟机的错误?,powershell,Powershell,我正在使用PowerShell连接到Hyper-V虚拟机。但由于某些原因,当我尝试启动PSSession时,我得到了错误“输入VMID未解析为单个虚拟机” 代码如下所示: $HyperVServerName = "TheServerName" $VMName = "AValidVMName" $SnapshotName = "ASnapshotName" $creds = Get-Credential Restore-VMSnapshot -ComputerName $HyperVServer

我正在使用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
行中得到错误

有关错误的详细信息:

  • CategoryInfo:InvalidArgument(:)[New PSSession],ArgumentException
  • FullyQualifiedErrorId:InvalidVMIdNotSingle,Microsoft.PowerShell.Commands.NewPSSessionCommand
关于是什么导致了这个错误,以及我如何纠正它,有什么想法吗



我以前能够在VM上运行命令,但我尝试修改代码,以便在使用
Invoke命令时不必提供VM的主机名,这里只想了一些想法,不确定为什么要将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...
}
$HyperVServerName=$env:COMPUTERNAME
$VMName=(获取虚拟机)[3]。名称
$creds=获取凭据-凭据“$env:USERDOMAIN\$env:USERNAME”
#我使用变量压缩来分配和输出信息。在prod中,你会移除那些paren。
($VMID=(Get-VM-ComputerName$HyperVServerName-Name$VMName).ID)
如果(-Not($VMID-eq$null))
{

这里有一些想法,不确定为什么要将目标锁定在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...
}
$HyperVServerName=$env:COMPUTERNAME
$VMName=(获取虚拟机)[3]。名称
$creds=获取凭据-凭据“$env:USERDOMAIN\$env:USERNAME”
#我正在使用变量压缩来分配和输出信息。在prod中,您将删除这些参数。
($VMID=(Get-VM-ComputerName$HyperVServerName-Name$VMName).ID)
如果(-Not($VMID-eq$null))
{

对于任何虚拟机,仅运行
$psSession=New-psSession-VMId$VMId-Credential$creds
$psSession=New-psSession-VMName$VMName-Credential$creds
时我遇到了相同的错误。以管理员身份启动powershell为我解决了此错误。

我仅运行
$psSession=New-psSession-VMId时遇到了相同的错误$VMID-Credential$creds
$psSession=New psSession-VMName$VMName-Credential$creds
用于任何VM。以管理员身份启动powershell为我解决了此错误。

尝试使用
VMID
属性而不是
Id
属性。尝试使用
VMID
属性而不是
Id
属性y、 这解决了我的问题,谢谢!这解决了我的问题,谢谢!