chelnak
7/30/2015 - 10:00 AM

Migrate vRealize Business Groups with PowerShell

Migrate vRealize Business Groups with PowerShell

<#
Usage:

1. Generate vra-businessgroups.json by running the following cloudclient command
   
   ./cloudclient.bat vra businessgroup list --export D:\vRAExports\vra-businessgroups.json --format "JSON"

2. Configure the variables and run this script to generate businessgroup-cloudclient-commands.ps1

3. Get a replace all of the machine prefix Ids in businessgroup-cloudclient-commands.ps1 with ones obtained from the new server

   ./cloudclient.bat vra machineprefix -list

4. Run businessgroup-cloudclient-commands.ps1

#>

#region Variables
$cloudClientPath = "D:\cloudclient\bin\cloudclient.bat"
$busunessGroupJsonFile = "vra-businessgroups.json"
$businessGroupScriptFile = "businessgroup-cloudclient-commands.ps1"
#endregion

#region functions
function Format-Users($users){

    if ($users){
        return $users -join ","   
        }

    }
#endregion

#region main
$businessGroups = Get-Content -Raw -Path "$($busunessGroupJsonFile)" | ConvertFrom-Json

$outFile = @()

foreach ($item in $businessGroups.Items){

    $params = @(

        "--name '" + $($item.Name) + "'"

        #Id AD Container is empty give it the value of None
        #In the UI this is not a mandetory field but CloudClient thinks it is
        if ($item.ActiveDirectoryContainer){
            "--adContainer '" + $($item.ActiveDirectoryContainer) + "'"
            }
        else {
            "--adContainer 'None'"
        }

        "--email '" + $($item.AdministratorEmail) + "'"

        "--machinePrefixId '" + $($item.MachinePrefixId) + "'"

        #If any of the below are empty, do not include the switch in $params
        if ($item.Administrators){
            "--admins '" + $(Format-Users($item.Administrators)) + "'"
            }

        if ($item.Support){
            "--support '" + $(Format-Users($item.Support)) + "'"
            }

        if ($item.Users){
            "--users '" + $(Format-Users($item.Users)) + "'"
            }

        )
 
    #Build each command and add it to the $outFile array
    $outFile += 'Invoke-Expression -Command "' + $($cloudClientPath) + ' vra businessgroup add ' + $($params) +'"'

    }

#Build the script file
$outFile | Set-Content -Path "$($businessGroupScriptFile)"
#end region