2 Copyright 2014, 2015 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))
45 throw "The OVS port name '$OVSPortName' is already assigned to this port."
47 throw "Cannot assign the OVS port name '$OVSPortName' as it is already assigned to an other port."
50 $sd = gwmi -namespace $ns -class Msvm_EthernetPortAllocationSettingData -Filter "InstanceId like '$EscapedId%'"
54 $sd.ElementName = $OVSPortName
56 $vsms = gwmi -namespace $ns -class Msvm_VirtualSystemManagementService
57 $retVal = $vsms.ModifyResourceSettings(@($sd.GetText(1)))
60 CheckWMIReturnValue $retVal
64 throw "Assigning OVS port '$OVSPortName' failed"
70 function Get-VMNetworkAdapterByOVSPort
76 [parameter(Mandatory=$true)]
77 [ValidateLength(1, 48)]
82 $ns = "root\virtualization\v2"
84 $sd = gwmi -namespace $ns -class Msvm_EthernetPortAllocationSettingData -Filter "ElementName = '$OVSPortName'"
92 function Get-VMByOVSPort
97 [parameter(Mandatory=$true)]
98 [ValidateLength(1, 48)]
103 $ns = "root\virtualization\v2"
105 $vms = gwmi -namespace $ns -class Msvm_VirtualSystemSettingData
108 $ports = gwmi -Namespace $ns -Query "
109 Associators of {$vm} Where
110 ResultClass = Msvm_EthernetPortAllocationSettingData"
111 if ($ports.ElementName -eq $OVSPortName)
119 #This function returns the Msvm_VirtualSystemSettingData given a VMName
120 function Get-VMNetworkAdapterWithOVSPort
125 [parameter(Mandatory=$true)]
126 [ValidateLength(1, 1024)]
131 $ns = "root\virtualization\v2"
135 $vm = gwmi -namespace $ns -class Msvm_VirtualSystemSettingData -Filter "ElementName = '$VMName'"
137 $ports = gwmi -Namespace $ns -Query "
138 Associators of {$vm} Where
139 ResultClass = Msvm_EthernetPortAllocationSettingData"
145 function CheckWMIReturnValue($retVal)
147 if ($retVal.ReturnValue -ne 0)
149 if ($retVal.ReturnValue -eq $WMI_JOB_STATUS_STARTED)
153 $job = [wmi]$retVal.Job
155 while ($job.JobState -eq $WMI_JOB_STATE_RUNNING)
157 if ($job.JobState -ne $WMI_JOB_STATE_COMPLETED)
159 echo $job.ReturnValue
160 $errorString = "Job Failed. Job State: " + $job.JobState.ToString()
161 if ($job.__CLASS -eq "Msvm_ConcreteJob")
163 $errorString += " Error Code: " + $job.ErrorCode.ToString()
164 $errorString += " Error Details: " + $job.ErrorDescription
168 $error = $job.GetError()
171 $errorString += " Error:" + $error.Error
179 throw "Job Failed. Return Value: {0}" -f $job.ReturnValue
184 function Set-VMNetworkAdapterOVSPortDirect
189 [parameter(Mandatory=$true)]
190 [ValidateLength(1, 1024)]
193 [parameter(Mandatory=$true)]
194 [ValidateLength(1, 48)]
203 $vnic = Get-VMNetworkAdapter -VMName $vmName
205 # XXX the vnic index should be provided by the caller
206 $vnic[0] | Set-VMNetworkAdapterOVSPort -OVSPortName $OVSPortName
210 Export-ModuleMember -function Set-*, Get-*