Reputation: 3194
Even asking this question, I'm wondering how something so simple can be so difficult for me. All I want to do is automate the stopping and starting of an Azure WebSite on a schedule. At first, I looked at WebJobs, and created a powershell script that would stop the WebSite that used the stop-azurewebsite commandlet:
stop-azurewebsite [my site]
I then created a .cmd file to call it using powershell.exe to execute the powershell file
PowerShell.exe -ExecutionPolicy RemoteSigned -File stopit.ps1
I created a WebJob to run the powershell command to stop the site, but it errored with a message saying:
stop-azurewebsite : The term 'stop-azurewebsite' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
So, I figured I'd go the REST API route. I created a post request using fiddler and the proper management certificate to make a call to:
https://management.core.windows.net/[my subscription id]/services/WebSpaces/[webspace]/sites/[website name]/stop
Turns out, there is no 'stop' command. There's 'restart', but that's obviously of no use in this situation. So, all that said, what is a reasonable way to automate the stopping and subsequent (but much later) starting of an Azure WebSite on a specific time schedule?
UPDATE:
I've figured out how to issue the http request to stop the site using a PUT to
https://management.core.windows.net/[my subscription id]/services/WebSpaces/[webspace]/sites/[website name]
with a body of {"State":"Stopped"}
, but I still don't have an obvious way of 'PUT`ing' to this URL in a WebJob.
Upvotes: 5
Views: 7413
Reputation: 823
It's even easier now to do things on schedules using Azure Scheduler, especially in this situation. Stopping a website by simply making that API call with a scheduled job. Take a look at https://azure.microsoft.com/en-us/services/scheduler/
Upvotes: 0
Reputation: 86
Use Azure Automation, create a runbook and add the code below to get all 'Running' websites/webapps and shut them down.
# Get Websites/webapps
$websites = Get-AzureWebsite | where-object -FilterScript{$_.state -eq 'Running' }
# Stop each website
foreach -parallel ($website In $websites)
{
$result = Stop-AzureWebsite $website.Name
if($result)
{
Write-Output "- $($website.Name) did not shutdown successfully"
}
else
{
Write-Output "+ $($website.Name) shutdown successfully"
}
}
Publish the runbook and you should be able to schedule it straight in Azure Portal. Make sure your automated user is authenticated and your subscription selected in the runback and there should be no problems.
Similarly for starting up all websites/webapps just change 'Running' to 'Stopped' and 'Stop-AzureWebsite' to 'Start-AzureWebsite'.
Upvotes: 7
Reputation: 24656
Take a look at Azure Automation. It lets you run Powershell scripts on a schedule and is a much easier way to run automation scripts that will start/stop/modify your sites.
Upvotes: 0