PowerCLI to Generate Nested VM Folder Structure: Difference between revisions

From roonics
Jump to navigation Jump to search
No edit summary
No edit summary
Line 13: Line 13:
TestDataCenter\SQL\Stage
TestDataCenter\SQL\Stage
TestDataCenter\SQL\Uat
TestDataCenter\SQL\Uat
</pre>
Now save this script below in the same location as folders.txt, connect to your vCenter and run it:
<pre>
$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
            }
        }
    }
}
</pre>
</pre>

Revision as of 10:11, 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
            }
        }
    }
}