.SYNOPSIS
<A brief description of the script>
.DESCRIPTION
<A detailed description of the script>
.PARAMETER viserver
Virtual Infrastructure Server name
.PARAMETER vmguest
Virtual Machine Name
.EXAMPLE
Migrate-myvmnetwork -viserver vcenter -vmguest myvmguest
#>
#constants
#$NetworkKey = Name,Value
#OldnetworkName NewNetworkName
#$NetworkKey.Name $NetworkKey.Value
function Migrate-MyVMNetwork {
param(
[Parameter(Position=0, Mandatory=$true)][System.String]$viserver,
[Parameter(Position=1, Mandatory=$true)][System.String]$vmguest
)
$NetworkKey=@{
"oldnetworkname"="newNetworkName"
}
try{connect-viserver $viserver -ea stop|Out-Null}
catch { write-host $viserver "failed to connect";break }
$vm=get-vm $vmguest
$VMNetworkAdapters=$vm|Get-NetworkAdapter
foreach($VMNetworkAdapter in $VMNetworkAdapters){
if ($networkKey.containskey($VMNetworkAdapter.networkname)){
$NIC=$VMNetworkAdapter.Name
$oldNetworkName=$VMNetworkAdapter.networkname
$NewNetworkName=$networkKey.Get_Item($VMNetworkAdapter.networkname)
$PortGroup=Get-VDPortgroup -Name $NewNetworkName
Write-Host "VM Name:" $vm.Name
Write-Host "old network:" $oldNetworkName -ForegroundColor red
Write-Host "New Network:" $NewNetworkName -ForegroundColor Green
Write-Host "Port Group Name:" $PortGroup
#you can out-null this
$vm|Get-NetworkAdapter|?{$_.name -eq $nic}|Set-NetworkAdapter -PortGroup $PortGroup|ft -AutoSize
}
Elseif ($networkKey.containsvalue($VMNetworkAdapter.networkname)) { Write-Host $vm.Name ":" $vmnetworkadapter " Already Migrated." -ForegroundColor yellow }
else {Write-Host $vm.name ":" $vmnetworkadapter ":" $oldNetworkName ": Old name not in key pair." -ForegroundColor RED }
#paranoid
$NIC=""
$oldNetworkName=""
$NewNetworkName=""
$PortGroup=""
}
Disconnect-VIServer $viserver -confirm:$false
#paranoid
$VMNetworkAdapters=$null
$VM=$null
}
No comments:
Post a Comment