Created
May 30, 2018 18:40
-
-
Save dapacruz/284eed97843a1927c32486f7739fba4d to your computer and use it in GitHub Desktop.
Migrate Virtual Machines (VM) to a Virtual Distribututed Switch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $vmhost = 'esxi-01.domain.local' | |
| $virtual_machines = Get-VM -Location $vmhost | |
| $vcenter = 'vcenter.domain.local' | |
| $vcenter_user = 'administrator@vsphere.local' | |
| # Save an encrypted password to a file and retrieve it later for use in a script | |
| # Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File password.txt | |
| $vcenter_password = Get-Content 'Distributed Switch Migration\password.txt' | ConvertTo-SecureString | |
| if(-not $global:DefaultVIServers) { | |
| $creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $vcenter_user, $vcenter_password | |
| Connect-VIServer $vcenter -Credential $creds | |
| } | |
| Start-Transcript -Path 'distributed_switch_migration.txt' | |
| foreach ($vm in $virtual_machines) { | |
| $virtual_nics = Get-NetworkAdapter -VM $vm | |
| foreach ($vnic in $virtual_nics){ | |
| Write-Host "Migrating virtual machine '$($vm.Name)' network adapter '$($vnic.Name)' ... " -NoNewline | |
| $dist_pg = Get-VDPortgroup -name $vnic.NetworkName -ErrorAction SilentlyContinue | |
| if ($dist_pg){ | |
| $result = Set-NetworkAdapter -NetworkAdapter $vnic -PortGroup $dist_pg -Confirm:$false | |
| if ($result) { | |
| Write-Host "success" | |
| } else { | |
| Write-Host "fail" | |
| } | |
| } else { | |
| Write-Host "fail" | |
| Write-Host "`nDistributed port group '$($vnic.NetworkName)' does not exist ($($vm.Name))`n" -ForegroundColor 'Red' | |
| } | |
| } | |
| } | |
| Stop-Transcript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment