VMware Cloud Community
mandeeeps468
Contributor
Contributor
Jump to solution

how to stop duplicate folder creation attempt with Import export PowerCli function

The VM folder import functionality is functioning properly. However, I am currently encountering difficulty in managing the creation of duplicate folders in vCenter. The error arises when attempting to create a folder that already exists. What modifications can be implemented to validate existing folders and gracefully skip the creation process for duplicates?


Getting below error for duplicate folder creation - 


New-Folder : 10/23/2023 11:28:32 AM New-Folder The name 'vm' already exists.
At line:44 char:8
+ New-Folder -Name $folder.Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-Folder], DuplicateName
+ FullyQualifiedErrorId : Client20_InventoryServiceImpl_NewFolder_ViError,

===============
Function credit to - wiley

function Import-Folders {
param(
[String]$FolderType,
[String]$DC,
[String]$Filename
)

process{
$vmfolder = Import-Csv $filename | `
Sort-Object -Property Path
If ($FolderType -eq “Yellow”) {
$type = “host”
} Else {
$type = “vm”
}
foreach($folder in $VMfolder){
$key = @()
$key = ($folder.Path -split “\\”)[-2]
if ($key -eq “vm”) {
Get-Datacenter $dc | Get-Folder $type | `
New-Folder -Name $folder.Name
} else {
Get-Datacenter $dc | Get-Folder $type | `
Get-Folder $key | `
New-Folder -Name $folder.Name
}

}
}
}


CSV Data 

NamePath
vmvm\vm
Discovered virtual machinevm\Discovered virtual machine
Prodvm\Prod
vCLSvm\vCLS
PS_Testvm\PS_Test
Devvm\Dev
QA-Teamvm\Prod\QA-Team
App1&2vm\Prod\App1&2
App1&2vm\Dev\App1&2
QA-Teamvm\Dev\QA-Team
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

In the script from the link posted it already excludes the 'vm' folder on the export.
You can also add the 'vCLS' folder in that Where-clause.


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

The vCLS folder is a pre-defined system folder, you can't create that one.
Neither the vmfolder.

Perhaps try the export/import from Solved: Re: Export/Import Folder Structure - VMware Technology Network VMTN


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

Reply
0 Kudos
mandeeeps468
Contributor
Contributor
Jump to solution

Thanks for Quick response LucD. 

Yes, vCLS and VM are default folders and that is why I just want skip these folders name during import function. 

Thanks for the other PS link. I will check that one and will keep you posted. 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

In the script from the link posted it already excludes the 'vm' folder on the export.
You can also add the 'vCLS' folder in that Where-clause.


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

Reply
0 Kudos
mandeeeps468
Contributor
Contributor
Jump to solution

Thanks LucD for help. 

Export import is working fine for me now.  🤝

Reply
0 Kudos