Is it possible to access and utilize variables defined in a variable set in the "Run an Azure PowerShell Script" step type?

Suppose I have a project A which has 3 or 4 major process steps and one environment defined in its default lifecycle.
It also has a prompted variable called FeatureID whose value I set when invoking octo.exe create-release, passing it a value like so: --variable “FeatureID:$(Build.SourceBranchName)”. I’m not receiving an error during release creation, but am receiving an error on the first step of my deployment process. This first step is a “Run Azure Powershell Script” step type and it makes use of several variables
from a variable set called “Azure Deployment Settings”. Variable assignment/binding is as follows:
dnssuffix = cloudapp.azure.com
region = eastus2
resourcegroup = automationtests
dnsnamepattern = replaceme.#{region}.#{dnssuffix}

I have defined an azure resource manager service principal and that works fine as well.
The script body is as follows:

$resourceGroupName = #{ResourceGroup}
$regionName = #{Region}
$virtualMachineName = #{FeatureID}
$rawDnsNamePattern = #{DnsNamePattern}

$virtualMachineName = $virtualMachineName.Replace(“features/”,"")
$calculatedDnsName = $rawDnsNamePattern.Replace(“replaceme”, $virtualMachineName)

Write-Host “ResourceGroup is $resourceGroup”
Write-Host “Region is $region”
Write-Host “VirtualMachineName is $virtualMachineName”
Write-Host “PublicDnsName is $calculatedDnsName”

$vm = Get-AzureRmVM -ResourceGroupName $resourceGroup -Name $virtualMachineName -ErrorAction SilentlyContinue

if ($vm -eq $null)
{
Set-OctopusVariable -name “TargetVMSearchResult” -value “Found”
Write-Host “Found host: $calculatedDnsName”
}
else
{
Set-OctopusVariable -name “TargetVMSearchResult” -value “NotFound”
Write-Host “Did not find host: $calculatedDnsName”
}

This is the error I am getting in the task log. Am I referencing/accessing the variables incorrectly in Write-Host ? My
intent here is to echo the variables I’m setting to the task log and also set an output variable in step1. If the VM is found,
using the $calculatedDNSName (FeatureId.eastus.cloudapp.azure.com, for example), then I would proceed with step 2
and deploy the package. If however, it’s not found, then I would like to fire an email or a manual intervention step so the VM
can be created.

Unpacking Calamari.Azure version 3.3.5 to ‘D:\Octopus\Calamari\Azure\3.3.5’
Environment : AzureCloud
Account :
TenantId :
SubscriptionId :
CurrentStorageAccount :
Account :
Environment : AzureCloud
Subscription :
Tenant :

eastus2 : The term ‘eastus2’ 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 D:\Octopus\Work\20160412222235-2\Script.ps1:2 char:11

  • $region = eastus2
  •       ~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (eastus2:String) [], ParentContainsErrorRecordException
    • FullyQualifiedErrorId : CommandNotFoundException
      The step failed: The remote script failed with exit code 1

Hi Tristan,

Thanks for getting in touch!

PowerShell is interpreting the value of #{Region} as a cmdlet and tries to execute it, so if you wrap these variables in "" it should hopefully work better (see below).

$resourceGroupName = "#{ResourceGroup}"
$regionName = "#{Region}"
$virtualMachineName = "#{FeatureID}"
$rawDnsNamePattern = "#{DnsNamePattern}"

Hope that helps!

Thank you,
Henrik

