VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

svmotion script from csv file

trying to move a bunch of vms from one host to another

csv file is

NameDST_Net
DST_DS
vm1VM NetworkDS1
vm2VM NetworkDS1
vm3VM NetworkDS1
vm4VM NetworkDS1

but below script not working

import-csv -Path "C:\scripts\svmotion_vms.csv" -UseCulture

{

write-progress -Activity svmotion  -Status "Moving $_.name"

get-vm -name $_.name | move-vm -DiskStorageFormat $_.format -VMotionPriority High -Datastore $_.DST_DS -RunAsync -PortGroup $_.Dst_Net -Confirm:$false

}

any idea

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The problem is that the pipeline variable $_ on the Move-VM is what comes out of the Get-VM cmdlet.

Not the Import-Csv.

Try like this

Import-Csv -Path "C:\scripts\svmotion_vms.csv" -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    Write-Progress -Activity svmotion  -Status "Moving $row.name"

    Get-VM -name $row.name |

    Move-VM -Destination (Get-VMHost -Name $row.dst_vmhost) -DiskStorageFormat Thin -Datastore $row.dst_DS -VMotionPriority High -PortGroup $row.Dst_Net

}


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

View solution in original post

Reply
0 Kudos
14 Replies
LucD
Leadership
Leadership
Jump to solution

Is there a Format column in that CSV (since you use it on the DiskStorageFormat parameter)?
Are you getting any error messages?


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

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

yes actually they should all be thin. I tried this and its not working. No errors

import-csv -Path "C:\scripts\svmotion_vms.csv" -UseCulture

{

write-progress -Activity svmotion  -Status "Moving $_.name"

get-vm -name $_.name | move-vm -DiskStorageFormat $_.format -VMotionPriority High -Datastore $_.dst_DS -RunAsync -PortGroup $_.Dst_Net -Confirm:$false

}

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

sorry this is what I have tried

import-csv -Path "C:\scripts\svmotion_vms.csv" -UseCulture

{

write-progress -Activity svmotion  -Status "Moving $_.name"

get-vm -name $_.name | move-vm -Destination (get-vmhost -Name $_.dst_vmhost) -DiskStorageFormat Thin -Datastore $_.dst_DS -VMotionPriority High -PortGroup $_.Dst_Net

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I understand, but now dst_vmhost doesn't seem to be present in your CSV.
Or is that earlier content of your CSV not complete?


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

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

Namesource_NetDst_NetDst_DSdst_vmhost
vm1VM Network 2VM NetworkDS1host1
vm2VM Network 2VM NetworkDS1host1
vm3VM Network 2VM NetworkDS1host1

this is my csv file

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The problem is that the pipeline variable $_ on the Move-VM is what comes out of the Get-VM cmdlet.

Not the Import-Csv.

Try like this

Import-Csv -Path "C:\scripts\svmotion_vms.csv" -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    Write-Progress -Activity svmotion  -Status "Moving $row.name"

    Get-VM -name $row.name |

    Move-VM -Destination (Get-VMHost -Name $row.dst_vmhost) -DiskStorageFormat Thin -Datastore $row.dst_DS -VMotionPriority High -PortGroup $row.Dst_Net

}


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

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

now i m seeing this

Move-VM : Cannot bind parameter 'PortGroup'. Cannot convert the "VM Network" value of type

"System.String" to type

"VMware.VimAutomation.ViCore.Types.V1.Host.Networking.VirtualPortGroupBase".

At C:\scripts\svmotion_vm_script.ps1:8 char:141

+ ...  -Datastore $row.Dst_DS -VMotionPriority High -PortGroup $row.Dst_Net

+                                                              ~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Move-VM], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlet

   s.Commands.MoveVM

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That parameter obviously doesn't do OBN.

Change it to

-PortGroup (Get-VirtualPortGroup -Name $row.Dst_Net)


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

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

I am not getting the VM from this

$input = Import-Csv -Path "C:\scripts\svmotion_vms.csv" -PipelineVariable row

Foreach ($row in $input){

$vm = Get-VM -Name $row.Name

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Why did you change my code into this?
Did you use the Get-VirtualGroup as I showed?


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

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

i did. but its not able to get the vm name

this is the script

Import-Csv -Path "C:\scripts\svmotion_vms.csv" -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    Write-Progress -Activity svmotion  -Status "Moving $row.Name"

    Get-VM -name $row.Name | Move-VM -Destination (Get-VMHost -Name $row.dst_vmhost) -DiskStorageFormat Thin -Datastore $row.dst_DS -VMotionPriority High -PortGroup (get-vmhost -name $row.dst_vmhost | Get-VirtualPortGroup -Name $row.Dst_Net)

}

Get-VM : 4/4/2020 4:01:52 PM Get-VM VM with name 'SMSVW-DC ' was not found using the specified

filter(s).

At C:\scripts\svmotion_vm_script.ps1:6 char:5

+     Get-VM -name $row.Name |

+     ~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.C

   mdlets.Commands.GetVM

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

But that just means you have a VM in that CSV that is not found.
Nothing to do with the script.

Btw, I have been looking at your previous posts in this community, you hardly ever bother to answer or confirm that a reply was correct.

I think I will stop responding to your threads.
Perhaps you should have a look at HOWTO: Ask (and Answer) Questions


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

Reply
0 Kudos
daphnissov
Immortal
Immortal
Jump to solution

He's been told numerous times about forum posting etiquette and refuses to cooperate. Failing to mark your answers as correct or even helpful is only one example.

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

ok I had a space on the the VM in the Name column that caused it.

anyway appreciate the help

Thanks

Reply
0 Kudos