Automation

 View Only
Expand all | Collapse all

Script for Move vm from one folder to other folder

Natarajvd

NatarajvdMay 22, 2018 07:19 AM

Natarajvd

NatarajvdMay 22, 2018 09:09 AM

Natarajvd

NatarajvdMay 22, 2018 05:08 PM

  • 1.  Script for Move vm from one folder to other folder

    Posted May 19, 2018 12:21 PM

    I am looking Script for moving VM from one folder to other folder even Duplicated folder name exist on VC .The below script works fine in Unique folder structure.

    $VMfolder = @()

    $VMfolder = import-csv "C:\Users\Nataraj\Desktop\04-$($datacenter)-vms-with-FolderPath.csv" | Sort-Object -Property Path

    foreach($guest in $VMfolder){

         $key = @()

         $key =  Split-Path $guest.Path | split-path -leaf

         if ($key -eq $datacenter) {

              Write-Host "Root folder $guest.path"

              ##

              Move-VM (Get-VM $guest.Name) -Destination (Get-folder $key)

              }

              else

              {

              Move-VM (Get-VM $guest.Name) -Destination "VM"

              }

    }



  • 2.  RE: Script for Move vm from one folder to other folder

    Posted May 19, 2018 02:52 PM

    You didn't mention how the folder path is stored in that CSV file.

    If it is the full path starting from the Datacenter, you could use my Folder By Path function.



  • 3.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 04:56 AM

    Thank you So much for your reply

    below is the folder path i am using

    DC1\prd\test
    DC1\DRS\UAT
    Datacenter\ParentFolder\Subfolder

    My requirement is like this :I have list of VMs and folder path and it is saved in csv file

    Script should read the input csv file and it should move all mentioned vm to respective folders

    I am new to Scripting, i am great-full to you if you can describe or construct a script for me  how i can use your Folder By Path function to my requirement.



  • 4.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 05:00 AM

    Copy and paste my function into a .ps1 file.

    Add your code after the function in that same .ps1 file.

    Now call the script from the PowerShell/PowerCLI prompt.

    Don't forget to do a Connect-VIServer first.



  • 5.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 06:36 AM

    function Get-FolderByPath{

    <# .SYNOPSIS Retrieve folders by giving a path .DESCRIPTION The function will retrieve a folder by it's path. The path can contain any type of leave (folder or datacenter).

    .NOTES Author: Luc Dekens .PARAMETER Path The path to the folder. This is a required parameter. .PARAMETER Path The path to the folder. This is a required parameter.

    .PARAMETER Separator The character that is used to separate the leaves in the path. The default is '/' .EXAMPLE PS> Get-FolderByPath -Path "Folder1/Datacenter/Folder2"

    .EXAMPLE

    PS> Get-FolderByPath -Path "Folder1>Folder2" -Separator '>'

    #>

    param(

    [CmdletBinding()]

    [parameter(Mandatory = $true)]

    [System.String[]]${Path},

    [char]${Separator} = '/'

    )

    process{

    if((Get-PowerCLIConfiguration).DefaultVIServerMode -eq "Multiple"){

    $vcs = $defaultVIServers

    }

    else{

    $vcs = $defaultVIServers[0]

    }

    foreach($vc in $vcs){

    foreach($strPath in $Path){

    $root = Get-Folder -Name Datacenters -Server $vc

    $strPath.Split($Separator) | %{

    $root = Get-Inventory -Name $_ -Location $root -Server $vc -NoRecursion

    if((Get-Inventory -Location $root -NoRecursion | Select -ExpandProperty Name) -contains "vm"){

    $root = Get-Inventory -Name "vm" -Location $root -Server $vc -NoRecursion

    }

    }

    $root | where {$_ -is [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl]}|%{

    Get-Folder -Name $_.Name -Location $root.Parent -NoRecursion -Server $vc

    }

    }

    }

    }

    }

    # move the vm's to correct location

    $VMfolder = @()

    $VMfolder = import-csv "f:\vms-with-FolderPath.csv" | Sort-Object -Property Path

    foreach($guest in $VMfolder){

         $key = @()

         $key =  Split-Path $guest.Path | split-path -leaf

         if ($key -eq $datacenter) {

              Write-Host "Root folder $guest.path"

                      Move-VM (Get-VM $guest.Name) -Destination (Get-folder $key)

              }

              else

              {

                  Move-VM (Get-VM $guest.Name) -Destination "VM"

              }

    }

    ------------------------------------------------------------------------------------------------------

    I have modified the Script as per your instruction. after ran the script i got the output as attached and task is triggered in VC but none of the vm is moved to destination folder

    CSV file as below:

     

    Name Path
    SRV2012-R2-1 DC1\prd
    srv2008-r2-1 DC1\pordvm\prd
    srv2016-gui-1 DC1\VMS\test-VM

     



  • 6.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 07:09 AM

    You have to adapt your code a bit.

    Try the attached version.



  • 7.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 07:19 AM

    I got below Error



  • 8.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 08:35 AM

    Looks like something goes wrong with your Import-Csv.

    Do you have blank lines in your CSV file?

    What does this return?

    Import-Csv "f:\vms-with-FolderPath.csv" -Delimiter "`t" | Sort-Object -Property Path



  • 9.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 09:09 AM

    Below is the Output



  • 10.  RE: Script for Move vm from one folder to other folder
    Best Answer

    Posted May 22, 2018 09:14 AM

    Ok, you changed your CSV file content from what you originally showed (Tab-delimited fields).

    This should return the properties correctly. Can you give it a try?

    Import-Csv "f:\vms-with-FolderPath.csv" | Sort-Object -Property Path



  • 11.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 09:29 AM

    Yes you are right this is working like charm now. I will test this in all aspects, if i come across any issues i will contact you

    Thank you so much for your help, I appreciate your Skill and Patience



  • 12.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 03:56 PM

    I have additional requirement please help me

    After move the VMS I need a output report for verification purpose in form of xl or CSV . report should have VMs name and complete  folder path



  • 13.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 04:17 PM

    Will that not produce a CSV with the same content as your input CSV?

    In any case, try like this (I leave out the Get-FolderByPath function)

    # move the vm's to correct location

    $VMfolder = @()

    $report = @()

    $VMfolder = Import-Csv "f:\vms-with-FolderPath.csv" -Delimiter "`t" | Sort-Object -Property Path

    foreach($guest in $VMfolder){

       $destFolder = Get-FolderByPath -Path $guest.Path -Separator '\'

      Move-VM (Get-VM $guest.Name) -Destination $destFolder

       $report += New-Object PSOBject -Property @{

      VM = $guest.Name

      Folder = $guest.Path

      }

    }

    $report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

     



  • 14.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 04:43 PM

    I got this below Error. I have newly added this parameter -runasync

    I got report only for one server

      

    FolderVM
    DC1\vumupdatesrv2016-gui-1

    # move the vm's to correct location#

    $VMfolder = @()

    $VMfolder = Import-Csv "f:\vms-with-FolderPath.csv" | Sort-Object -Property Path

    foreach($guest in $VMfolder)

    {

        $destFolder = Get-FolderByPath -Path $guest.Path -Separator '\'

        Move-VM (Get-VM $guest.Name) -Destination $destFolder  -runasync

      $report += New-Object PSOBject -Property @{

      VM = $guest.Name

      Folder = $guest.Path

    }

    }

    $report | Export-Csv -Path "f:\report.csv" -NoTypeInformation -UseCulture



  • 15.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 04:54 PM

    Typo, I forgot the equal sign in the $report = @() line (it's corrected above).



  • 16.  RE: Script for Move vm from one folder to other folder

    Posted May 22, 2018 05:08 PM

    Grate it works Now. Thank you



  • 17.  RE: Script for Move vm from one folder to other folder

    Posted Jan 22, 2019 11:03 AM

    Hi Natarajvd​ and LucD​ ,

    I've the same problem-

    I've read the discussion, but i've not understood as you have create the export csv file...

    If i read LucD's code (Get-FolderPath) i can't see export command....

    Can we help me ?

    I used Cheap DR script , but receive this error when try to move VM to Folder:

    The output csv file created with Cheap DR Script is writed in this way:

    Name,"Path"

    vSphere Management Assistant (vMA),"CED Production\MGMT\vSphere Management Assistant (vMA)"

    As you can see, i've folders and vms with blank space into name.

    Thank for all...



  • 18.  RE: Script for Move vm from one folder to other folder

    Posted Jan 22, 2019 11:33 AM

    Script export-05-vms-with-folderpath.ps1 should create the CSV file.

    Did you run that?



  • 19.  RE: Script for Move vm from one folder to other folder

    Posted Jan 22, 2019 11:52 AM

    Hi LucD,

    thanks for your reply.

    Yes, i used it for export.

    Source vcenter is 5.5 and destination is 6.5 , both Upd 2.

    As powershell module are these versions:

    Version         Name

    -------         ----

    6.7.0.11233116  VMware.DeployAutomation

    6.7.0.11233116  VMware.ImageBuilder

    11.1.0.11289667 VMware.PowerCLI

    6.7.0.10334489  VMware.Vim

    11.0.0.10335701 VMware.VimAutomation.Cis.Core

    11.0.0.10379994 VMware.VimAutomation.Cloud

    11.0.0.10334497 VMware.VimAutomation.Common

    11.0.0.10336080 VMware.VimAutomation.Core

    7.6.0.10230451  VMware.VimAutomation.HorizonView

    10.0.0.7893904  VMware.VimAutomation.License

    11.0.0.10364044 VMware.VimAutomation.Nsxt

    11.0.0.10334495 VMware.VimAutomation.Sdk

    11.0.0.10380515 VMware.VimAutomation.Security

    11.1.0.11289292 VMware.VimAutomation.Srm

    11.1.0.11273342 VMware.VimAutomation.Storage

    1.3.0.0         VMware.VimAutomation.StorageUtility

    11.0.0.10336077 VMware.VimAutomation.Vds

    11.0.0.10336076 VMware.VimAutomation.Vmc

    10.0.0.7893921  VMware.VimAutomation.vROps

    6.5.1.7862888   VMware.VumAutomation



  • 20.  RE: Script for Move vm from one folder to other folder

    Posted Jan 22, 2019 12:11 PM

    The Split-Path has no issue with blanks in the name.

    It looks like there is an empty Path in the CSV.

    You should check the CSV



  • 21.  RE: Script for Move vm from one folder to other folder

    Posted Jan 22, 2019 12:50 PM

    sorry...i wrong to paste ....:

    "Name","Path"

    "vSphere Management Assistant (vMA)","CED PRODUCTION\MGMT\vSphere Management Assistant (vMA)"



  • 22.  RE: Script for Move vm from one folder to other folder

    Posted Jan 22, 2019 12:57 PM