PowerShell - Export All Profile and Import All Profiles

Export All Profile and Import All Profiles

Written by Giuseppe

Last published at: January 27th, 2023


Import-Module ThinscaleManagement
Connect-TSTMGMTServer -uri "https//server/TSTMgmt" -username "Administrator" -password "xxxxxx"
$ProfileDirectory = "C:\Users\Usersname\Downloads\Test\"


$allData = Get-TSTMGMTAllData


<# Export all the profiles in a folder called test in downloads #>

foreach ($profile in $allData.Profiles)
{
    $profileName = $profile.ProfileName
    $productType = $profile.ProductType

    Export-TSTMGMTProfile -File ( $ProfileDirectory + $profileName +"_"+ $productType + ".json") -Profile $profile
}


<# Import all the profiles from a folder called test in downloads into a folder called Import in the console #>

foreach ($item in Get-ChildItem -Path $ProfileDirectory)
{    
    $indexOfProductTypeInString = $item.Name.LastIndexOf('_') + 1

    $productType = $item.Name.SubString($indexOfProductTypeInString, $item.Name.Length - 5 - $indexOfProductTypeInString)

    $profileJson = Import-TSTMGMTProfileObject -File $item.FullName -ProfileType $productType

    New-TSTMGMTProfile -ProfileName ("\Profiles\Import\" + $item.Name.SubString(0, $indexOfProductTypeInString - 1)) -ProfileDescription "New Imported Profile" -RevisionNotes "Profile created" -ProfileType $productType -Profile $profileJson

}