BaluVirigineni
Enthusiast
Enthusiast

I want to move VMs to a folder name called Linux, but we have multiple folder by the same name in our Datacenter, please provide me PowerCLI Script to move exactly

Jump to solution

I want to move VMs to a folder name called Linux, but we have multiple folder by the same name in our Datacenter, please provide me PowerCLI Script to move exactly.

I tried to use to the below FolderByPath function, but not sure how to use it and get worked. Please help me in this.

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 -Server $vc

        }

      }

    }

  }

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You can try to fetch the folder object stepwise as well.

Something like this for example

$folder = Get-Datacenter -Name "LTX01-G8" | Get-Folder -Name "LTX-Servers" | Get-Folder -Name "LTX Servers 0000 - 0299" | Get-Folder -Name Linux

I assume the first qualifier in your path was the datacenter.


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

View solution in original post

0 Kudos
11 Replies
LucD
Leadership
Leadership

The Get-FolderByPath function from my Folder by Path post returns a folder object.

That folder object can then be used on any parameter that accepts a folder object.

For example,

$folder = Get-FolderByPath -Path "DC1/Blue1/Blue12/Blue121/Linux"

Get-VM -Name Srv1 | Move-VM -Destination $folder


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

0 Kudos
BaluVirigineni
Enthusiast
Enthusiast

Hi LucD,

Thanks for the reply.. what I have done is " Saved the script as FolderbyPath.Ps1" and tried to run the command but the PowerCLI is throwing error as below. How should I run functions in PowerCLI..

pastedImage_2.png

0 Kudos
LucD
Leadership
Leadership

For a function that is stored in a .ps1 file, you will have to dot-source the .ps1 file.

In other words, feed the function definition to the PowerShell engine, so you can call it later on.

For that do

. ./FolderByPath.ps1

Note that there is a blank between the 2 dots.


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

0 Kudos
BaluVirigineni
Enthusiast
Enthusiast

Hi LucD,

I ran the script as you said, when I ran the script its not giving any output..just prompt is coming after a minute.

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

Once the function is dot-sourced, you can call it.

So you do

. ./FolderByPath.ps1

$folder = Get-FolderByParh  -Path "LTX01-G8/LTX-Servers/LTX Servers 0000 - 0299/Linux"

Get-VM -Name YourVM | Move-VM -Destination $folder

This will move 1 VM, called YourVM to the folder.

Note that you have to do the dot-sourcing only once in your session.


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

0 Kudos
BaluVirigineni
Enthusiast
Enthusiast

Hi LucD,

I saved the below content as MoveVM.PS1 and ran the command but its still giving error as earlier.

. ./FolderByPath.ps1

$folder = Get-FolderByPath  -Path "LTX01-G8\LTX-Servers\LTX Servers 0000 - 0299\Linux"

Get-VM -Name ltxl0278 | Move-VM -Destination $folder

Error Screenshot:

pastedImage_0.png

0 Kudos
BaluVirigineni
Enthusiast
Enthusiast

I have opened a new PowerCLi windows and ran the script, seems that now its running script, however its giving error while collecting Inventory details.

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

Try using the forward slash (/) instead of the back slash (\) in the path.


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

0 Kudos
BaluVirigineni
Enthusiast
Enthusiast

Tried using backward slash and its stil giving the same error, Lucd I just want to know cant we move the VMs to Folder by knowing the FolderIDs..

0 Kudos
LucD
Leadership
Leadership

You can try to fetch the folder object stepwise as well.

Something like this for example

$folder = Get-Datacenter -Name "LTX01-G8" | Get-Folder -Name "LTX-Servers" | Get-Folder -Name "LTX Servers 0000 - 0299" | Get-Folder -Name Linux

I assume the first qualifier in your path was the datacenter.


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

View solution in original post

0 Kudos
BaluVirigineni
Enthusiast
Enthusiast

Great it worked like a charm..you made it.. Thanks a ton LucD..

0 Kudos