14 Replies Latest reply on Apr 4, 2020 5:19 PM by tdubb123

    svmotion script from csv file

    tdubb123 Master

      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

        • 1. Re: svmotion script from csv file
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

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

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: svmotion script from csv file
            tdubb123 Master

            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

            }

            • 3. Re: svmotion script from csv file
              tdubb123 Master

              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

              }

              • 4. Re: svmotion script from csv file
                LucD Guru
                Community WarriorsUser ModeratorsvExpert

                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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                • 5. Re: svmotion script from csv file
                  tdubb123 Master
                  Namesource_NetDst_NetDst_DSdst_vmhost
                  vm1VM Network 2VM NetworkDS1host1
                  vm2VM Network 2VM NetworkDS1host1
                  vm3VM Network 2VM NetworkDS1host1

                   

                   

                  this is my csv file

                  • 6. Re: svmotion script from csv file
                    LucD Guru
                    User ModeratorsCommunity WarriorsvExpert

                    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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                    • 7. Re: svmotion script from csv file
                      tdubb123 Master

                      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

                      • 8. Re: svmotion script from csv file
                        LucD Guru
                        Community WarriorsUser ModeratorsvExpert

                        That parameter obviously doesn't do OBN.

                        Change it to

                         

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

                        Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                        • 9. Re: svmotion script from csv file
                          tdubb123 Master

                          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

                          }

                          • 10. Re: svmotion script from csv file
                            LucD Guru
                            User ModeratorsvExpertCommunity Warriors

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

                            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                            • 11. Re: svmotion script from csv file
                              tdubb123 Master

                              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

                              • 12. Re: svmotion script from csv file
                                LucD Guru
                                vExpertUser ModeratorsCommunity Warriors

                                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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                • 13. Re: svmotion script from csv file
                                  daphnissov Guru
                                  Community WarriorsvExpert

                                  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.

                                  • 14. Re: svmotion script from csv file
                                    tdubb123 Master

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

                                     

                                    anyway appreciate the help

                                     

                                    Thanks