Azure PoawerShell query

Hello,

i am new to Octopus and trying to run ps script from script content with below command and getting below error could you please help.

$resourceGroup = $OctopusParameters[“Azure.ResourceGroup”]
Write-Output “Disabling all alerts in resource group: $resourceGroup”

$alertrules = Get-AzureRmAlertRule -ResourceGroupName $OctopusParameters[“Azure.ResourceGroup”]

Error :-
Invoke-Expression : A parameter cannot be found that matches parameter name
June 7th 2018 17:14:03
Error
‘ResourceGroupName’.

Hi Manglesh,

Thanks for getting in touch.

Your script looks correct. Could you please confirm what step type you are using to run this script? Are you using a PowerShell Script step or an Azure PowerShell Script step?

We were able to run this command successfully as follows using an Azure PowerShell Script step:

$resourceGroup = $OctopusParameters["MarkTestWebApp"]
Write-Output "Disabling all alerts in resource group: $resourceGroup"
$alertrules = Get-AzureRmAlertRule -ResourceGroupName $resourceGroup

To help us reproduce, could you please enable print variables on your project, and attach the raw task log file for us to analyse? This will help us see the values being passed at deployment time.

Cheers
Mark

Hi,
I have been trying to deploy a script that first fetches the data from Azure portal, does some filtering and stores it in a variable. Then based on the data in variable further action is done.

$alertRules2 = Get-AzureRmAlertRule -ResourceGroup $ResourceGroup | ?{$_.AlertRuleResourceName -like “AvailTest”}

If i simply try to print the output of the variable i do not get any data, is it even possible in Octopus Deploy ?

Thank YouServerTasks-5187.log.txt.zip (153.8 KB)

Hi Manglesh,

That is definitely possible and after closer inspection I noticed that you have a small mistake in your script which would explain why you would not see any input.

You are trying to output the value of $ResourceGroup as part of your script, however, the variable doesn’t exist as you have assigned $resourceGroup. If you change your script to the following, then you should see some output:

$alertRules2 = Get-AzureRmAlertRule -ResourceGroup $resourceGroup | ?{$_.AlertRuleResourceName -like “AvailTest”}

Hope that helps!

Regards,
Shaun

Hi Shaun,

Thank you for taking time and responding to query.
I understand Octopus Deploy variables are not case senstiive, so could you please explain how is $resourceGroup different then $ResourceGroup. Secondly i even tried by changing the case unfortunately it doesn’t provide the desired result.

My goal is to see the outcome of the Powershell Azure cmdlet stores the data in that variable. so it’s not just about using the Get-AzureRmAlertRule even if i try with Get-AzureRmResourceGroup it doesn’t store anything in the variable.

Hope you would be able guide here.

Thank You.

further to above, i quickly did a test by running the following code and got the desired result, so please help me in understanding how is $resourceGroup different then $ResourceGroup or am i missing anything here ?

$resourceGroup = $OctopusParameters[“Azure.ResourceGroup”]
Write-Output “Disabling all alerts in resource group: $ResourceGroup”

Thank You.

Hi Manglesh,

The difference here is that $OctopusParameters is a Powershell hash table where accessing the values by key is case insensitive by default. This happens when accessing the hash table via square brackets such as $OctopusParameters["Azure.ResourceGroup"] which is equivalent to $OctopusParameters["azure.resourcegroup"]. One thing to note here is that this only gives the illusion of Octopus variables being case-insensitive. In reality, they aren’t :slight_smile:

To answer your question regarding how $resourceGroup is different from $ResourceGroup is once again Powershell related. Variables defined in Powershell happen to be case sensitive, so when you create a variable such as $resourceGroup like you did when assigning it to a value from the $OctopusParameters you have to refer to it by that exact casing i.e.

#This is different
$resourceGroup = $OctopusParameters[“Azure.ResourceGroup”]
#From this due to case sensitivity
$ResourceGroup = "Mary Poppins"

