Start-AzPostgressFlex.Ps1

Function Start-AzPostgressFlex {
    param(
        [string] $AzureRunAsConnection,
        [string] $resourceGroupName,
        [string] $Name
        # [parameter(Mandatory=$false)]
        # [string] $scalingScheduleTimeZone = "W. Europe Standard Time"
    )
 
    filter timestamp { "[$(Get-Date -Format G)]: $_" }
 
    Write-Output 'Script started.' | timestamp
 
    $VerbosePreference = 'Continue'
    $ErrorActionPreference = 'Stop'
 
    # Connect using a Managed Service Identity
    try {
        $AzureContext = (Connect-AzAccount -Identity).context
        Write-Output 'Authenticated with MI.' | timestamp
    }
    catch {
        Write-Output 'There is no system-assigned user identity. Aborting. Setup the same or try using RunAs account automation method.';
    }
    Write-Output ('AzureRunAsConnection: {0} ResourceGroupName: {1} Name: {2}' -F $AzureRunAsConnection, $ResourceGroupName, $Name)
 
    # Get the server object
    $pgsqlflex = Get-AzPostgreSqlFlexibleServer -ResourceGroup $ResourceGroupName -ServerName $Name -ErrorAction Stop
    Write-Output "Postgress SQL Server Name: $($pgsqlflex.Name)" | timestamp
    Write-Output "Current server status: $($pgsqlflex.State), SKU: $($pgsqlflex.SkuName)" | timestamp
    if ($pgsqlflex.State -eq "Stopped") {
        ('-' * 75)
        Write-Output "Triggering [START] operation for $($Name)" | timestamp
        ('-' * 75)
        Start-AzPostgreSqlFlexibleServer -ResourceGroupName $ResourceGroupName -Name $Name -ErrorAction Stop
    }
    else {
        Write-Output 'Service already Running !' | timestamp
    }
    Write-Output 'Script finished.' | timestamp
}