Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Some friends here on the Hyper-V team shared a PowerShell 2.0 script for connecting your virtual switches to the management partition (parent partition):
# Connect a Virtual Switch to the management partition
param(
[string]$vsName = $(throw "Must specify virtual switch name"),
[string]$intEthName = [guid]::NewGuid().guid,
[string]$switchPortName = [guid]::NewGuid().guid
)$vSwitch = gwmi -namespace root\virtualization Msvm_VirtualSwitch -filter "ElementName = '$vsName'"
$vsms = gwmi -namespace root\virtualization Msvm_VirtualSwitchManagementService# Create a new internal ethernet port
$result = $vsms.CreateInternalEthernetPortDynamicMac($intEthName,"Internal Ethernet Port")
if($result.ReturnValue -ne 0){
return $result.ReturnValue
}
$intEthernet = [wmi]$result.CreatedInternalEthernetPort# Create a new switch port on the virtual switch
$result = $vsms.CreateSwitchPort($vSwitch, $switchPortName, "Switch Port to Internal Ethernet", "")
if($result.ReturnValue -ne 0){
return $result.ReturnValue
}
$switchPort = [wmi]$result.CreatedSwitchPort# Get the endpoint connected to the internal ethernet port and
# connect it to the switch port
$endpoint = gwmi -namespace root\virtualization `
-query "Associators of {$intEthernet} where ResultClass = Msvm_SwitchLANEndpoint"$result = $vsms.ConnectSwitchPort($switchPort, $endpoint)
if($result.ReturnValue -eq 4096){
# A Job was started, and can be tracked using its Msvm_Job instance
$job = [wmi]$result.Job
# Wait for job to finish
while($job.jobstate -lt 7){$job.get()}
# Return the Job's error code
return $job.ErrorCode
}
# Otherwise, the method completed
return $result.ReturnValue
For more info on how to use PS cmdlets see: https://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/index.mspx
See also James O’Neil’s New and improved PowerShell Library for Hyper-V. Now with more functions and... documentation!
For all 35 sample Hyper-V PS1 scripts in a zipfile, go to: Hyper-V PowerShell Example Scripts.zip-download