Posts in this Series
- It Begins – Azure with PowerShell I
- Accounts – Azure with PowerShell II
- Accounts Continued – Azure with PowerShell III
Before we jump back into Azure, I wanted to quickly share a graphic I put together. This image shows the number of site visitors I had to my site on Wednesday, May 25, 2022. This was the day I published the first post in this series, linked above. How encouraging! I had no idea I’d see any visitor increase due to the post.
I signed up and then signed in to Azure in the UI. Apparently, I had been there before, so it wasn’t really signing up. I wasn’t using the same credit card, but once in, I removed the older credit card and left the UI for my PowerShell console. I only looked at the Azure PowerShell Az.Billing module momentarily, but I’m guessing what I did–removing the credit card from my account–couldn’t be done in PowerShell. I’d love to be proven wrong.
Now that that’s done, let’s follow the Get started with Azure PowerShell document which begins with making a connection to Azure using the Connect-AzAccount
cmdlet. This is the third article of those written by Microsoft. You may have read the first two if you read my first post, It Begins – Azure with PowerShell I. Either way, I’ve linked all three below.
Connect-AzAccount -OutVariable AzOutput
When we invoke the above command, to somewhat of a surprise–maybe–we’re presented by a webpage from which to sign in.
Once the browser-based authentication is complete, our PowerShell host updates and provides some host-based output. As you can see in the above command, and the below image, we made use of the OutVariable
common parameter, in order to capture any output that isn’t displayed in the host program by default when this command is invoked. Notice, that there’s is no $
before the variable name when used with this parameter; PowerShell knows it’s a variable and doesn’t need any help determining that.
Let’s explore the new variable to see if it holds any other information: When we enter $AzOutput
into the PowerShell console, it only returns the four above properties we have already seen. So, naturally, let’s try more. Let’s pipe the variable to Select-Object -Property *
.
$AzOutput | Select-Object -Property *
Environments Context ------------ ------- {[AzureUSGovernment, AzureUSGovernment], [AzureCloud, AzureCloud], [AzureGermanCloud, AzureGermanCloud], [AzureChinaCloud, AzureChinaCloud]} Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext
There is definitely more here.
While we tend to stay away from the Format-*
cmdlets (as there’s not much we can do with the output once it’s been sent through a formatting cmdlet), it made sense in this instance. We want an easier way to view the output on the screen. We don’t need or want to do anything more with it. Oh! This rule is the format right rule. Formatting cmdlets, if you need to use them, should be at the rightmost of your command(s). The other half of the rule is filter left, so in total, it’s filter left, format right.
In this example, it’s much easier for us to see what we have. There are two properties contained in our $AzOutput
variable: Environments and Context.
$AzOutput | Format-List -Property *
Environments : {[AzureUSGovernment, AzureUSGovernment], [AzureCloud, AzureCloud], [AzureGermanCloud, AzureGermanCloud], [AzureChinaCloud, AzureChinaCloud]} Context : Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext
Now. let’s explore each. Once done, we’ll see that the Environments property contains information about, let’s assume environments. And, the Context property contains the properties we saw earlier in this post: the Account, the SubscriptionName, etc. But we’re not done here; we’re not about to trust this is it. I’ve been around too long.
$AzOutput.Environments Key Value --- ----- AzureUSGovernment AzureUSGovernment AzureCloud AzureCloud AzureGermanCloud AzureGermanCloud AzureChinaCloud AzureChinaCloud
$AzOutput.Context
Name Account SubscriptionName Environment TenantId ---- ------- ---------------- ----------- -------- tommymaynard@xxxxxx Free Trial AzureCloud xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
There really isn’t much more nested in our Context property, but here’s a command worth running if you’re following along: $AzOutput.Context | Select-Object -Property *
to what else is in there.
There is, however, an unreal amount of information on the Environments property. Much of this is just experience. I am coming to Azure from PowerShell, so these tactics and these processes are ingrained in me. I’m not going to show you the output of each of the below commands, but I will say this, investigate your properties, and your properties’ properties, and their properties too, and never forget there may be additional information that isn’t be displayed. Also, understand what the Get-Member cmdlet does. So it’s been said (although it’s been linked–right here and right now), its job is to return the properties and methods of PowerShell objects, which is what PowerShell has been designed to return to us by default. So you can really get in there and see what values are stored in an object(s) (properties), and what you can do with your object(s) (methods), too.
$AzOutput.Environments | Get-Member $AzOutput.Environments.Keys $AzOutput.Environments.Values $AzOutput.Environments.Values | Get-Member $AzOutput.Environments.Values | Select-Object -Property *
I think we’ll stop here for now; I had no idea I’d find so much to write about so soon. I’m not even sure if I really even got started. That said, I did notice that there are plenty of other cmdlets in the Az.Accounts PowerShell module. Using the Get-Command
command with the Module parameter, we can see them all. We can also view the online help.
Get-Command -Module Az.Accounts
CommandType Name Version Source ----------- ---- ------- ------ Alias Add-AzAccount 2.8.0 Az.Accounts Alias Get-AzDomain 2.8.0 Az.Accounts Alias Invoke-AzRest 2.8.0 Az.Accounts Alias Login-AzAccount 2.8.0 Az.Accounts Alias Logout-AzAccount 2.8.0 Az.Accounts Alias Remove-AzAccount 2.8.0 Az.Accounts Alias Resolve-Error 2.8.0 Az.Accounts Alias Save-AzProfile 2.8.0 Az.Accounts Alias Select-AzSubscription 2.8.0 Az.Accounts Cmdlet Add-AzEnvironment 2.8.0 Az.Accounts Cmdlet Clear-AzConfig 2.8.0 Az.Accounts Cmdlet Clear-AzContext 2.8.0 Az.Accounts Cmdlet Clear-AzDefault 2.8.0 Az.Accounts Cmdlet Connect-AzAccount 2.8.0 Az.Accounts Cmdlet Disable-AzContextAutosave 2.8.0 Az.Accounts Cmdlet Disable-AzDataCollection 2.8.0 Az.Accounts Cmdlet Disable-AzureRmAlias 2.8.0 Az.Accounts Cmdlet Disconnect-AzAccount 2.8.0 Az.Accounts Cmdlet Enable-AzContextAutosave 2.8.0 Az.Accounts Cmdlet Enable-AzDataCollection 2.8.0 Az.Accounts Cmdlet Enable-AzureRmAlias 2.8.0 Az.Accounts Cmdlet Get-AzAccessToken 2.8.0 Az.Accounts Cmdlet Get-AzConfig 2.8.0 Az.Accounts Cmdlet Get-AzContext 2.8.0 Az.Accounts Cmdlet Get-AzContextAutosaveSetting 2.8.0 Az.Accounts Cmdlet Get-AzDefault 2.8.0 Az.Accounts Cmdlet Get-AzEnvironment 2.8.0 Az.Accounts Cmdlet Get-AzSubscription 2.8.0 Az.Accounts Cmdlet Get-AzTenant 2.8.0 Az.Accounts Cmdlet Import-AzContext 2.8.0 Az.Accounts Cmdlet Invoke-AzRestMethod 2.8.0 Az.Accounts Cmdlet Open-AzSurveyLink 2.8.0 Az.Accounts Cmdlet Register-AzModule 2.8.0 Az.Accounts Cmdlet Remove-AzContext 2.8.0 Az.Accounts Cmdlet Remove-AzEnvironment 2.8.0 Az.Accounts Cmdlet Rename-AzContext 2.8.0 Az.Accounts Cmdlet Resolve-AzError 2.8.0 Az.Accounts Cmdlet Save-AzContext 2.8.0 Az.Accounts Cmdlet Select-AzContext 2.8.0 Az.Accounts Cmdlet Send-Feedback 2.8.0 Az.Accounts Cmdlet Set-AzContext 2.8.0 Az.Accounts Cmdlet Set-AzDefault 2.8.0 Az.Accounts Cmdlet Set-AzEnvironment 2.8.0 Az.Accounts Cmdlet Uninstall-AzureRm 2.8.0 Az.Accounts Cmdlet Update-AzConfig 2.8.0 Az.Accounts
While I don’t plan to go through each, we’ll certainly learn about what we can do with some of the Get
commands. Look at that. In our above output, we’ve returned Get-AzContext
and Get-AZEnvironment
cmdlets. Interesting, right? We’ll come back to this, but I’m guessing all the work we did exploring our output variable may not have been necessary; there may be individual cmdlets to return that data without the extra work on our part. We’ll find out in time, but still, it was a good exercise.
Get-Command -Module Az.Accounts -Verb Get
CommandType Name Version Source ----------- ---- ------- ------ Alias Get-AzDomain 2.8.0 Az.Accounts Cmdlet Get-AzAccessToken 2.8.0 Az.Accounts Cmdlet Get-AzConfig 2.8.0 Az.Accounts Cmdlet Get-AzContext 2.8.0 Az.Accounts Cmdlet Get-AzContextAutosaveSetting 2.8.0 Az.Accounts Cmdlet Get-AzDefault 2.8.0 Az.Accounts Cmdlet Get-AzEnvironment 2.8.0 Az.Accounts Cmdlet Get-AzSubscription 2.8.0 Az.Accounts Cmdlet Get-AzTenant 2.8.0 Az.Accounts
Here’s a little bonus content before we wrap up. Using the Type parameter with the Get-Command
cmdlet, we can tell PowerShell to only return a certain type of command. In this example, we return only the aliases. When I saw the full list of cmdlets for the first time, I was confused by Login-AzAccount
as we had been using Connect-AzAccount
. It turns out that Login-AzAccount
is simply an alias for Connect-AzAccount
.
Get-Command -Module Az.Accounts -Type Alias
CommandType Name Version Source ----------- ---- ------- ------ Alias Add-AzAccount 2.8.0 Az.Accounts Alias Get-AzDomain 2.8.0 Az.Accounts Alias Invoke-AzRest 2.8.0 Az.Accounts Alias Login-AzAccount 2.8.0 Az.Accounts Alias Logout-AzAccount 2.8.0 Az.Accounts Alias Remove-AzAccount 2.8.0 Az.Accounts Alias Resolve-Error 2.8.0 Az.Accounts Alias Save-AzProfile 2.8.0 Az.Accounts Alias Select-AzSubscription 2.8.0 Az.Accounts
Get-Command -Module Az.Accounts -Type Alias | ForEach-Object {$_ | Select-Object -Property @{Name='Alias';Expression={$_.DisplayName}},ResolvedCommand}
Alias ResolvedCommand ----- --------------- Add-AzAccount Connect-AzAccount Get-AzDomain Get-AzTenant Invoke-AzRest Invoke-AzRestMethod Login-AzAccount Connect-AzAccount Logout-AzAccount Disconnect-AzAccount Remove-AzAccount Disconnect-AzAccount Resolve-Error Resolve-AzError Save-AzProfile Save-AzContext Select-AzSubscription Set-AzContext