如何创建Azure自动化Runbook,它将启动VM,然后在VM上执行(本地或Azure)脚本?

问题描述 投票:0回答:1

我不想创建一个Runbook来启动一个特定的(或参数控制的)VM,然后在VM上运行一个脚本(本地或blob存储)。

我已经检查了很多文档,但到目前为止还没有运气让它运行起来。

到目前为止,我在同一个资源组下得到了什么:

  1. VM已创建
  2. 自动化帐户创建包括运行方式帐户
  3. Azure自动化解决方案(OMS)
  4. 自动帐户下的凭证(我自己的帐户)
  5. 使用了几个Runbook画廊和其他代码示例,使用了例如Start-AzureVM ... Invoke-Command ...

你们中的哪些好人可以根据使用的方法对需要的指南进行抽样?

VM启动部分正在运行,但我无法使用脚本的登录+执行工作!

我不是一个技术高超的开发人员,我甚至怀疑在Azure中选择脚本语言。

任何帮助将受到高度赞赏。

谢谢,汤姆

调用命令

调用-AzureRmVMRunCommand

SET-AzureRmVMCustomScriptExtension

New-SSHSession + Invoke-SSHCommand

代码取自例如gallary“Connect-AzureVM”

azure-automation
1个回答
0
投票

参数-ScriptPath of Invoke-AzureRmVMRunCommand不应指向远程计算机中的路径,而应指向Runbook环境的本地路径。

示例代码如下(在远程虚拟机中创建名为atestfile.txt的文件):

$ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'

Add-AzureRmAccount `
    -ServicePrincipal `
    -TenantId $ServicePrincipalConnection.TenantId `
    -ApplicationId $ServicePrincipalConnection.ApplicationId `
    -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint

#define resource group and vm name
$resourceGroup ="xxx"
$VmName ="xxx"

#define the scripts in $scriptblock, and add the content of $scriptblock to aa.ps1 in current directory of runbook
write-output "create test file"
$scriptblock = "New-Item -path c:\test -name atestfile.txt -itemtype file -force"
Out-File -FilePath aa.ps1 -InputObject $scriptblock

#Note that the -ScriptPath should not point to the remote path(in remote vm), it should point to the local path where you execute the command Invoke-AzureRmVMRunCommand
Invoke-AzureRmVMRunCommand -ResourceGroupName $resourceGroup -Name $VmName -CommandId 'RunPowerShellScript' -ScriptPath aa.ps1

#after execution, you can remove the file
Remove-Item -Path aa.ps1

write-output "done now"

测试结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.