Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 从主机设置vm的ip地址时出错_Powershell - Fatal编程技术网

Powershell 从主机设置vm的ip地址时出错

Powershell 从主机设置vm的ip地址时出错,powershell,Powershell,我从3天开始尝试在powershell脚本下运行,但出现了一个错误 如果我设置$NetworkSettings[0].ProtocolIFType=4096,则$job.jobstate给出的值为10(例外情况)。 如果我只是注释掉-$NetworkSettings[0]。ProtocolIFType=4096,脚本将成功执行,但不会更改VM的IP地址 Function Set-VMNetworkConfiguration { [CmdletBinding()] Param ( [Pa

我从3天开始尝试在powershell脚本下运行,但出现了一个错误

如果我设置$NetworkSettings[0].ProtocolIFType=4096,则$job.jobstate给出的值为10(例外情况)。 如果我只是注释掉-$NetworkSettings[0]。ProtocolIFType=4096,脚本将成功执行,但不会更改VM的IP地址

 Function Set-VMNetworkConfiguration {
[CmdletBinding()]
Param (
    [Parameter(Mandatory=$true,
               Position=1,
               ParameterSetName='DHCP',
               ValueFromPipeline=$true)]
    [Parameter(Mandatory=$true,
               Position=0,
               ParameterSetName='Static',
               ValueFromPipeline=$true)]
    [Microsoft.HyperV.PowerShell.VMNetworkAdapter]$NetworkAdapter,

    [Parameter(Mandatory=$true,
               Position=1,
               ParameterSetName='Static')]
    [String[]]$IPAddress=@(),

    [Parameter(Mandatory=$false,
               Position=2,
               ParameterSetName='Static')]
    [String[]]$Subnet=@(),

    [Parameter(Mandatory=$false,
               Position=3,
               ParameterSetName='Static')]
    [String[]]$DefaultGateway = @(),

    [Parameter(Mandatory=$false,
               Position=4,
               ParameterSetName='Static')]
    [String[]]$DNSServer = @(),

    [Parameter(Mandatory=$false,
               Position=0,
               ParameterSetName='DHCP')]
    [Switch]$Dhcp
)

$VM = Get-WmiObject -Namespace 'root\virtualization\v2' -Class 'Msvm_ComputerSystem' | Where-Object { $_.ElementName -eq $NetworkAdapter.VMName } 
$VMSettings = $vm.GetRelated('Msvm_VirtualSystemSettingData') | Where-Object { $_.VirtualSystemType -eq 'Microsoft:Hyper-V:System:Realized' }    

$VMNetAdapters = $VMSettings.GetRelated('Msvm_SyntheticEthernetPortSettingData') 

$NetworkSettings = @()
foreach ($NetAdapter in $VMNetAdapters) {
    if ($NetAdapter.Address -eq $NetworkAdapter.MacAddress) {
        $NetworkSettings = $NetworkSettings + $NetAdapter.GetRelated("Msvm_GuestNetworkAdapterConfiguration")
    }
}

$NetworkSettings[0].IPAddresses = $IPAddress
$NetworkSettings[0].Subnets = $Subnet
$NetworkSettings[0].DefaultGateways = $DefaultGateway
$NetworkSettings[0].DNSServers = $DNSServer
$NetworkSettings[0].ProtocolIFType = 4096

if ($dhcp) {
    $NetworkSettings[0].DHCPEnabled = $true
} else {
    $NetworkSettings[0].DHCPEnabled = $false
}

$Service = Get-WmiObject -Class "Msvm_VirtualSystemManagementService" -Namespace "root\virtualization\v2"
$setIP = $Service.SetGuestNetworkAdapterConfiguration($VM, $NetworkSettings[0].GetText(1))

if ($setip.ReturnValue -eq 4096) {
    $job=[WMI]$setip.job 

    while ($job.JobState -eq 3 -or $job.JobState -eq 4) {
        start-sleep 1
        $job=[WMI]$setip.job
    }
    write-host "**jobstate = $job.JobState "
    if ($job.JobState -eq 7) {
        write-host "Success"
    }
    else {
        $job.GetError()
    }
} elseif($setip.ReturnValue -eq 0) {
    Write-Host "Success"
}
}


剧本对我来说很好

我觉得有几点建议可能对你有帮助

  • 检查网络适配器状态
  • 确保已启动并更新集成服务
我不确定
SetGuestNetworkAdapterConfiguration
是否适用于一些Linux客户。IP注入在Ubuntu上不起作用,已修复。

Get-VMNetworkAdapter -VMName uebase | Set-VMNetworkConfiguration -IPAddress 10.101.5.198 -Subnet 255.255.255.0 -DefaultGateway 10.101.5.1