我不想创建一个Runbook来启动一个特定的(或参数控制的)VM,然后在VM上运行一个脚本(本地或blob存储)。
我已经检查了很多文档,但到目前为止还没有运气让它运行起来。
到目前为止,我在同一个资源组下得到了什么:
你们中的哪些好人可以根据使用的方法对需要的指南进行抽样?
VM启动部分正在运行,但我无法使用脚本的登录+执行工作!
我不是一个技术高超的开发人员,我甚至怀疑在Azure中选择脚本语言。
任何帮助将受到高度赞赏。
谢谢,汤姆
调用命令
调用-AzureRmVMRunCommand
SET-AzureRmVMCustomScriptExtension
New-SSHSession + Invoke-SSHCommand
代码取自例如gallary“Connect-AzureVM”
参数-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"
测试结果: