2017-05-05 130 views
1

可以使用Restart-Azure Rm Web App PowerShell重新啓動Web應用程序,但這將同時重新啓動計劃中的所有服務器,從而縮短停機時間。高級應用程序的Powershell在Azure Web應用程序上重新啓動

Azure門戶具有「高級應用程序重新啓動」功能,該功能在重新啓動各個實例之間使用時間延遲。

有沒有辦法從PowerShell調用它?

+0

有任何更新,如果你覺得我的回答是有用/有幫助。請將其標記爲答案,以便其他人可以從中受益。 –

回答

0

根據您的描述,我建議您可以首先使用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 
       } 
      }  
     } 

    } 
} 

結果: enter image description here

+0

這是一種很好的,可行的方法實現。希望在某些時候它只是Restart- Azure Rm Web App cmdlet的一個選項。 –