VMware Cloud Community
xooops
Contributor
Contributor
Jump to solution

Move-VM Thin

Hello

Does any body use this script http://poshcode.org/1579 to move (svmotion) VM's to other datastores?

At the moment I can't start this script in vSphere PowerCLI.

thanks, Sven

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Sven,

do you use PowerShell V2? Because the parameter declaration in the function is a PowerShell V2 advanced function parameter declaration. I tried this parameter declaration in PowerShell V2 and it works good for me. If you use PowerShell V1 you use should change the beginning of the function to:

function Move-VMThin {
    PARAM($VM,$Datastore)

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

Haven't used this recently but it should work when you call it like this

function Move-VMThin {
...
}

Get-VM My | Move-VMThin -Datastore (Get-Datastore MyDatastore)

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
xooops
Contributor
Contributor
Jump to solution

Hello

i tried to store the code in a ps1 file and run the file. But I get

Parameter declarations are a comma-separated list of variable names with option

al initializer expressions.

At C:\Scripts\Move-VMThin.ps1:3 char:111

+ [Parameter(Mandatory=$true,ValueFromPipeline=$true,HelpMessage="Virt

ual Machine Objects to Migrate")] <<<<

I tired also

.\Move-VMThin.ps1 myVWName,MyNewDatastore.

but doesn't work.

regards, Sven

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Sven,

you should use:

.\Move-VMThin.ps1 -VM myVMName -Datastore MyNewDatastoreName

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
xooops
Contributor
Contributor
Jump to solution

Hello Robert,

this was my first idea... but I get

Parameter declarations are a comma-separated list of variable names with option

al initializer expressions.

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Move-VMThin.

ps1:3 char:111

+ [Parameter(Mandatory=$true,ValueFromPipeline=$true,HelpMessage="Virt

ual Machine Objects to Migrate")] <<<<

best regards, Sven

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Sven,

the script in the blog post is a function. If you saved it as a .ps1 file you should better rename it to something else to avoid confusion, e.g. function.ps1. Then you can run this function.ps1 file dot sourced. Like:

. ./function.ps1

After this, you can use the function in your shell like a cmdlet:

Move-VMThin -VM MyVMName -Datastore MyNewDatastoreName

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
xooops
Contributor
Contributor
Jump to solution

Hello Robert,

:_| no success, I get always the same error.

the file is called "func_Move-VMThin.ps1" is stored under C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts

I start the VMware vSphere PowerCLI Console, connect-viserver,

. ./ func_Move-VMThin.ps1

But still get this error.

thanks, Sven

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Sven,

do you use PowerShell V2? Because the parameter declaration in the function is a PowerShell V2 advanced function parameter declaration. I tried this parameter declaration in PowerShell V2 and it works good for me. If you use PowerShell V1 you use should change the beginning of the function to:

function Move-VMThin {
    PARAM($VM,$Datastore)

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
xooops
Contributor
Contributor
Jump to solution

Hello Robert,

My client was running power shell v1 that was the problem. After upgrading to v2 it works fine.

Many thanks, Sven

0 Kudos
CITITECHS
Contributor
Contributor
Jump to solution

Is it possible to modify Move-VMThin to RunAsync the same way that Move-VM works ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, in the Move-VMThin function change line 29 into

$vmView.RelocateVM_Task($spec, $null)

All the SDK methods that end in _Task will return a MoRef to a Task object and the next line will execute immediately.

If you want to wait for the the task to end, you do something like this

$taskMoRef = $vmView.RelocateVM_Task($spec, $null)

$task = Get-View $taskMoRef 
while ("running","queued" -contains $task.Info.State) {
  sleep 5
  $task.UpdateViewData("Info.State")
}
...

If you leave out that loop, the script will continue and the task will run async.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
CITITECHS
Contributor
Contributor
Jump to solution

LucD ,

Thanks that worked ! I modified the line suggested.

Another Question I like the fact that I can run more than one now. That was really annoying before. But any suggestions on how to cap it ?

I have over 300 Storage Motions so kicking off all 300 wouldn’t be ideal. So like maybe 5 at time. Or whatever I define the sweetspot to be.

Here is currently how am calling MoveVM-Thin

$csv_info = Import-Csv C:\labmotion.csv

foreach ($line in $csv_info) {

Move-VMthin -VM $line.name -Datastore "$($line.location)" }

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could use a hash table to store the tasks that are running.

In a loop you can test if there are 5 entries.

If there are, you sleep for some time and test again.

If there are less, you fire the next task.

I used a hash table for async tasks in About Async tasks, the Get-Task cmdlet and a hash table.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dpils
Contributor
Contributor
Jump to solution

It seems the script has a bug here:

$dsView = Get-View -ViewType Datastore -Filter @{"Name" = "$Datastore"}

This filter is greedy grep for Datastore name, so if you have datastores called  'datastore' and 'datastore0', then running against 'datastore', script will return result for both objects. Adding endline metacharacter $ is one way to fix it.

$dsView = Get-View -ViewType Datastore -Filter @{"Name" = "$Datastore+$"}

0 Kudos