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"
}
}
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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 |
You have to adapt your code a bit.
Try the attached version.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I got below Error
Looks like something goes wrong with your Import-Csv.
Do you have blank lines in your CSV file?
What does this return?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Below is the Output
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
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)
$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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I got this below Error. I have newly added this parameter -runasync
I got report only for one server
Folder | VM |
DC1\vumupdate | srv2016-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
Typo, I forgot the equal sign in the $report = @() line (it's corrected above).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Grate it works Now. Thank you
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...
Script export-05-vms-with-folderpath.ps1 should create the CSV file.
Did you run that?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference