1
votes

I wan't to create a Runbook that will start a specific (or parameter controlled) VM, and then run a script (locally or from blob storage) on the VM.

I have checked a lot of documentation, but so far without luck in getting it to work.

What I got so far under the same Resource Group:

  1. VM created

  2. Automation account created incl. Run As account

  3. Azure Automation solution (OMS)

  4. Credential (to my own account) under the automation account

  5. Used several Runbook galleries and other code examples using functions as e.g. Start-AzureVM ... Invoke-Command ...

Anyone of you good people out there who can sample a guideline on what is needed depending on methods being used?

The VM start part is working, but I cannot get the login + executing of script to work!

I'm not a high skilled developer, and I have even doubts about choosing between the script languages in Azure.

Any help will be highly appreciated.

Thanks, Tom

Invoke-Command

Invoke-AzureRmVMRunCommand

Set-AzureRmVMCustomScriptExtension

New-SSHSession + Invoke-SSHCommand

Code taken from e.g. gallary "Connect-AzureVM"

1
Regarding run a script (locally or from blob storage) on the VM, does the locally means that the scripts file is on the VM?Ivan Yang
Yes, either stored on the VM or in a container. The main target is run get the script to run after succesfull login :-)Tom Djernæs
Below script run with error returned: Invoke-AzureRmVMRunCommand : Could not find file 'C:\home\tdj\test.sh'. At line:6 char:1 + Invoke-AzureRmVMRunCommand -ResourceGroupName 'SAPBASIS' -Name 'sles1 ...Tom Djernæs
PS SCRIPT: $conn = Get-AutomationConnection -Name AzureRunAsConnection Login-AzureRmAccount -ServicePrincipal -Tenant $conn.TenantID ` -ApplicationId $conn.ApplicationID -CertificateThumbprint $conn.CertificateThumbprint Start-AzureRmVM -Name 'VM' -ResourceGroupName 'RG' Invoke-AzureRmVMRunCommand -ResourceGroupName 'RG' -Name 'VM' -CommandId 'RunPowerShellScript' -ScriptPath '/home/user/test.sh' -Parameter @{"arg1" = "var1";"arg2" = "var2"}Tom Djernæs
You may misunderstand the parameter -ScriptPath of Invoke-AzureRmVMRunCommand, as per my test, -ScriptPath is a path where you start your command, not on the remote machine.Ivan Yang

1 Answers

3
votes

the parameter -ScriptPath of Invoke-AzureRmVMRunCommand should not point to the path in the remote computer, but should point to the local path of runbook environment.

Sample code like below(create a file named atestfile.txt in the remote vm):

$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"

Test result:

enter image description here