Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #Capture pass paramater
  2. param (
  3. [Parameter(Mandatory=$true)]
  4. [string]$csv,
  5. [Parameter(Mandatory=$true)]
  6. [string]$format,
  7. [switch]$dryrun
  8. )
  9.  
  10. # test virtual center connectivity
  11. Try {
  12. $target_clusters = get-cluster -EA 4
  13. write-output "########################################################################"
  14. write-output "Targeting the following connected clusters"
  15. write-output $target_clusters.Name
  16. write-output "########################################################################"
  17. } Catch {
  18. Write-output "Use the connect-viserver commmand to connect to a virtual center host before running this script."
  19. write-output $_.Exception.Message
  20. exit 1
  21. }
  22.  
  23. #make sure csv file exists
  24. if (!(test-path $csv )) {
  25. write-output "Input data file not found $csv"
  26. exit 1
  27. }
  28.  
  29. #Import the csv
  30. $source_data = Import-Csv $csv -Header @("vmname","datastore","status","error")
  31.  
  32. # allow debugging and validation of input data
  33. if ($dryrun) {
  34. foreach ($target in $source_data) {
  35. $vmname = $target.vmname
  36. $datastore = $target.datastore
  37. write-output "Move-VM -VM $vmname -Datastore $datastore -DiskStorageFormat $format"
  38. }
  39. exit 0
  40. }
  41.  
  42. # do the migration work serially
  43. foreach ($target in $source_data) {
  44. Try
  45. {
  46. $vmname = $target.vmname
  47. $datastore = $target.datastore
  48. if ($target.status -match "Done") {
  49. write-output "Skipping $vmname as it was already migrated..."
  50. $target.error = "Skipped $vmname as it was already migrated"
  51. }
  52. write-output "Migrating $vmname..."
  53. Move-VM -VM $vmname -Datastore $datastore -DiskStorageFormat $format -EA 4 | out-null
  54. write-output "$vmname completed..."
  55. $target.status = "Done"
  56. $source_data | Export-Csv $csv -NoTypeInformation
  57. (get-content $csv) | select -Skip 1 | set-content $csv
  58. }
  59. Catch
  60. {
  61. write-output "######################## ERROR #### $vmname ########################## `n $_.Exception.Message `n####################### ERROR #### $vmname ##########################"
  62. $target.error = $_.Exception.Message
  63. $target.status = "Error"
  64. $source_data | Export-Csv $csv -NoTypeInformation
  65. (get-content $csv) | select -Skip 1 | set-content $csv
  66. }
  67. }
  68.  
  69. write-output "################################### Run Completed!!! #####################################"
  70. write-output "Restults saved to $csv"
  71. write-output $source_data | format-table
  72.  
  73. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement