PowerCLI to Generate Nested VM Folder Structure: Difference between revisions

From roonics
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 104: Line 104:


[[File:Folders.png]]
[[File:Folders.png]]
[[Category:vmware]]
[[Category:vcenter]]
[[Category:powercli]]
‎<comments />

Latest revision as of 09:05, 20 March 2022

This is NOT my script, Full credit goes to http://notesofascripter.com/2018/08/20/powercli-nested-vm-folder/

First create a txt file named folders.txt with the folder structure as below. Make sure to replace "TestDataCenter" with the name of your datacenter in vCenter.

Note: You must have the first line:

TestDataCenter\VM
TestDataCenter\WebApp
TestDataCenter\SQL
TestDataCenter\WebApp\Prod
TestDataCenter\WebApp\Stage
TestDataCenter\WebApp\Uat
TestDataCenter\SQL\Prod
TestDataCenter\SQL\Stage
TestDataCenter\SQL\Uat

Now save this script below in the same location as folders.txt, connect to your vCenter and run it:

$ErrorActionPreference = 'SilentlyContinue'
$folders = get-content folders.txt
Foreach ($Folder in $folders){
    $Path = $folder
    $SplitPath = $Path.split('\')
     
    $SplitPath = $SplitPath | Where {$_ -ne "DataCenterName"}
    Clear-Variable Folderpath
    Clear-Variable Parent
 
    Foreach ($directory in $SplitPath){
        If ($Folderpath -ne $Null){
            IF ($(Get-folder $directory | Where Parentid -eq $($Folderpath.id)) -eq $Null){
                Write-Host "Generating new folder $directory" -ForegroundColor Green -BackgroundColor Black
                Get-Folder -id $parent | New-Folder $directory
            }
            Else{
                Write-host "Folder $directory Exists!" -ForegroundColor Magenta -BackgroundColor Black
                $Folderpath = Get-Folder $directory | Where Parentid -eq $($Folderpath.id)
                $Parent = $Folderpath.Id
            }
        }
        Else {
            $FolderExist = Get-folder -Name $directory
            IF ($FolderExist -eq $Null){
                Write-host "Generating new folder $directory" -ForegroundColor DarkGreen -BackgroundColor Black
                New-folder -Name $directory -Location VM
                $Folderpath = Get-folder $directory
            }
            Else{
                Write-host "Folder $directory Exists!" -ForegroundColor DarkMagenta -BackgroundColor Black
                $Folderpath = Get-Folder $directory
                $Parent = $Folderpath.Id
            }
        }
    }
}

You will see something like: Example output:

Generating new folder TestDataCenter

Name                           Type
----                           ----
TestDataCenter                 VM
Generating new folder VM
Folder TestDataCenter Exists!
Generating new folder WebApp
WebApp                         VM
Folder TestDataCenter Exists!
Generating new folder SQL
SQL                            VM
Folder TestDataCenter Exists!
Folder WebApp Exists!
Generating new folder Prod
Prod                           VM
Folder TestDataCenter Exists!
Folder WebApp Exists!
Generating new folder Stage
Stage                          VM
Folder TestDataCenter Exists!
Folder WebApp Exists!
Generating new folder Uat
Uat                            VM
Folder TestDataCenter Exists!
Folder SQL Exists!
Generating new folder Prod
Prod                           VM
Folder TestDataCenter Exists!
Folder SQL Exists!
Generating new folder Stage
Stage                          VM
Folder TestDataCenter Exists!
Folder SQL Exists!
Generating new folder Uat
Uat                            VM
Folder  Exists!
Folder  Exists!

Now login to vCenter webui and you should see your folder structure as below:

‎<comments />