简体   繁体   中英

How to start or stop appservices in Azure

I want to stop my appservices at midnight and want to start them at morning.So i came across two things, runbooks and webjobs.So first i included a runbook which start/stop services in a resource group.But when i tested it out, i faced an error -

在此处输入图像描述

And also when i tried using a webjob, i used this code from here , but i was not able to see the result.The webjob was working as a script but was it actually starting/stoping the services i dont know.I am new to powershell scripts so i dont know where to make necessary changes in the code.I dont know whether i am doing it right or wrong, please help me out.Thank You.

If you want to manage Azure ARM Resource with Azure Runbook, you can create Run As accounts in your Azure automation account. When we create it, it will create a new service principal user in Azure Active Directory (AD) and assigns the Contributor role to this user at the subscription level. For more details, please refer to the document and the document .

For example

  1. Create Run As accounts

    a. Search for and select Automation Accounts .

    b. On the Automation Accounts page, select your Automation account from the list.

    c. In the left pane, select Run As Accounts in the account settings section.

    d. Depending on which account you require, select either Azure Run As Account or Azure Classic Run As Account .

    e. Depending on the account of interest, use the Add Azure Run As or Add Azure Classic Run As Account pane. After reviewing the overview information, click Create . 在此处输入图像描述

  2. Create a PowerShell Workflow runbook

  3. Script

workflow START_STOP_APP_SERVICE_BY_RESOURCE
{
    Param( 
        [Parameter (Mandatory= $true)] 
        [bool]$Stop, 


       [Parameter (Mandatory= $true)] 
        [string]$ResourcegroupName 
    )

    try
    {
        # use Azure As Account to log in Azure
        $servicePrincipalConnection=Get-AutomationConnection -Name "AzureRunAsConnection"      

        Add-AzureRmAccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -ApplicationId $servicePrincipalConnection.ApplicationId `
            -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
    }
    catch {
        if (!$servicePrincipalConnection)
        {
            $ErrorMessage = "Connection $connectionName not found."
            throw $ErrorMessage
        } else{
            Write-Error -Message $_.Exception
            throw $_.Exception
        }
    }
        $status = 'Stopped' 
        if ($Stop) 
        { 
            $status = 'Running' 
        } 

        # Get Running WebApps (website_Processings_Running) 
        $website_Processings_Running = Get-AzureRMWebAPP -ResourceGroupName $ResourcegroupName | where-object -FilterScript{$_.state -eq $status } 

        foreach -parallel ($website_Processing In $website_Processings_Running) 
        { 
            if ($Stop) 
            { 
                $result = Stop-AzureRmWebApp -ResourceGroupName $ResourcegroupName -Name $website_Processing.Name 
                if($result) 
                { 
                    Write-Output "- $($website_Processing.Name) shutdown successfully" 
                } 
                else 
                { 
                    Write-Output "+ $($website_Processing.Name) did not shutdown successfully" 
                } 
            } 
            else 
            { 
                $result = Start-AzureRmWebApp -ResourceGroupName $ResourcegroupName -Name $website_Processing.Name 
                if($result) 
                { 
                    Write-Output "- $($website_Processing.Name) start successfully" 
                } 
                else 
                { 
                    Write-Output "+ $($website_Processing.Name) did not started successfully" 
                } 
            }  
        } 
}

在此处输入图像描述

Today in 2022 you have to use Az module instead of AzureRM. You also have to switch to 7.1 Runtime if you want to succeed.

Param( 
    [bool]$Stop, 
    [string]$ResourcegroupName,
    [string]$WebAppName 
)
try
{
    # use Azure As Account to log in Azure
    $servicePrincipalConnection=Get-AutomationConnection -Name "AzureRunAsConnection"      

    Write-Output $servicePrincipalConnection.TenantId
    Write-Output $servicePrincipalConnection.ApplicationId
    Write-Output $servicePrincipalConnection.CertificateThumbprint

    Add-AzAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}
    $status = 'Stopped' 
    if ($Stop) 
    { 
        $status = 'Running' 
    } 

    # Get Running WebApps (website_Processings_Running) 
    $website_Processings_Running = Get-AzWebApp `
        -ResourceGroupName $ResourcegroupName] `
        -Name $WebAppName | where-object -FilterScript{$_.state -eq $status}

    Write-Output "- $($website_Processing.Name) WebApp to manage" 

    foreach ($website_Processing In $website_Processings_Running) 
    { 
        if ($Stop) 
        { 
            $result = Stop-AzWebApp -ResourceGroupName $ResourcegroupName -Name $website_Processing.Name
            if($result) 
            { 
                Write-Output "- $($website_Processing.Name) shutdown successfully" 
            } 
            else 
            { 
                Write-Output "+ $($website_Processing.Name) did not shutdown successfully" 
            } 
        } 
        else 
        { 
            $result = Start-AzWebApp -ResourceGroupName $ResourcegroupName -Name $website_Processing.Name
            if($result) 
            { 
                Write-Output "- $($website_Processing.Name) start successfully" 
            } 
            else 
            { 
                Write-Output "+ $($website_Processing.Name) did not started successfully" 
            } 
        }  
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM