【问题标题】:Azure WebApp - How to restart individual instance using Powershell or APIAzure WebApp - 如何使用 Powershell 或 API 重新启动单个实例
【发布时间】:2018-03-19 18:13:13
【问题描述】:

我们正在运行一个包含 3 个实例的 WebApp。我们想在晚上单独重启这些实例。我试图找到一个 Powershell 或 REST API 解决方案来与一个 cron 作业一起工作,但我只是想出重新启动整个 WebApp 的解决方案。

我们知道重新启动它们的手动过程(下面的屏幕截图链接),但我们希望自动化该过程。

Screenshot link.jpg

【问题讨论】:

    标签: powershell azure web-applications azure-powershell azure-web-app-service


    【解决方案1】:

    是的,您可以使用 Azure Power Shell 来执行此操作,请查看answer

    根据您的描述,我建议您可以先使用Get-AzureRmResource 命令在您的Web 应用程序中找到每个实例的进程。然后你可以使用Remove-AzureRmResource 来停止这些进程。然后当您访问 azure web 应用程序时,azure 会自动创建新实例的进程来运行您的应用程序。

    更多细节,你可以参考下面的powershell代码:

    Login-AzureRmAccount
    Select-AzureRmSubscription -SubscriptionId '{your subscriptionid}'
    
    $siteName = "{sitename}"
    $rgGroup = "{groupname}" 
    
    $webSiteInstances = @()
    
    #This gives you list of instances
    $webSiteInstances = Get-AzureRmResource -ResourceGroupName $rgGroup -ResourceType Microsoft.Web/sites/instances -ResourceName $siteName -ApiVersion 2015-11-01 
    
    $sub = (Get-AzureRmContext).Subscription.SubscriptionId 
    
    foreach ($instance in $webSiteInstances)
    {
        $instanceId = $instance.Name
        "Going to enumerate all processes on {0} instance" -f $instanceId 
    
        # This gives you list of processes running
        # on a particular instance
        $processList =  Get-AzureRmResource `
                        -ResourceId /subscriptions/$sub/resourceGroups/$rgGroup/providers/Microsoft.Web/sites/$sitename/instances/$instanceId/processes `
                        -ApiVersion 2015-08-01 
    
        foreach ($process in $processList)
        {               
            if ($process.Properties.Name -eq "w3wp")
            {            
                $resourceId = "/subscriptions/$sub/resourceGroups/$rgGroup/providers/Microsoft.Web/sites/$sitename/instances/$instanceId/processes/" + $process.Properties.Id            
                $processInfoJson = Get-AzureRmResource -ResourceId  $resourceId  -ApiVersion 2015-08-01                                     
    
                # is_scm_site is a property which is set
                # on the worker process for the KUDU 
    
                    $computerName = $processInfoJson.Properties.Environment_variables.COMPUTERNAME
    
                    if ($processInfoJson.Properties.is_scm_site -ne $true)
                {
                    $computerName = $processInfoJson.Properties.Environment_variables.COMPUTERNAME
                    "Instance ID" + $instanceId  + "is for " +   $computerName
    
                    "Going to stop this process " + $processInfoJson.Name + " with PID " + $processInfoJson.Properties.Id
    
                    # Remove-AzureRMResource finally STOPS the worker process
                    $result = Remove-AzureRmResource -ResourceId $resourceId -ApiVersion 2015-08-01 -Force 
    
                    if ($result -eq $true)
                    { 
                        "Process {0} stopped " -f $processInfoJson.Properties.Id
                    }
                }       
           }
    
        }
    }
    

    【讨论】:

    • 太棒了,我已经对其进行了测试并且可以正常工作。很高兴你能提供帮助,因为我的 Azure 支持工程师说没有办法,所以我很沮丧。 :)
    • @KentRozenberg 如果我的回答有效,请不要忘记接受它作为答案。
    【解决方案2】:

    如果您正在查看 Windows Azure PowerShell Cmdlet,您要使用的命令是 Reset-AzureRoleInstance (http://msdn.microsoft.com/en-us/library/jj152835.aspx)

    【讨论】:

    • 这只支持ASM模型?
    • @ThuanNg 这是一个错误的答案。它仅适用于 Azure 云服务器(经典),OP 想要重新启动 Web 应用程序的实例。
    • @ShengbaoShui-MSFT 那是正确的应用服务实例类型
    • @KentRozenberg 有可能,您可以查看我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多