我已经安排了许多虚拟机,这些虚拟机已安排了自动更新。它们中有一些正在运行的特定服务,如果在更新过程中需要重新启动,我想优雅地停止并启动它们。我有下面的模板,但是失败了...如果有人可以帮助我,我将不胜感激。
脚本看起来像这样...
<#
.SYNOPSIS
Stop a service on an AzureRM using RunCommand
.DESCRIPTION
This script is intended to be run as a part of Update Management Pre/Post scripts.
It uses RunCommand to execute a PowerShell script to stop a service
.PARAMETER SoftwareUpdateConfigurationRunContext
This is a system variable which is automatically passed in by Update Management during a deployment.
#>
param(
[string]$SoftwareUpdateConfigurationRunContext
)
#region BoilerplateAuthentication
#This requires a RunAs account
$connectionName = "AzureRunAsConnection"
$ServicePrincipalConnection = Get-AutomationConnection -Name $connectionName
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $ServicePrincipalConnection.TenantId `
-ApplicationId $ServicePrincipalConnection.ApplicationId `
-CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
$AzureContext = Select-AzureRmSubscription -SubscriptionId $ServicePrincipalConnection.SubscriptionID
#endregion BoilerplateAuthentication
#If you wish to use the run context, it must be converted from JSON
$context = ConvertFrom-Json $SoftwareUpdateConfigurationRunContext
$vmIds = $context.SoftwareUpdateConfigurationSettings.AzureVirtualMachines
$runId = $context.SoftwareUpdateConfigurationRunId
#The script you wish to run on each VM
#VM Name = ***
$scriptBlock = @"
Stop-Service -Name "service1"
Stop-Service -Name "service2"
"@
#The cmdlet only accepts a file, so temporarily write the script to disk using runID as a unique name
Out-File -FilePath "$runID.ps1" -InputObject $scriptBlock
#Start script on each machine
$vmIds | ForEach-Object {
$vmId = $_
$split = $vmId -split "/";
$subscriptionId = $split[2];
$rg = $split[4];
$name = $split[8];
Write-Output ("Subscription Id: " + $subscriptionId)
$mute = Select-AzureRmSubscription -Subscription $subscriptionId
Write-Output "Invoking command on '$($name)' ..."
Invoke-AzureRmVMRunCommand -ResourceGroupName $rg -Name $name -CommandId 'RunPowerShellScript' -ScriptPath "$runID.ps1" -AsJob
}
Write-Output "Waiting for machines to finish executing..."
Get-Job | Wait-Job
#Clean up our variables:
Remove-Item -Path "$runID.ps1"
错误似乎与语法或格式有关...它们都与以下错误类似,但使用的术语/命令不同。
**-ServicePrincipal:不将术语“ -ServicePrincipal”识别为cmdlet,函数,脚本文件或 可操作的程序。检查名称的拼写,或者是否包含路径,请确认路径正确,然后重试。
在第22行:5个字符
-ServicePrincipal`
~~~~~~~~~~~~~~~~
CategoryInfo:ObjectNotFound:(-ServicePrincipal:String)[],CommandNotFoundException
FullyQualifiedErrorId:CommandNotFoundException **
预先感谢...