How to get the id of a VM in azure where power state is running and a specific tag is null? - azure

So I am trying to get the ID of all VMs across all subscriptions and regions, where a specific tag is null. For this I am using the following command
az vm list -d --query '[?!not_null(tags.run)]|[].id'
Please note: I want to get the ids only if the tag doesn't exist
Here notice I need to use single quotes to cover the query as I am using the '!' operator to inverse the not_null() function. If I were to use double quotes bash will throw an event not found error.
So now the problem arises when I also want to add a condition to check the current state of the VM and return id only if it is running and tag doesn't exist.
az vm list -d --query '[?!not_null(tags.run)] | [?powerState=="VM running"].id'
Here I have to wrap VM running in double quotes and this gives me an empty output as the string is not being matched because the query expects single quotes like so -
"[?powerState=='VM running'].id"
Could someone help me with a workaround for this?

Use raw string literals for VM running string. You just have to surround your string with a back tick and a double quote.
az vm list -d --query '[?!not_null(tags.run)]|[?powerState==`"VM running"`].id'

Related

Substituting New Relic query element with Bash variable - Bash

I would like to query New Relic (a logging/monitoring service) for some metrics using the New Relic API. I am running this query through a Docker container using a Bash script, and attempting to pass in a dynamic variable.
Here is my simplified script:
##!/bin/bash
day='Since 1 day ago'
#GPU Metrics Query
newrelic nrql query --accountId {account_id} --query 'SELECT * FROM NvidiaGpuMetricSample $day'
The query that the New Relic API accepts is:
newrelic nrql query --accountId {account_id} --query 'SELECT * FROM NvidiaGpuMetricSample Since 1 day ago'
And the error message I receive with this Bash script above is:
level=fatal msg="NRQL Syntax Error: Error at line 1 position 37, unexpected '$'"
I have tried to use different methods to pass in the variable, for example using 'eval $day' and passing that in a new variable or using /"$day"/ within the query string, but nothing seems to give the API what it is looking for.
Summary: I would like to pass a string variable into a string query using Bash.
Use double quotes instead of single quotes.
newrelic nrql query --accountId {account_id} --query "SELECT * FROM NvidiaGpuMetricSample $day"
Referencing variables in Bash is possible only inside double quotes, see https://tldp.org/LDP/abs/html/quotingvar.html

Azure Devops Release Pipeline - Keyvault with special characters in the secret

I'm running a devops release pipeline and i pull back the keyvaults secrets via this task
steps:
task: AzureKeyVault#1
My secret value looks something like this. abc$def&ghi
I'm updating a database record with this secret via Powershell.
The value that ends up in the database is "abcghi" The $ and characters up to, and including the & are excluded.
Do I need to escape the secrets coming from keyvault somehow?
-Randy
Are you trying to use the secret value inside of a double-quoted string in your PowerShell database update script? Something like this:
$sqlCommand = "update table set value='abc$def&ghi' where id=1";
If so, PowerShell is attempting to expand $def as a variable. It's probably not really a variable and will expand into an empty string.
You can escape the dollar sign in your key vault by using a backtick `$, but you have to be careful if you need to use that secret in a non-PowerShell scenario.
Alternatively, you could use single-quote characters around your string instead; although you'll then need to escape any single-quote characters used inside that string:
$sqlCommand = 'update table set value=''abc$def&ghi'' where id=1';
See the PowerShell docs about quoting rules for more information.

Azure DevOps - pipeline variables - special char issue $$

I am using DevOps pipeline to build and deploy to different environments
For one environment I am encountering this issue where i am using a Pipeline Variable with $$ in the value
For Example:
Password pipeline variable with value = $omeCla$$Password
When i deploy it fails and when i check the logs the password is displayed as $omeCla$Password. So basically when $$ are together it drops one $
For all variable i am using regex __VaraibleValue__ and its working fine
I have tried:
$omeCla$\$Password to try and escape and it displays as $omeCla$\$Password . So basically \ doesn't work.
I tried '$omeCla$$Password' to try and escape and it displays as '$omeCla$Password'
I want to keep this value as a normal pipeline variable before review
So basically how can I escape this?
Or should I add a Secret Token here in the replace token task (see screenshot below)? and then make the pipeline variable secret? If so, what should I set for Secret Token? Also, in app.config in my repo what should I use instead of the regex __VariableName__ that I use for normal variables?
The solution was to use 4 $. So if you have $$ together you need to add $$$$
Example: $someCla$$$$Password
#JaneMa-MSFT as requested
https://developercommunity.visualstudio.com/content/problem/1296808/azure-pipeline-how-to-escape-special-characters-in.html

How to find length of result array in Azure CLI via JMESPath?

I am trying to "explore" json results from an Azure CLI command using the --query switch (e.g. az functionapp list --query <something>), and to get started I'd like the length of the resulting array.
The Azure CLI help says nothing specific, and points to jmespath.org which does indeed show that a length function exists, however it seems to require an argument. I have no name for the argument, which is the root/outermost array returned by the list command.
It seems from jmespath.org that length(something) is what I want, but I don't know what to put in for the "something" part. What do I put here? Or am I going about this all wrong??
As we know az functionapp list returns a json where the root node is an array. In order to get the length of this array we can use the following syntax:
az functionapp list --query "[] | length(#)"

AWS cli. how to query snapshots and their name tags

first of all thanks for taking the time in helping me out on this one.
I have a 12300 long list of snapshots, working on deleting certain snapshots, so im trying to list them all first thru the CLI.
I want to get the SnapshotID, the StartTime, and from the tags, the 'Name'
I tried quite a few querys, but all of them result in null on the name :/
THsi is my latest one:
aws ec2 describe-snapshots --query 'Snapshots[*].{ID:SnapshotId,Time:StartTime,Name:Tags[?Key=='Name'].Value[*]}'
Is this something one can do? or should i query all Key pairs, and then filter them out with --filters ?
Few issues to be considered:
Beware of the type of quote marks around the Key Names(backticks, not single quotes)
Forcing a single value out of the tag array.
You should specify the --owner-ids otherwise all accessible snapshots will be listed (including ones that don't belong to your account)
This command works:
aws ec2 describe-snapshots --query 'Snapshots[*].{ID:SnapshotId,Time:StartTime,Name:Tags[?Key==`Name`]|[0].Value}' --owner-ids *<YOUR-ACCOUNT-ID>*

Resources