Guest User

Untitled

a guest
May 4th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. # Save an encrypted password to a file and retrieve it later for use in a script
  2. # Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File password.txt
  3.  
  4. if(-not $global:DefaultVIServers) {
  5. $user = 'ncoe\dsatech'
  6. $password = Get-Content 'Distributed Switch Migration\password.txt' | ConvertTo-SecureString
  7. $creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $password
  8.  
  9. Connect-VIServer ncoe-vcenter-01.ncoe.k12.ca.us -Credential $creds
  10. }
  11.  
  12. Start-Transcript -Path 'Distributed Switch Migration\dis_switch_mig.txt'
  13.  
  14. $virtual_machines = Get-VM -Location ncoe-vm10*
  15.  
  16. foreach ($vm in $virtual_machines) {
  17. $virtual_nics = Get-NetworkAdapter -VM $vm
  18. foreach ($vnic in $virtual_nics){
  19. Write-Host "Migrating virtual machine '$($vm.Name)' network adapter '$($vnic.Name)' ... " -NoNewline
  20. $dist_pg = Get-VDPortgroup -name $vnic.NetworkName -ErrorAction SilentlyContinue
  21. if ($dist_pg){
  22. $result = Set-NetworkAdapter -NetworkAdapter $vnic -PortGroup $dist_pg -Confirm:$false
  23. if ($result) {
  24. Write-Host "success"
  25. } else {
  26. Write-Host "fail"
  27. }
  28. } else {
  29. Write-Host "fail"
  30. Write-Host "`nDistributed port group '$($vnic.NetworkName)' does not exist ($($vm.Name))`n" -ForegroundColor 'Red'
  31. }
  32. }
  33.  
  34. }
  35.  
  36. Stop-Transcript
Add Comment
Please, Sign In to add comment