PowerCLI to Generate Nested VM Folder Structure: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 54: | Line 54: | ||
} | } | ||
} | } | ||
</pre> | |||
You will see something like: | |||
Example output: | |||
<pre style="color: silver; background: black; width: 800px"> | |||
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! | |||
</pre> | </pre> |
Revision as of 10:12, 10 August 2021
First create a txt file named folders.txt with the folder structure as below.
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!