5

即使问这个问题,我也想知道这么简单的事情对我来说怎么这么难。我要做的就是按计划自动停止和启动 Azure 网站。起初,我查看了 WebJobs,并创建了一个 powershell 脚本来停止使用 stop-azurewebsite 命令行开关的网站:

stop-azurewebsite [my site]

然后我创建了一个 .cmd 文件来调用它,使用 powershell.exe 来执行 powershell 文件

PowerShell.exe -ExecutionPolicy RemoteSigned -File stopit.ps1

我创建了一个 WebJob 来运行 powershell 命令来停止该站点,但它出错并显示一条消息:

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.

所以,我想我会走 REST API 路线。我使用 fiddler 和适当的管理证书创建了一个发布请求来调用:

https://management.core.windows.net/[my subscription id]/services/WebSpaces/[webspace]/sites/[website name]/stop

事实证明,没有“停止”命令。有“重启”,但这显然在这种情况下没有用。那么,综上所述,在特定时间安排上自动停止和随后(但要晚得多)启动 Azure 网站的合理方法是什么?

更新:

我已经想出了如何使用 PUT 发出 http 请求来停止站点

https://management.core.windows.net/[my subscription id]/services/WebSpaces/[webspace]/sites/[website name]正文为{"State":"Stopped"},但我仍然没有明显的方式在 WebJob 中向此 URL 'PUT`ing'。

4

3 回答 3

7

使用 Azure 自动化,创建一个运行手册并添加以下代码以获取所有“正在运行”的网站/Web 应用程序并关闭它们。

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

发布运行手册,您应该可以直接在 Azure 门户中安排它。确保您的自动用户已通过身份验证,并且您的订阅已在运行回溯中选择,并且应该没有问题。

同样,对于启动所有网站/webapps,只需将“正在运行”更改为“已停止”,将“停止-AzureWebsite”更改为“启动-AzureWebsite”。

于 2015-08-21T10:07:09.293 回答
0

看看 Azure 自动化。它允许您按计划运行 Powershell 脚本,并且是一种更简单的方式来运行将启动/停止/修改您的站点的自动化脚本。

于 2015-02-24T03:10:33.897 回答
0

现在使用 Azure Scheduler 按计划执行任务变得更加容易,尤其是在这种情况下。通过简单地使用预定作业进行 API 调用来停止网站。看看https://azure.microsoft.com/en-us/services/scheduler/

于 2016-03-15T21:54:24.717 回答