Since you were referring to the variable $ResourceGroup which did not exist within the scope of your script you did not get any output. When you changed the value to $resourceGroup you were using the correct casing and used the assigned value from the hash table.

I hope I managed to clarify things for you :slight_smile:

Regards,
Shaun

ok, got it and i have moved further in to the script, but this one keeps throwing challenges. I cannot filter the output.

so, i am running following code and getting the output as:

code: Get-AzureRmAlertRule -ResourceGroup $ResourceGroup | select -ExpandProperty ‘Name’

Output:
Name : int-en.ca.pods.com.hi.zone-AvailTest-AlertRule
June 12th 2018 17:41:57
Info
Properties : Microsoft.Azure.Management.Insights.Models.AlertRuleResource
June 12th 2018 17:41:57
Info
Tags : {[hidden-link:, Resource], [hidden-link:/subscriptions/ca1eacdc-c8
June 12th 2018 17:41:57
Info
2e-48b0-81ae-43bf2280e9e3/resourceGroups/pods-int-rg/providers/mic
June 12th 2018 17:41:57
Info
rosoft.insights/webtests/pods-int-rg-single.azurewebsites.net-Avai
June 12th 2018 17:41:57
Info
lTest, Resource], [hidden-link:/subscriptions/ca1eacdc-c82e-48b0-8
June 12th 2018 17:41:57
Info
1ae-43bf2280e9e3/resourceGroups/pods-int-rg/providers/Microsoft.In
June 12th 2018 17:41:57
Info
sights/components/pods-int-rg-ai, Resource]}
June 12th 2018 17:41:57
Info
Id : /subscriptions/ca1eacdc-c82e-48b0-81ae-43bf2280e9e3/resourceGroups
June 12th 2018 17:41:57
Info
/pods-int-rg/providers/microsoft.insights/alertrules/pods-int-rg-s
June 12th 2018 17:41:57
Info
ingle.azurewebsites.net-AvailTest-AlertRule
June 12th 2018 17:41:57
Info
Location : East US

As you can see the first property in the output itself is ‘Name’, however when i try to filter using the pipe and select commands in PS it simply throws following error.

select : Property “Name” cannot be found.
June 12th 2018 17:50:44
Error
At C:\Octopus\Work\20180612122040-5355-666\Script.ps1:4 char:54
June 12th 2018 17:50:44
Error

  • … ertRule -ResourceGroup $ResourceGroup | select -ExpandProperty ‘Name’
    June 12th 2018 17:50:44
    Error
  •                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    

June 12th 2018 17:50:44
Error
+ CategoryInfo : InvalidArgument: (Microsoft.Azure…rtRuleNoDeta
June 12th 2018 17:50:44
Error
ils:PSObject) [Select-Object], PSArgumentException
June 12th 2018 17:50:44
Error
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Comm
June 12th 2018 17:50:44
Error
ands.SelectObjectCommand

Since i am quite new in to Octopus i cannot figure whether filtering is allowed within script or not, assuming that it’s powershell cmdlets, it should.

Thank You.

Hi Manglesh,

Anything that Powershell supports is also supported by Octopus as we simply run Powershell. In the case of the Azure Powershell we also include version 5.7.0 of the Azure Powershell commandlets bundled with Octopus. This may account for the difference you are seeing perhaps? What version of Powershell and the Azure commandlets are you currently using?

Could I also get you to attach the raw task log as that should help us work out what is going on here?

Regards,
Shaun Marx

Hi Shaun,

Thanks for getting back, see the two files attached one is the raw logs and the other is PS out file.ServerTasks-5477.log.txt (162.1 KB)
output-log.txt (3.3 KB)

Hi Manglesh,

I can’t see anything wrong based on the logs you provided other than the error provided by Powershell indicating that the resource does not have the associated property to expand? Just to clarify, we execute a Powershell script so I would expect the exact same error outside of Octopus given the same version of the Azure Powershell modules. Does the script work outside of Octopus? If so, what version of the Azure Powershell modules do you have installed?

Regards,
Shaun