如何在Azure中启动或停止应用程序服务

问题描述 投票:0回答:1

我想在午夜停止我的应用程序服务,并想在早晨启动它们。所以我遇到了两件事,即运行手册和网络作业。因此,我首先在资源组中包含了一个可以启动/停止服务的运行手册。但是当我对其进行测试时出来,我遇到了一个错误-

enter image description here

而且当我尝试使用webjob时,我使用了此code from here,但是我看不到结果。该webjob作为脚本工作,但实际上是在启动/停止我不知道的服务。我是Powershell脚本的新手,所以我不知道在代码中进行必要的更改。我不知道我是对还是错,请帮助我。谢谢。

azure azure-web-app-service azure-webjobs azure-powershell azure-runbook
1个回答
0
投票

如果要使用Azure Runbook管理Azure ARM资源,则可以在Azure自动化帐户中创建Run As accounts。创建它时,它将在Azure Active Directory(AD)中创建一个新的服务主体用户,并在订阅级别为该用户分配“贡献者”角色。有关更多详细信息,请参阅documentdocument

例如

  1. 创建Run As accounts

    a。搜索并选择自动化帐户

    b。在“自动化帐户”页面上,从列表中选择您的自动化帐户。

    c。在左窗格中,在帐户设置部分中选择帐户运行身份

    d。根据所需的帐户,选择Azure运行方式为帐户Azure经典运行方式为帐户

    e。根据感兴趣的帐户,使用“添加Azure Run As”或“添加Azure Classic Run As Account”窗格。查看概述信息后,单击创建enter image description here

  2. Create a PowerShell Workflow runbook

  3. 脚本

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" 
                } 
            }  
        } 
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.