如何使用 powershell 使 Azure 云服务自行暂停

How to get an Azure Cloud Service to suspend itself using powershell

我有一个 Azure 云服务,我希望它在工作完成后自行挂起。我有一个方法(如下)可以从控制台应用程序暂停云服务,但是当我放置该方法并从已部署的云服务中调用它时,它不会自行暂停。

    public void StopService(string serviceName, string email, string password)
    {
        using (PowerShell script = PowerShell.Create())
        {
            script.AddScript(string.Format("$password = ConvertTo-SecureString \"{0}\" -AsPlainText -Force", password));
            script.AddScript(string.Format("$userName = \"{0}\"", email));
            script.AddScript(string.Format("$creds = New-Object System.Management.Automation.PSCredential ($userName, $passwprd)"));

            /*Code to sign in to azure subscription
             * ...
             * ...
            */

            script.AddScript(string.Format("$Status = (Get-AzureDeployment -ServiceName \"{0}\").Status", serviceName));
            script.AddScript(string.Format("if($Status -eq \"Running\") {1} Get-AzureService -ServiceName \"{0}\" | Stop-AzureService {2}", serviceName, _bracketOpen, _bracketClose));
            script.AddScript(string.Format("(Get-AzureDeployment -ServiceName \"{0}\").Status", serviceName));

            Collection<PSObject> result = script.Invoke();
        }
    }

Azure 云服务可以自行暂停还是需要暂停外部服务?

您必须改用 Set-AzureDeployment cmdlet。这是一个基本示例:

Set-AzureDeployment -Status -ServiceName "MySvc1" -Slot "Production" -NewStatus "Suspended"

请注意,暂停云服务并不意味着您不会为此付费。它类似于在不重新分配的情况下关闭 VM。停止消耗资源的唯一方法是删除部署。

更多信息:

https://msdn.microsoft.com/en-us/library/azure/dn495140.aspx

您还可以做的另一件事是,您可以使用 azure 自动化运行手册,它会持续监控您的云服务的状态,并 stop/suspend 根据您的要求进行监控。

Link-

http://blogs.technet.com/b/keithmayer/archive/2014/04/04/step-by-step-getting-started-with-windows-azure-automation.aspx