2015-02-06 151 views
1

我想运行下面的PowerShell脚本,因为3天,并得到一个错误。错误,同时设置从主机虚拟机的IP地址

如果我设置$ NetworkSettings [0] .ProtocolIFType = 4096,那么$ job.jobstate的值为10(例外情况)。 如果我只是注释掉 - $ NetworkSettings [0] .ProtocolIFType = 4096,脚本执行成功,但虚拟机的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[]][email protected](), 

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

    [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" 
} 

}

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

回答

1

运行该脚本对我很好。

一些指点,我觉得可能会帮助你,

  • 检查网络适配器状态。
  • 确保集成服务已启动和更新。

我不确定SetGuestNetworkAdapterConfiguration是否适用于少数Linux客户。 IP注入在Ubuntu上不起作用,并且已修复。 https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1360580