PowerCLI

 View Only
  • 1.  PowerCLI script to get vm Folders

    Posted May 04, 2011 02:49 PM

    Hi there

    I am looking for a bit of help to script the following senario.

    I want to run a script on our production cluster to get a list of VMs and the Folder location in vCenter then export to a csv file.

    then at our DR site i want to use that csv file to move the list of VMs to the correct folders.

    I have the script below already

    Move-VM -VM fileserver1 -Destination (Get-Folder -Name "File Servers")

    but would need to have a line per VM that would constantly need updating.

    any help much appreciated.

    S



  • 2.  RE: PowerCLI script to get vm Folders
    Best Answer

    Posted May 05, 2011 12:47 AM

    Assuming that all your folders are level 1 folder, in other words not nested folders, you can do the following to create a CSV file

    Get-VM | select Name,@{N="Folder";E={$_.Folder.Name}} | `
    Export-Csv "C:\vm-folder.csv" -NoTypeInformation -UseCulture

    To move the VMs to the correct folder from this CSV file you could do

    Import-Csv "C:\vm-folder.csv" -UseCulture | %{
        Move-VM -VM $_.Name -Destination (Get-Folder -Name $_.Folder)
    }
    


  • 3.  RE: PowerCLI script to get vm Folders

    Posted May 09, 2011 11:02 AM

    Hi Luc

    the command you gave works perfectly even for nested folders as long as all folder names are unique.

    many thanks

    S



  • 4.  RE: PowerCLI script to get vm Folders

    Posted Jan 13, 2020 09:09 PM

    Muchas gracias, me sirvio mucho dicho codigo. Saludos.