Azure:启动或closures多个虚拟机的脚本

是否可以编写和保存某种脚本来启动Azure PowerShell,然后validationAzure订阅,然后一步启动/closures多个虚拟机

我有5个虚拟机,我经常在特定的计划时间使用,所以我不想做login到Azure门户的步骤,逐个启动虚拟机,然后同样closures虚拟机,每次我需要使用它们! 我需要自动化这个过程。

明确地说,您应该使用Azure Automation按计划运行PowerShell脚本,以便在Azure中closures或启动您的虚拟机。 它在Microsoft网站上已经有很好的logging。

这里有3个链接,一步一步解释如何做到这一点

使用Azure自动化Runbook停止Azure虚拟机https://gallery.technet.microsoft.com/scriptcenter/Stop-Azure-Virtual-Machine-0b1fea97

使用Azure自动化closuresAzure虚拟机http://blogs.technet.com/b/georgewallace/archive/2014/11/05/shutting-down-a-azure-vm-with-azure-automation.aspx

使用Azure自动化在办公时间内只运行虚拟机https://blogs.endjin.com/2015/01/using-azure-automation-to-run-vms-during-office-hours-only/

问候

斯坦尼斯

有很多解决scheme

您可以在Azure自动化中使用Powershell脚本,该脚本将连接到您的订阅并closures或根据参数启动服务器。

然后,你可以有一个webhook连接到这些服务器,你可以连接到一个HTTP POSTbutton的地方停止或启动它们。

或者你也可以用类似的方式与ac#app做类似的事情。 这种方法的好处是您可以在接受Webhook之前在HTTP堆栈上进行身份validation

就个人而言,我有一些从API应用程序pipe理的服务器,它连接到我手机上的自动化应用程序。 当我离开办公室时,会closures我的开发服务器(当我到达时启动它)

您可以使用数据中心中的任何服务器来运行预定的PowerShell脚本。 Mark Hick在做所有这些事情方面都有很好的职位。 我想补充一点,你需要传递login参数,如果你想closures一台服务器上的所有东西,那么就像get-VM | 停止-VM。 如果你想在VM上首先停顿服务,那么你需要在每个虚拟机的foreach(VM inVMList){}循环中进入远程PowerShell,但是如果虚拟机与服务器在同一个域中,这并不困难。 如果没有,你需要使用credssp在服务器和虚拟机之间build立信任。

有一个服务pipe理API调用,将使用REST为你做(假设你使用的是传统的虚拟机)。 出于某种原因,这不是在PowerShell cmdlet中实现的(也许你可以贡献:))

查看closuresangular色此API可以一次closures(和可选地释放)一个或多个虚拟机。

<ShutdownRolesOperation xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <OperationType>ShutdownRolesOperation</OperationType> <Roles> <Name>name-of-virtual-machine</Name> </Roles> <PostShutdownAction>shutdown-action</PostShutdownAction> </ShutdownRolesOperation> 

同样, Start Roles可以同时启动多个VM。

您也可以使用SmiKar Software的Azure虚拟机调度程序。 AVMS因为它已知将连接到您的Azure订阅,并允许您select虚拟机和一个开机或关机时间表来适应。

适用于V1和V2 Azure虚拟机

http://www.smikar.com/automate-scheduled-power-azure-vms/

对于那些使用资源pipe理器 API的人来说,我写了一个脚本来并行重启多个虚拟机。 Restart-AzureRmVM不会返回,直到VM完成重新启动,所以此脚本启动多个这样的命令作为后台作业。

 function Restart-VMs { param ( [Parameter(Mandatory=$true, HelpMessage="LIKE pattern for VM name (use * for all)")] [string] $vmNamePattern, [Parameter(Mandatory=$true, HelpMessage="LIKE pattern for Resource Group name (use * for all)")] [string] $resourceGroupNamePattern ) $vmsToRestart = Get-AzureRmVm | Where-Object { $_.Name -like $vmNamePattern -and $_.ResourceGroupName -like $resourceGroupNamePattern } Write-Host "Restarting $($vmsToRestart.Length) VMs" # Need to save the profile so that the login from Login-AzureRmAccount works in the background jobs $profilePath = [System.IO.Path]::GetTempFileName() Remove-Item $profilePath Write-Host "Temporarily saving Azure profile to $profilePath" Save-AzureRmProfile -Path $profilePath $ErrorActionPreference = "Continue" # Continue restarting other machines if some fail try { $restartScriptBlock = { param ($vmToRestart, $profilePath) Select-AzureRmProfile -Path $profilePath | Out-Null Write-Host "Restarting VM: $($vmToRestart.Name)" try { Restart-AzureRmVM -Name $vmToRestart.Name -ResourceGroupName $vmToRestart.ResourceGroupName } catch { Write-Error "FAILED to restart VM: $($vmToRestart.Name)" Write-Error -ErrorRecord $_ } Write-Host "DONE restarting VM: $($vmToRestart.Name)" } $jobs = @() foreach ($vmToRestart in $vmsToRestart) { $jobs += Start-Job -ScriptBlock $restartScriptBlock -ArgumentList $vmToRestart,$profilePath } Write-Host "Restart jobs started, waiting..." Wait-Job -Job $jobs | Out-Null Receive-Job -Job $jobs Write-Host "DONE restarting $($vmsToRestart.Length) VMs" } finally { Write-Host "Deleting saved Azure profile $profilePath" Remove-Item $profilePath -Force $ErrorActionPreference = "Stop" } } 

在调用上述函数之前,您需要通过调用来login

 Login-AzureRmAccount 

请注意,它会将您的login令牌临时保存到磁盘,并将其加载到每个后台作业中,否则会出现关于未在后台作业中login的错误(请参阅https://github.com/Azure/azure-powershell/问题/ 1288 )。