我可以按计划自动启动和停止天蓝色网站吗?

son*_*lis 5 azure azure-web-sites azure-webjobs

即使提出这个问题,我也想知道如此简单的事情对我来说如此困难.我想要做的就是按计划自动停止和启动Azure WebSite.起初,我查看了WebJobs,并创建了一个powershell脚本,可以阻止使用stop-azurewebsite命令行开关的WebSite:

stop-azurewebsite [my site]
Run Code Online (Sandbox Code Playgroud)

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

PowerShell.exe -ExecutionPolicy RemoteSigned -File stopit.ps1
Run Code Online (Sandbox Code Playgroud)

我创建了一个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.
Run Code Online (Sandbox Code Playgroud)

所以,我想我会去REST API路线.我使用fiddler创建了一个post请求,并使用适当的管理证书来调用:

https://management.core.windows.net/[my subscription id]/services/WebSpaces/[webspace]/sites/[website name]/stop
Run Code Online (Sandbox Code Playgroud)

事实证明,没有'停止'命令.有'重启',但在这种情况下显然没有用.所以,所有的说,什么是在一个特定的时间安排自动执行Azure的网站停止和随后的(但要晚得多),启动时有一个合理的方式?

更新:

我已经弄清楚如何使用PUT发出http请求来停止网站

https://management.core.windows.net/[my subscription id]/services/WebSpaces/[webspace]/sites/[website name]有一个机构{"State":"Stopped"},但我仍然没有一个明显的方式在WebJob中对此URL进行"PUT".

小智 7

使用Azure自动化,创建一个Runbook并添加以下代码以获取所有"正在运行"的网站/ webapps并关闭它们.

# 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"
    }
}
Run Code Online (Sandbox Code Playgroud)

发布Runbook,您应该能够在Azure门户中直接安排它.确保您的自动用户已通过身份验证,并在回溯中选择了您的订阅,并且应该没有问题.

同样,为了启动所有网站/网络应用程序,只需将"正在运行"更改为"已停止"并将"停止 - Azure网站"更改为"启动 - Azure网站".