Guest User

PowerShell_Move-VM

a guest
Apr 10th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import-module activedirectory
  2.  
  3. Function Get-FileName($initialDirectory)
  4. {  
  5.  [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
  6.  Out-Null
  7.  
  8.  $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
  9.  $OpenFileDialog.initialDirectory = $initialDirectory
  10.  $OpenFileDialog.filter = "All files (*.*)| *.*"
  11.  $OpenFileDialog.ShowDialog() | Out-Null
  12.  $OpenFileDialog.filename
  13. } #end function Get-FileName
  14.  
  15. function PromptOldHost()
  16. {
  17.     do{$error = 0
  18.         write-host -NoNewline -ForegroundColor Red "Please Specify the Old Host: "
  19.         $oldhost = Read-Host  
  20.         try
  21.         {
  22.             $info1 = Get-ADComputer $oldhost
  23.         }
  24.         catch
  25.         {
  26.             write-host "Could not find host. Please Try again"
  27.             $error = 1
  28.         }
  29.     }while($error -eq 1)
  30.     return $oldhost
  31. }
  32.  
  33. function PromptNewHost()
  34. {
  35.     do{$error = 0
  36.         write-host -NoNewline -ForegroundColor Red "Please Specify the New Host: "
  37.         $newhost = Read-Host  
  38.         try
  39.         {
  40.             $info2 = Get-ADComputer $newhost
  41.         }
  42.         catch
  43.         {
  44.             write-host "Could not find host. Please Try again"
  45.             $error = 1  
  46.         }
  47.     }while($error -eq 1)
  48.     return $newhost
  49. }
  50.  
  51. # *** Entry Point to Script ***
  52.  
  53. write-host -ForegroundColor Red "Please select the File that contains a list of VMs you would like to migrate"
  54. $vmListFile = Get-FileName -initialDirectory "%HOMEDIR%"
  55. $vmList = Get-Content $vmListFile
  56.  
  57. $oldhost = PromptOldHost
  58. $newhost = PromptNewHost
  59.  
  60.  
  61. Invoke-Command -ComputerName $oldhost -ScriptBlock {
  62.     param($vl,$nhost)
  63.     Foreach($machine in $vl)
  64.     {
  65.         Move-VM -Name $machine -DestinationHost $nhost -IncludeStorage
  66.     }
  67. } -ArgumentList $mvList,$newhost
Advertisement
Add Comment
Please, Sign In to add comment