Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Capture pass paramater
- param (
- [Parameter(Mandatory=$true)]
- [string]$csv,
- [Parameter(Mandatory=$true)]
- [string]$format,
- [switch]$dryrun
- )
- # test virtual center connectivity
- Try {
- $target_clusters = get-cluster -EA 4
- write-output "########################################################################"
- write-output "Targeting the following connected clusters"
- write-output $target_clusters.Name
- write-output "########################################################################"
- } Catch {
- Write-output "Use the connect-viserver commmand to connect to a virtual center host before running this script."
- write-output $_.Exception.Message
- exit 1
- }
- #make sure csv file exists
- if (!(test-path $csv )) {
- write-output "Input data file not found $csv"
- exit 1
- }
- #Import the csv
- $source_data = Import-Csv $csv -Header @("vmname","datastore","status","error")
- # allow debugging and validation of input data
- if ($dryrun) {
- foreach ($target in $source_data) {
- $vmname = $target.vmname
- $datastore = $target.datastore
- write-output "Move-VM -VM $vmname -Datastore $datastore -DiskStorageFormat $format"
- }
- exit 0
- }
- # do the migration work serially
- foreach ($target in $source_data) {
- Try
- {
- $vmname = $target.vmname
- $datastore = $target.datastore
- if ($target.status -match "Done") {
- write-output "Skipping $vmname as it was already migrated..."
- $target.error = "Skipped $vmname as it was already migrated"
- }
- write-output "Migrating $vmname..."
- Move-VM -VM $vmname -Datastore $datastore -DiskStorageFormat $format -EA 4 | out-null
- write-output "$vmname completed..."
- $target.status = "Done"
- $source_data | Export-Csv $csv -NoTypeInformation
- (get-content $csv) | select -Skip 1 | set-content $csv
- }
- Catch
- {
- write-output "######################## ERROR #### $vmname ########################## `n $_.Exception.Message `n####################### ERROR #### $vmname ##########################"
- $target.error = $_.Exception.Message
- $target.status = "Error"
- $source_data | Export-Csv $csv -NoTypeInformation
- (get-content $csv) | select -Skip 1 | set-content $csv
- }
- }
- write-output "################################### Run Completed!!! #####################################"
- write-output "Restults saved to $csv"
- write-output $source_data | format-table
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement