trying to move a bunch of vms from one host to another
csv file is
Name | DST_Net | DST_DS |
---|---|---|
vm1 | VM Network | DS1 |
vm2 | VM Network | DS1 |
vm3 | VM Network | DS1 |
vm4 | VM Network | DS1 |
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
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
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
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
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
}
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
}
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
Name | source_Net | Dst_Net | Dst_DS | dst_vmhost |
vm1 | VM Network 2 | VM Network | DS1 | host1 |
vm2 | VM Network 2 | VM Network | DS1 | host1 |
vm3 | VM Network 2 | VM Network | DS1 | host1 |
this is my csv file
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
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
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
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
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
}
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
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
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
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.
ok I had a space on the the VM in the Name column that caused it.
anyway appreciate the help
Thanks