Azure Powershell Commands
--
Here let's explore different PowerShell commands to manage the Azure platform.
1. Creating Variables in Powershell
It can define a variable in PowerShell using the $ symbol followed by the name of the variable. In the below example, the variable $myfirstvar
is assigned with the string “Hello World”
$myfirstvar = "Hello World"
Going forward, we will see the different variable assignments as part of different azure operations.
Splatting
Splatting is a PowerShell feature that allows you to bundle up the parameters you want to supply to a command and pass them in a table, rather than one long line of code. With PowerShell splatting, your commands can be easier to read and navigate.
Lets see an example of this,
Copy-Item -Path "test.txt" -Destination "test2.txt" -WhatIf -Force
The above example shows copying a text file from source to destination. Here with the Copy-Item command, it passes three parameters Path, Destination, and WhatIf. If there are more parameters, then you want to add it with the command. Here the problem is as the parameter increases, readability become more difficult and the user cant reuse a set of parameter for another command. Splatting can solve these issues. Using splitting, it can implement the above command as follows.
$HashArguments = @{
Path = "test.txt"
Destination = "test2.txt"
WhatIf = $true
}
Copy-Item @HashArguments
The first command creates a hash table of parameter-name and parameter-value pairs and stores it in the $HashArguments
variable. The second command uses the $HashArguments
variable in the command with splatting. The At symbol (@HashArguments
) replaces the dollar sign ($HashArguments
) in the command.
2. Connecting to the Azure Account
The command Connect-AzAccount
is used to connect your PowerShell console with the azure account. Once the user invokes this command, the the user can access different resources under his account. Users can pass different parameters as part of Connect-AzAccount
command. Below shows the parameters supported by the command.