2015-02-06 180 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