简体   繁体   中英

Azure WebApp - How to restart individual instance using Powershell or API

We are running a WebApp with 3 instances. We would like to restart these instances at night individually. Im trying to find a Powershell or REST API solution to work along with a cron job but I'm only coming up with solutions to restart the entire WebApp.

We are aware of the manual process to restart them (screenshot link below) but we would like to automate the process.

Screenshot link.jpg

Yes, you could use Azure Power Shell to do this, please check this answer .

According to your description, I suggest you could firstly find each instance's process in your web app by using Get-AzureRmResource command. Then you could use Remove-AzureRmResource to stop these processes. Then when you access the azure web application, azure will automatic create new instance's process to run your application.

More details, you could refer to below powershell codes:

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

    }
}

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

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