Thursday 14 July 2016

Move VM files between datastore using PowerCLI

 

If you need to move VM folders from one datastore to another, you can do it easily using PowerCLI.

In this case, I had to move orphaned VMDK files and its folder from many different datastore to one temporary datastore. You can also use this script to Move ISO or any other files from one datastore to other datastore.

In below script specify your “destination-Datastore-name”

Create a csv file in c:\tmp\vmdk-folders.csv, with datastore and vmdkfolder headers. Under datastore column put the VM folder datastore name and in vmdkfolder column put VM folder path.

 

Connect-VIServer vcenter-server

$dstDatastore = Get-Datastore destination-Datastore-Name

New-PSDrive -PSProvider VimDatastore -Root "\"  -location $dstDatastore -Name dstDS

$vmdks = Import-Csv c:\temp\vmdk-folders.csv

foreach ( $vmdk in $vmdks ) { 

Write-Host Moving $vmdk.vmdkfolder from $vmdk.datastore....

$srcDatastore = Get-Datastore $vmdk.datastore

$vmdkfolder = $vmdk.vmdkFolder

New-PSDrive -PSProvider VimDatastore -Root "\"  -location $srcDatastore -Name srcDS

Move-Item srcDs:\$vmdkfolder -Destination dstDS:\

Remove-PSDrive -Name srcDS

}

 

 

If you want to just copy VM or VM files from one datastore to another datastore or download files to local computer then replace Move-Item command with Copy-Item or Copy-DatastoreItem command.

Also You can change source and destination as needed.

 

Thanks,

vPRH

1 comment: