Public/Repair-VCDAReplicators.ps1

<#
Copyright 2023 VMware, Inc.
SPDX-License-Identifier: BSD-2-Clause
#>


function Repair-VCDAReplicators {
    <#
    .SYNOPSIS
        Power On all or given VCDA virtual machine in AVS environment.
    .DESCRIPTION
        Power On all or given VCDA virtual machine in AVS environment.
        By default all virtual machines that are not in 'PoweredOn' state will be powered on.
    .PARAMETER VMName
        Name of the VCDA Virtual Machine to Power On.
    .EXAMPLE
        Start-VCDAVM
        Will Power On all VCDA virtual machines that are not in 'PoweredOn' state.
    .EXAMPLE
        Start-VCDAVM -VMName 'VCDA_AVS_Replicator_01'
        Will Power on a VCDA virtual machine named 'VCDA_AVS_Replicator_01'.
    #>

    [AVSAttribute(30, UpdatesSDDC = $false)]
    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Name of the VCDA Virtual Machine to Power On.')]
        [ValidateNotNullOrEmpty()]
        [string]
        $VMName
    )
    Try {
        #make sure vc connection is healthy, script will fail if not
        if ($null -eq ((Get-View SessionManager -Server $global:DefaultVIServer).CurrentSession)) {
            Write-Error "vCenter server '$($Global:defaultviserver.Name)' connection is not heathy."
        }
        $manager_vm = Get-VCDAVM -type "cloud"
        $replicator_vms = Get-VCDAVM -type "replicator"

        $manager_ip = $manager_vm.ExtensionData.guest.IpAddress
        $manager_service_cert = ($manager_vm.ExtensionData.Config.ExtraConfig | Where-Object { $_.key -eq 'guestinfo.manager.certificate' }).value
        $manager_url = 'https://' + $manager_ip + ':8441'
        #make sure the certificate we see over the network matches the one of the VM.
        $manager_remote_cert = Get-RemoteCert -url  $manager_url -type string
        if ($manager_remote_cert -ne $manager_service_cert) {
            Write-Error "Manager certificate seen on the network differs from the expected one."
        }
        $man_pass = Get-VCDAVMPassword -name $manager_vm.name
        $man_credentials = New-Object System.Management.Automation.PSCredential("root", $man_pass.current)
        $vcda_server = Connect-VCDA -Server $manager_ip -AuthType Local -Credentials $man_credentials -port 8441 -SkipCertificateCheck -NotDefault
        $site = (Get-Config -server $vcda_server).site
        $replicators = Get-VCDAReplicator -Server $vcda_server| Where-Object { $_.site -eq $site }
        $SSO_domain = (Get-IdentitySource -System).name
        $ssoUser = $Script:vcda_avs_params.vsphere.sa_username + '@' + $SSO_domain
        $ssoPass = $PersistentSecrets[$Script:vcda_avs_params.vsphere['sa_current_password']] | ConvertTo-SecureString -AsPlainText -Force

        foreach ($replicator in $replicators) {
            $replicator_ip = ([uri]$replicator.apiUrl).Host
            $repl_pass = Get-VCDAVMPassword -name (($replicator_vms | Where-Object { $_.ExtensionData.guest.IpAddress -eq $replicator_ip }).name)
            $InvokeParams = @{
                'apiUrl'        = $replicator.apiUrl
                'apiThumbprint' = $certThumbprint
                'rootPassword'  = $repl_pass.current
                'ssoUser'       = $ssoUser
                'ssoPassword'   = $ssoPass
                'replicatorId'  = $replicator.id
                'server'        = $vcda_server
            }
            Write-Log -message "Rapairing Replicator $replicator_ip"
            $response = Repair-VCDAReplicator @InvokeParams
        }
    }
    Catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}