Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. Start-Transcript
  2.  
  3. #import the servernames from csv file
  4. Import-Csv 'C:UsersAdministratorDesktopRestoreDetails Queue.csv' | foreach {
  5.  
  6. #check if vm already on required datastore, change the datastore name as per your environment
  7. if ((get-vm $_.host |Get-Datastore).name -ne "DataStore1" -or (get-vm $_.host |Get-Datastore).name -ne "DataStore2") {
  8.  
  9. Write-Host "`nStaring vMotion session for "$_.host
  10.  
  11. #Get current running tasks for Relocatin VM and check if the count is lower than 4
  12. #I am checking for currently running tasks and with Perecentage over 0%, sometimes there are tasks that are just waiting
  13. if ((Get-Task | ?{$_.name -eq "RelocateVM_Task" -and $_.state -eq "Running" -and $_.PercentComplete -ne "0"}).count -lt "4") {
  14.  
  15.  
  16. Write-Host "`nLess than 4 vMotions Migrations are going"
  17.  
  18. #if the count is lower than 4 then check which datastore has more freespace available
  19. if ((Get-Datastore -Name "DataStore1").freespacegb -ge (Get-Datastore -Name "DataStore2").freespacegb) {
  20.  
  21. Write-Host ("`nStarting " + $_.host + " to Datastore DataStore1")
  22. #send an email
  23. Send-MailMessage -to "username@domain.com" -from from@domain.com -Subject ("Starting " + $_.host + " to Datastore DataStore1") -SmtpServer smtp.domain.com -DeliveryNotificationOption OnFailure, delay
  24.  
  25. #start vMotion to DataStore1
  26. get-vm -Name $_.HOST | Move-VM -Datastore DataStore1 -RunAsync
  27.  
  28. }
  29. else {
  30.  
  31.  
  32. Write-Host ("`nStarting " + $_.host + " to DataStore2")
  33. Send-MailMessage -to "username@domain.com" -from from@domain.com -Subject ("Starting " + $_.host + " to Datastore DataStore1") -SmtpServer smtp.domain.com -DeliveryNotificationOption OnFailure, delay
  34.  
  35. #Start vMotion to Storage DataStore2
  36. get-vm -Name $_.HOST | Move-VM -Datastore DataStore2 -RunAsync
  37.  
  38. }
  39. }
  40. else {
  41.  
  42. #if more that 4 relocation tasks are running then wait for them to finish
  43. sleep 5
  44. Write-Host "`nWaiting for current vMotions to finish..."
  45. Write-Host "`nCurrent number of vMotions:"(Get-Task | ?{$_.name -eq "RelocateVM_Task" -and $_.state -eq "Running" -and $_.PercentComplete -ne "0"}).count
  46. do {
  47. #wait 60 seconds and recheck again
  48. sleep 60
  49. } while ((Get-Task | ?{$_.name -eq "RelocateVM_Task" -and $_.state -eq "Running"-and $_.PercentComplete -ne "0"}).count -ge "4")
  50.  
  51. #Repeate the above process when vMotion tasks go lower than 4
  52. if ((Get-Task | ?{$_.name -eq "RelocateVM_Task" -and $_.state -eq "Running" -and $_.PercentComplete -ne "0"}).count -lt "4") {
  53.  
  54. Write-Host "`nLess than 4 vMotions Migrations are going"
  55.  
  56. if ((Get-Datastore -Name "DataStore1").freespacegb -ge (Get-Datastore -Name "DataStore2").freespacegb) {
  57.  
  58. Write-Host ("`nStarting " + $_.host + " to Datastore1")
  59. Send-MailMessage -to "username@domain.com" -from from@domain.com -Subject ("Starting " + $_.host + " to Datastore DataStore1") -SmtpServer smtp.domain.com -DeliveryNotificationOption OnFailure, delay
  60.  
  61. get-vm -Name $_.HOST | Move-VM -Datastore DataStore1 -RunAsync
  62.  
  63. }
  64. else {
  65. Write-Host ("`nStarting " + $_.host + " to Datastore2")
  66. Send-MailMessage -to "username@domain.com" -from from@domain.com -Subject ("Starting " + $_.host + " to Datastore DataStore1") -SmtpServer smtp.domain.com -DeliveryNotificationOption OnFailure, delay
  67.  
  68. get-vm -Name $_.HOST | Move-VM -Datastore DataStore2 -RunAsync
  69.  
  70. }
  71. }
  72.  
  73. }
  74.  
  75. }
  76. }
  77.  
  78. #Check which VM's are on DR DataStore storage already
  79. Import-Csv 'C:UsersAdministratorDesktopRestoreDetails Queue.csv' | foreach {
  80.  
  81. if ((get-vm $_.host |Get-Datastore).name -ne "DataStore2" -or (get-vm $_.host |Get-Datastore).name -ne "DataStore1") {
  82. [array]$DRStorage =+ $_.host
  83. }
  84. else {
  85. [array]$NoDRStorage =+ $_.host
  86. }
  87. }
  88.  
  89.  
  90. Stop-Transcript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement