自动化SQL DataWareHouse(暂停和恢复)

时间:2018-11-05 15:35:56

标签: powershell

使用Powershell自动执行SQL DWH暂停和恢复。 注意:我将对此发表答案。

1 个答案:

答案 0 :(得分:-1)

[CmdletBinding(DefaultParametersetName='None')]
Param 
(    
    [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 
    [String] 
    $AzureSubscriptionId, 
    [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 
    [String] 
    $AzureDataWareHouseList="All", 
    [Parameter(Mandatory=$true)][ValidateSet("Suspend","Resume")] 
    [String] 
    $Action 
)

function PauseAzureDWH
{
    Param
    (
    [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 
    [String] 
    $AzureSubscriptionId, 
    [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 
    [String] 
    $AzureDataWareHouseList="All", 
    [Parameter(Mandatory=$true)][ValidateSet("Suspend","Resume")] 
    [String] 
    $Action
    )
    try     
    {

    Login-AzureRmAccount
    Select-AzureRmSubscription -SubscriptionId $AzureSubscriptionId 

    if($AzureDataWareHouseList -ne "All") 
    {
        $AzureDWHList = @()
        $AzureDWHTotalList = $AzureDataWareHouseList.Split(",") 
        foreach($DWHitem in $AzureDWHTotalList)
        {
            $DWH = "*$DWHitem*"
            $DWH = Get-AzureRmResource | Where-Object ResourceName -like $DWH
            if($DWH -ne $Null)
            {
                $dwc = $DWH.ResourceName.split("/")
                # splat reused parameter lists
                $ThisDW = @{
                'ResourceGroupName' = $DWH.ResourceGroupName
                'ServerName' = $dwc[0]
                'DatabaseName' = $dwc[1]
                }
                $AzureDWHList += $ThisDW
            }
            else
            {
                Write-Warning "Given DataWarehouse '$DWHitem' is not found in given subscription"
            }
        }
    }
    else 
    {
        [array]$TotalDataWareHouseList = Get-AzureRmResource | Where-Object ResourceType -EQ "Microsoft.Sql/servers/databases" | Where-Object Kind -Like "*datawarehouse*"
        $AzureDWHList = @()
        foreach($DWH in $TotalDataWareHouseList)
        {
            $dwc = $DWH.ResourceName.split("/")
            $ThisDW = @{
            'ResourceGroupName' = $DWH.ResourceGroupName
            'ServerName' = $dwc[0]
            'DatabaseName' = $dwc[1]
            }
            $AzureDWHList += $ThisDW
        }
    }

    <# foreach($AzureDWHItem in $AzureDWHList) 
    {
        if(!(Get-AzureRmResource | ? {$_.Name -eq $AzureDWHItem.ServerName}) ) 
        {
            throw " AzureDWH : [$AzureDWHItem] - Does not exist! - please Check your inputs " 
        }
    } #>

    if($Action -eq "Suspend") 
    {
        Write-Output "Suspending Azure DataWareHouses"; 
        foreach ($AzureDWH in $AzureDWHList) 
        { 
            $status = Get-AzureRmSqlDatabase @AzureDWH | Select Status
            if($status.Status -eq "Online")
            {
                Suspend-AzureRmSqlDatabase @AzureDWH
            }
        }
    }
    else 
    { 
        Write-Output "Resuming Azure DataWareHouses"; 
        foreach ($AzureDWH in $AzureDWHList) 
        {
            $status = Get-AzureRmSqlDatabase @AzureDWH | Select Status
            if($status.Status -eq "Paused")
            {
                Resume-AzureRmSqlDatabase @AzureDWH
            }
        }
    }
}

catch
{
    Write-Error " Exception while getting resource details and writing back to CSV"
    Write-Error $_.Exception.message
    Write-Error " ErrorStack: $Error[0] "
    exit 1

}
}

PauseAzureDWH -AzureSubscriptionId $AzureSubscriptionId -AzureDataWareHouseList $AzureDataWareHouseList -Action $Action