2 Copyright 2014 Cloudbase Solutions Srl
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 $WMI_JOB_STATUS_STARTED = 4096
18 $WMI_JOB_STATE_RUNNING = 4
19 $WMI_JOB_STATE_COMPLETED = 7
21 $hvassembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.HyperV.PowerShell")
23 function Set-VMNetworkAdapterOVSPort
28 [parameter(Mandatory=$true, ValueFromPipeline=$true)]
29 [Microsoft.HyperV.PowerShell.VMNetworkAdapter]$VMNetworkAdapter,
31 [parameter(Mandatory=$true)]
32 [ValidateLength(1, 48)]
37 $ns = "root\virtualization\v2"
38 $EscapedId = $VMNetworkAdapter.Id.Replace('\', '\\')
40 $sd = gwmi -namespace $ns -class Msvm_EthernetPortAllocationSettingData -Filter "ElementName = '$OVSPortName'"
43 if($sd.InstanceId.Contains($VMNetworkAdapter.Id)){
44 throw "The OVS port name '$OVSPortName' is already assigned to this port."
46 throw "Cannot assign the OVS port name '$OVSPortName' as it is already assigned to an other port."
49 $sd = gwmi -namespace $ns -class Msvm_EthernetPortAllocationSettingData -Filter "InstanceId like '$EscapedId%'"
53 $sd.ElementName = $OVSPortName
55 $vsms = gwmi -namespace $ns -class Msvm_VirtualSystemManagementService
56 $retVal = $vsms.ModifyResourceSettings(@($sd.GetText(1)))
59 Check-WMIReturnValue $retVal
63 throw "Assigning OVS port '$OVSPortName' failed"
69 function Get-VMNetworkAdapterByOVSPort
75 [parameter(Mandatory=$true)]
76 [ValidateLength(1, 48)]
81 $ns = "root\virtualization\v2"
83 $sd = gwmi -namespace $ns -class Msvm_EthernetPortAllocationSettingData -Filter "ElementName = '$OVSPortName'"
91 function Get-VMByOVSPort
96 [parameter(Mandatory=$true)]
97 [ValidateLength(1, 48)]
102 $ns = "root\virtualization\v2"
104 $vms = gwmi -namespace $ns -class Msvm_VirtualSystemSettingData
107 $ports = gwmi -Namespace $ns -Query "
108 Associators of {$vm} Where
109 ResultClass = Msvm_EthernetPortAllocationSettingData"
110 if ($ports.ElementName -eq $OVSPortName){
117 function Check-WMIReturnValue($retVal)
119 if ($retVal.ReturnValue -ne 0)
121 if ($retVal.ReturnValue -eq $WMI_JOB_STATUS_STARTED)
125 $job = [wmi]$retVal.Job
127 while ($job.JobState -eq $WMI_JOB_STATE_RUNNING)
129 if ($job.JobState -ne $WMI_JOB_STATE_COMPLETED)
131 echo $job.ReturnValue
132 $errorString = "Job Failed. Job State: " + $job.JobState.ToString()
133 if ($job.__CLASS -eq "Msvm_ConcreteJob")
135 $errorString += " Error Code: " + $job.ErrorCode.ToString()
136 $errorString += " Error Details: " + $job.ErrorDescription
140 $error = $job.GetError()
143 $errorString += " Error:" + $error.Error
151 throw "Job Failed. Return Value: {0}" -f $job.ReturnValue
156 function Set-VMNetworkAdapterOVSPortDirect
161 [parameter(Mandatory=$true)]
162 [ValidateLength(1, 1024)]
165 [parameter(Mandatory=$true)]
166 [ValidateLength(1, 48)]
175 $vnic = Get-VMNetworkAdapter -VMName $vmName
177 # XXX the vnic index should be provided by the caller
178 $vnic[0] | Set-VMNetworkAdapterOVSPort -OVSPortName $OVSPortName