PowerShell - Retrieve all the folders from one console and recreate them into a different one

Retrieve all the folders from one console and recreate them into a different one

Written by Giuseppe

Last published at: January 27th, 2023



Import-Module ThinscaleManagement

<# Connect to server, This will save in memory all the folder structure #>
<# from the management console #>

Connect-TSTMGMTServer -uri "https://server/TSTMgmt" -username "Administrator" -password "xxxxxxxxxxx"

$allData = Get-TSTMGMTAllData

$folderNames = New-Object -TypeName 'System.Collections.ArrayList';

foreach ($folder in $allData.Folders)
{
    $folderName = New-Object -TypeName 'System.Collections.ArrayList'
    $folderName.Add($folder.FolderName)

    if ($folder.FolderId -ne '00000000-0000-0000-0000-000000000000')
    {
        while ($true)
        {
            $folder = $allData.Folders | Where-Object {$_.ObjectId -eq $folder.FolderId}

            $folderName.Add($folder.FolderName)

            if ($folder.FolderId -eq '00000000-0000-0000-0000-000000000000')
            {
                break;
            }
        }
    }

    if ($folderName.Count -gt 1)
    {
        $folderNameString = ""

        for ($ia=$folderName.Count-1; $ia -ge 0; $ia--)
        {
            $folderNameString += ("\" + $folderName[$ia])
        }

        $folderNames.Add($folderNameString)
    }

}

$folderNames

<# Connect to a new server or connect to an SQL Server from a localdb #>
<# During deployment don't close Powershell. Switch the configuration to SQL and then run this missing code#>

Connect-TSTMGMTServer -uri "https://server/TSTMgmt" -username "Administrator" -password "xxxxxxx"

foreach ($folderName in $folderNames)
{
   
    <# Call tst mgmt create folder #>

    New-TSTMgmtFolder -FolderName $folderName

<# use the break to recreate only one folder. If the script works then remove the break and run it again #>

    break;

}