Azure PowerShell

Home / Azure PowerShell

Azure PowerShell

July 31, 2019 | Azure, PowerShell | 1 Comment

I was thinking of creating some PowerShell scripts to take care of some repetitive tasks when using the UI, normal thoughts when your wanting to script something out.

Microsoft has a new Az Module that is replacing the AzureRM Module, I wanted to make sure I was going to be using the newest module so the script commands would be supported longer under the new module.

I’ve written a few snip it scripts using the AzureRM, I wanted to take a simple one and migrate it to Az.

My old script had a basic task of doing a query look up. It logged, it ran the query and saved output. It looked something like this:

###### Old Script
Login-AzureRMaccount
$MyQuery = 'LongQueryHere-blah-blah-blah | where TimeGenerated between(datetime("2019-07-24 17:37:30.999") .. datetime("2019-07-25 17:37:32.999"))'

$MyQueryResponce = Invoke-AzureRmOperationalInsightsQuery -WorkspaceID "0e000e00-0aa0-0ee0-a0aa-0000000000ee" -Query $MyQuery

Write-Output $MyQueryResponce.Results > C:\Folder\MyFileNameHere.txt

Now there are quite a few pages that help you get started on the Microsoft website in their documentation. Started on this page and made my way around the links:

https://docs.microsoft.com/en-us/powershell/azure/overview?view=azps-2.4.0

When I’m doing research like this, I have a habit of opening a new tab if the page I’m on has good information but is also linking over to another page. To give you an idea about how much looking around I did, after reading the pages and completing my migration of this script I had about 10 tabs still open.

Now I personally had no luck uninstalling the old AzureRM as recommended. I following the steps and tried a few different ways but kept getting access denied errors (even when running as admin)

I ended up skipping the uninstall and knew I would just need to avoid using the commands from AzureRM.

#Azure PowerShell needs PowerShell 5.1 or higher on Windows 
# (or 6.0 on other platforms)
$PSVersionTable.PSVersion

#Install Az
Install-Module -Name Az -AllowClobber

After trying to uninstall AzureRM, making sure I had the right versions installed and installing the Az Module. I started off on the first part:

Logging into Azure. This was simple and strait forward.

Connect-AzAccount

Boom. Easy. Just as easy as logging into AzureRM. I even found how to login to other Azure environments, such as AzureChinaCloud and AzureUSGovernment.

Connect-AzAccount -Environment AzureUSGovernment

Now the next step was to find the new matching OperationalInsightsQuery command. Microsoft had a sample on how to do this:

Get-Command -Verb Get -Noun AzVM* -Module Az.Compute

This returned 30 commands at the time. There was not really any notes on how to use this command, but after looking at the list and then back at the command I could see this was all the command for AzVM in the Az.Compute, it did take a couple tries to notice the Az.Compute was filtering more then what I wanted to filter so I changed the command to return all AZ items… and I quickly saw why they filtered… it returned over 2500 commands.

 (Get-Command -Noun Az* -Module Az.*).count

Re-filtering my command and it became clear that the new command was almost the same as the last one.

Get-Command -Verb Invoke -Noun *Query*

Name
----
Invoke-AzOperationalInsightsQuery
Invoke-AzureRmOperationalInsightsQuery

There is my old command and my new command. This was a straight forward fix, replacing just a couple commands. Ran the script and it worked.

### New Script
Connect-AzAccount

$MyQuery = 'LongQueryHere-blah-blah-blah | where TimeGenerated between(datetime("2019-07-24 17:37:30.999") .. datetime("2019-07-25 17:37:32.999"))'

$MyQueryResponce = Invoke-AzOperationalInsightsQuery -WorkspaceID "0e000e00-0aa0-0ee0-a0aa-0000000000ee" -Query $MyQuery

Write-Output $MyQueryResponce.Results > C:\Folder\MyFileNameHere.txt

Over all it was not bad. The hard part is finding the commands you want to run and then knowing how to use them, but in my case it was much of the same. I reviewed the breaking changes list and it did not look to bad from my usage. I like naming over the old way, and so far it’s working smoothly.

,

About Author

1 Comment
  1. Jop Gommans

    Great stuff, thanks for the explanation on the conversion of the commands. Was just getting started with this and this is exactly what I needed!

Leave a Reply

Your email address will not be published. Required fields are marked *