Question about Runbooks and Inline Script

I’m trying to create a runbook that will run a simple bash script when I start up my Azure VMs with a runbook.

However, I get an error about the VM parameter that I’m passing to the CustomScriptForLinux Set-AzureVMExtension.

What am I doing wrong? Can this be solved with InlineScript? I read something about the problem having to due with the deserialization of the $vm object and an InlineScript would fix it, but when I wrapped the block in InlineScript, I just got an error saying “InlineScript not recognized as a cmdlet”.

InlineScript{ BLOCK }

Code below, thanks for your help.

“` # Input Parameters for # – VmName: name of the vm to perform action to # – ResourceGroupName: resource group where the vm belongs to # – VmAction:action to perform (startup or shutdown) Param( [string]$VmName, [string]$ResourceGroupName, [ValidateSet(“Startup”, “Shutdown”)] [string]$VmAction )

# Authenticate with your Automation Account $Conn = Get-AutomationConnection -Name AzureRunAsConnection Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID ` -ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint # Startup VM IF ($VmAction -eq "Startup") { Start-AzureRmVM -Name $VmName -ResourceGroupName $ResourceGroupName Set-AzureRmContext -SubscriptionId 38b34c96-edb4-4213-9bce-820ff337ebe7 -TenantId $Conn.TenantID #Enter the VM name and Service name $vm = Get-AzureRmVM -ResourceGroupName $VmName -Name $VmName #Specify the command to execute $PublicConfiguration = '{"commandToExecute": " /bin/bash -l -c cd echo 'hello world'"}' #Deploy the extension to the VM $ExtensionName = 'CustomScriptForLinux' $Publisher = 'Microsoft.OSTCExtensions' $Version = '1.*' Set-AzureVMExtension -ExtensionName $ExtensionName -VM $vm -Publisher $Publisher -Version $Version -PublicConfiguration $PublicConfiguration | Update-AzureVM } # Shutdown VM IF ($VmAction -eq "Shutdown") { Stop-AzureRmVM -Name $VmName -ResourceGroupName $ResourceGroupName -Force } 

submitted by /u/DHHjr
[link] [comments]

Leave a Reply