Hi Henrik,
Thanks for your quick response. Do i wrap both variables on
either side of the assignment operator or just wrap the right side?
The editor is doing preventing me from wrapping the octo variables in a
double quote " " (see #{ResourceGroup}). Instead, it is adding
a double quote at the end instead of just letting me enclose it.

[image: Inline image 1]

Hi Henrik,
I am able to enclose the octo variables now by adding a "
before the octo variable and " after. The editor will try to add another "
and I’m able to use backspace to
delete the extra " before saving it. I fired off a build now to see if step
1 would work.

Tristan

Hi Henrik,
I got past the parsing error in step 1. However, I’m noticing
that the prompted variable (–variable “FeatureID:dev”) being passed by
Octo doesn’t seem to work.

Here’s the build log I have from VSTS. So far so good.

Path to Octo.exe = C:\tasks\JabilOctopusCreateRelease\0.2.23\Octo.exe
C:\tasks\JabilOctopusCreateRelease\0.2.23\Octo.exe create-release
–project=“A” --server=http://myoctopusserver/ --apiKey=********
–deployTo=“AzureDevTest” --channel=“Default”
–releaseNotesFile=“D:\Agent_work\69\a
release-notes-b30382a0-4cdd-4f3e-858c-6fa262462a4f.md”
–packageversion=4.0.16104.2 --variable “FeatureID:dev”
Octopus Deploy Command Line Tool, version
3.3.9+Branch.master.Sha.ce01420355c08e8af3d2d4c124357bd7ecd35b56
Handshaking with Octopus server: http://myoctopusserver/
Handshake successful. Octopus version: 3.3.6; API version: 3.0.0
Authenticated as: ******** Account <> (a service account)
Finding project: A
Finding channel: Default
Finding deployment process for project: A
Finding release template…
Using version number from package step.
Release plan for release: 4.0.16104.2
Steps:

Name Version Source


1 Deploy A 4.0.16104.2 User specified
Creating release…
Release 4.0.16104.2 created successfully!
Deploying A 4.0.16104.2 to: AzureDevTest (Guided Failure: Not Enabled)


Finishing task: JabilOctopusCreateRelease



Finishing Build


Worker Worker-c12eefca-ea12-4d5d-afad-312e3975fd17 finished running job
c12eefca-ea12-4d5d-afad-312e3975fd17

In Octopus task log, here’s the error I get and this is sort of what I
expect, minus the exception since I provided the -ErrorAction
SilentlyContinue parameter
when calling the Get-AzureRMVM. Another thing I noticed is that it takes 4
minutes for it to execute the short script. Is this because of the azure
powershell
version 0.9.5 being bundled with octopus server? Is there another way for
me to tell it to use version 1.3.0? I looked at this link but it seems old
and works
only with Azure classic and not the newer Azure RM api. (
http://docs.octopusdeploy.com/display/OD/Configuring+the+version+of+the+Azure+PowerShell+modules
)
I am not able to find azure.psd1 in the RM folders.

ResourceGroup is AutomationTests
01:37:42
Info
Region is eastus2
01:37:42
Info
VirtualMachineName is dev
01:37:42
Info
PublicDnsName is dev.eastus2.cloudapp.azure.com
01:37:43
Error
Get-AzureRmVM : ResourceNotFound: The Resource
‘Microsoft.Compute/virtualMachines/dev’ under resource group
01:37:50
Error
‘AutomationTests’ was not found.
01:37:50
Error
OperationID : ‘faef504c-3231-4bdd-abbc-700429691b03’
01:37:50
Error
At D:\Octopus\Work\20160413053353-4\Script.ps1:14 char:7
01:37:50
Error

  • $vm = Get-AzureRmVM -ResourceGroupName $resourceGroup -Name
    $virtualMachineName …
    01:37:50
    Error
01:37:50
Error
+ CategoryInfo : NotSpecified: (:) [Get-AzureRmVM], ComputeCloudException
01:37:50
Error
+ FullyQualifiedErrorId :
Microsoft.WindowsAzure.Commands.Common.ComputeCloudException,
Microsoft.Azure.Commands.Co
01:37:50
Error
mpute.GetAzureVMCommand
01:37:50
Error
01:37:50
Fatal
The step failed: The remote script failed with exit code 1

![image.png](upload://3dPC5f664GacFwhZZ7VPLCygwI4.png)

Hi Henrik,
Looks like using the try catch + creating a new release gave me
the behavior I needed. Now I just need to find a way to “stop” the
subsequent steps if step A’s output
variable is equal a certain value. Do you know of another way to achieve
conditional process step execution or should i just go ahead and throw an
exception?

Thanks,
Tristan

Hi Tristan,

Yea, either throw an exception, or make your script return a non-zero exit code. These will both achieve the same thing, a failed step which means any subsequent steps set to run only on success of previous steps won’t run.

Thanks,
Henrik

Hi Henrik,
What are child steps and what step types can be child steps? When
would it be advisable to use child steps?
Is this what I can use to conditionally execute steps outside of the scope
and previous step constraints?
I have been searching the online docs but could not really find much about
child steps.

Thanks,
Tristan

Hi Tristan,

Child steps are used in rolling deployments, you can read more about it here http://docs.octopusdeploy.com/display/OD/Rolling+deployments). The gist of them are that it allows you to run a series of steps on a target before moving on to the next target, so wouldn’t really help you here I think.

Thank you and best regards,
Henrik