0
votes

My dev environment is using the remote hybrid worker to execute a console app. I am executing the app via webhook and it works exactly as i want. The powershell looks like this:

[CmdletBinding()]
Param([object]$WebhookData) #this parameter name needs to be called 
WebHookData otherwise the webhook does not work as expected.

$VerbosePreference = 'continue'

    if($WebhookData -ne $null) 
        {
        C:\Projects\myconsoleApp.exe
        }
    else
        {
        Write-Error -Message 'Runbook was not started from Webhook' -ErrorAction stop
        }

For the life of me I cannot find a sample to do the same thing on my azure VM where my production code is living. I have many vms and I only want to run this on one VM. This is where I am at right now.. I can't this to work at all.

[CmdletBinding()]
   #workflow MyFirstRunbook-Workflow
#{
$ResourceGroupName = "My-Resource_Group"
$VMName = "my-vm-name"

#connect to the VM
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
#Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
#connect to the account
Connect-AzureRmAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

#run the console app
C:\projects\myconsoleapp.exe

#Start-AzureRmVM -Name $VMName -ResourceGroupName $ResourceGroupName
#}

My error output looks like this:

Connect-AzureRmAccount : The term 'Connect-AzureRmAccount' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:11 char:1 + Connect-AzureRmAccount -ServicePrincipal -Tenant $Conn.TenantID -Appl ... + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Connect-AzureRmAccount:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException C:\projects\myconsoleapp.exe : The term 'C:\projects\myconsoleapp.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:14 char:1 + C:\projects\myconsoleapp.exe + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\projects\myconsoleapp.exe:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

I am confused pretty much about everything when it comes to asking my one azure VM to do what I would think is a pretty simple request. I don't know if I am supposed to be installing something onto my VM or.... ? The VM is fairly new.. ~6 months old

--------------UPDATE---------------

I changed my code to read as

workflow MyFirstRunbook-Workflow
{
Get-Module AzureRm
Install-Module AzureRm -Force
$ResourceGroupName = "My-Resource_Group"
$VMName = "My-Vm-Name"

#connect to the VM
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
#connect to the account
Connect-AzureRmAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

#run the console app
C:\projects\myconsoleapp.exe

#Start-AzureRmVM -Name $VMName -ResourceGroupName $ResourceGroupName
}

which ended up throwing this error

At line:17 char:1 + C:\projects\myconsoleapp.exe + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Cannot find the 'C:\projects\myconsoleapp.exe' command. If this command is defined as a workflow, ensure it is defined before the workflow that calls it. If it is a command intended to run directly within Windows PowerShell (or is not available on this system), place it in an InlineScript: 'InlineScript { C:\projects\myconsoleapp.exe }' + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : CommandNotFound

What I cannot understand is how it knows to connect to the particular VM I have listed since nowhere does it use the $VMName or $ResourceGroupName?? I am so confused about how to get this to run on azure.

1

1 Answers

0
votes

The issue you are having is likely due to the version of Azure PowerShell you are running on the VM. Connect-AzureRMAccount is a valid command but when it is not recognized it is usually due to having an older version.

Try updating your PS module and trying the code again.

Get-Module AzureRm
Install-Module AzureRm -Force