Difference between revisions of "Azure/az-cli"

From Ever changing code
Jump to navigation Jump to search
 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Install az PowerShell module =
= Install Az PowerShell module =
Note <tt>az</tt> replaced <tt>AzureRM</tt> that is backwards compatible but both shouldn't be installed at the same time. [https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azurermps-6.12.0 Official installation guide] might come here very handy. Otherwise follow quick notes:
Note <tt>az</tt> replaced <tt>AzureRM</tt> that is backwards compatible but both shouldn't be installed at the same time. [https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azurermps-6.12.0 Official installation guide] might come here very handy. Otherwise follow quick steps:


<source lang="PS">
 
### Install
Install Az module and login to Azure cloud
$PSVersionTable.PSVersion #check required version PowerShell 5.x or 6.x
<syntaxhighlightjs lang="PowerShell">
Install-Module -Name Az -AllowClobber #admin rights required
# Verify
### Sign in
C:\> $PSVersionTable.PSVersion #check required version PowerShell 5.x or 6.x
# Import the module into the PowerShell session
C:\> Get-InstalledModule -Name Az -AllVersions | select Name,Version
Import-Module Az
Name Version
# Connect to Azure with a browser sign in token
---- -------
Connect-AzAccount
Az  4.4.0
WARNING: To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code DUNAAABBB
 
# Install and import
C:\> Install-Module -Name Az -AllowClobber # admin rights required
C:\> Import-Module Az  # Import the module into the PowerShell session
C:\> Get-Command *az*  # List all available PShell commands
 
# Login
C:\> Connect-AzAccount # Connect to Azure with a browser sign in token
WARNING: To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code DAAAAAAAA
to authenticate.
to authenticate.


Line 19: Line 27:
TenantId        : bbbbbbbb-cccc-dddd-eeee-ffffffffffff
TenantId        : bbbbbbbb-cccc-dddd-eeee-ffffffffffff
Environment      : AzureCloud
Environment      : AzureCloud
</source>


= Get a list of all subnets =
# Update module
<source lang="bash">
C:\> Update-Module -Name Az
#!/bin/bash
</syntaxhighlightjs>
for ResourceGroup in $(az group list -o tsv | cut -f4); do
 
    for VNET in $(az network vnet list --resource-group $ResourceGroup -o tsv | cut -f9); do
== Using the module commands to manage resources ==
        echo "### ResourceGroup: $ResourceGroup VNET: $VNET"
<syntaxhighlightjs lang="PowerShell">
        az network vnet subnet list --resource-group $ResourceGroup --vnet-name $VNET -o tsv | cut -f1,9,13 | column -t
PS> help New-AzVM
     done
PS> New-AzVM -Name test-1 -ResourceGroupName 1-149a69-playground-sandbox
done
cmdlet New-AzVM at command pipeline position 1
</source>
Supply values for the following parameters:
Credential
User: devops
Password for user devops: **********
 
PS> Get-AzVM
ResourceGroupName  Name  Location VmSize          OsType    NIC    ProvisioningState Zone
-----------------  ----  -------- ------          ------    ---    ----------------- ----
SANDBOX            test-1 westus  Standard_DS1_v2 Windows  test-1 Succeeded 
</syntaxhighlightjs>
 
== Install on [https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-apt?view=azure-cli-latest Linux] ==
Worked on U20.04
<syntaxhighlightjs lang="bash">
sudo apt-get update
sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg
 
curl -sL https://packages.microsoft.com/keys/microsoft.asc |
    gpg --dearmor |
     sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null
 
AZ_REPO=$(lsb_release -cs)
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" |
    sudo tee /etc/apt/sources.list.d/azure-cli.list


= Resources =
sudo apt-get update
* [https://docs.microsoft.com/en-us/azure/cloud-shell/quickstart Install Azure Cloud Bash shell]
sudo apt-get install azure-cli


[[Category:azure]] [[Category:windows]]
az login
</syntaxhighlightjs>

Latest revision as of 23:49, 26 July 2020

Install Az PowerShell module

Note az replaced AzureRM that is backwards compatible but both shouldn't be installed at the same time. Official installation guide might come here very handy. Otherwise follow quick steps:


Install Az module and login to Azure cloud <syntaxhighlightjs lang="PowerShell">

  1. Verify

C:\> $PSVersionTable.PSVersion #check required version PowerShell 5.x or 6.x C:\> Get-InstalledModule -Name Az -AllVersions | select Name,Version Name Version


-------

Az 4.4.0

  1. Install and import

C:\> Install-Module -Name Az -AllowClobber # admin rights required C:\> Import-Module Az # Import the module into the PowerShell session C:\> Get-Command *az* # List all available PShell commands

  1. Login

C:\> Connect-AzAccount # Connect to Azure with a browser sign in token WARNING: To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code DAAAAAAAA to authenticate.

Account  : john@example.com SubscriptionName : Microsoft Azure Sponsorship SubscriptionId  : aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee TenantId  : bbbbbbbb-cccc-dddd-eeee-ffffffffffff Environment  : AzureCloud

  1. Update module

C:\> Update-Module -Name Az </syntaxhighlightjs>

Using the module commands to manage resources

<syntaxhighlightjs lang="PowerShell"> PS> help New-AzVM PS> New-AzVM -Name test-1 -ResourceGroupName 1-149a69-playground-sandbox cmdlet New-AzVM at command pipeline position 1 Supply values for the following parameters: Credential User: devops Password for user devops: **********

PS> Get-AzVM ResourceGroupName Name Location VmSize OsType NIC ProvisioningState Zone


---- -------- ------ ------ --- ----------------- ----

SANDBOX test-1 westus Standard_DS1_v2 Windows test-1 Succeeded </syntaxhighlightjs>

Install on Linux

Worked on U20.04 <syntaxhighlightjs lang="bash"> sudo apt-get update sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg

curl -sL https://packages.microsoft.com/keys/microsoft.asc |

   gpg --dearmor |
   sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null

AZ_REPO=$(lsb_release -cs) echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" |

   sudo tee /etc/apt/sources.list.d/azure-cli.list

sudo apt-get update sudo apt-get install azure-cli

az login </syntaxhighlightjs>