azure:qnd:azure_powershell

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
azure:qnd:azure_powershell [2022/09/13 11:58] – [List All Extension on VMs] mguptonazure:qnd:azure_powershell [2022/10/21 21:58] (current) – [Get Resources] mgupton
Line 4: Line 4:
 Authenticate with Azure, list subscriptions for tenants and select a subscription to work with. Authenticate with Azure, list subscriptions for tenants and select a subscription to work with.
 <code powershell> <code powershell>
-Connect-AzAccount+Connect-AzAccount -Tenant <tenant id>
  
 Get-AzSubscription Get-AzSubscription
  
 Select-AzSubscription -SubscriptionId <id> Select-AzSubscription -SubscriptionId <id>
 +</code>
 +
 +====== Command Context ======
 +Get Azure tenants that there is an authenticated session for.
 +<code>
 +Get-AzTenant | select id,name
 </code> </code>
  
Line 25: Line 31:
 </code> </code>
  
 +==== Remove item from available context ====
 +
 +<code powershell>
 +(Get-AzContext -ListAvailable).Name
 +</code>
 +
 +<code powershell>
 +Remove-Context -Name <context name>
 +</code>
 +
 +====== Get Resources ======
 +List all resources in the current subscription.
 +<code powershell>
 +Get-AzResource
 +</code>
 +
 +<code powershell>
 +Get-AzResource | select Name,Type,ResourceGroupName,SubscriptionId | Export-Csv resources.csv
 +</code>
  
 ====== Loop Over All Subscriptions ====== ====== Loop Over All Subscriptions ======
Line 46: Line 71:
 <code powershell> <code powershell>
 foreach ($sub in Get-AzSubscription -TenantId "xyz") { foreach ($sub in Get-AzSubscription -TenantId "xyz") {
-    Set-AzContext -Subscription $sub.id+    Set-AzContext -Subscription $sub.id | Out-Null
     #     #
     # Other commands     # Other commands
Line 52: Line 77:
 } }
 </code> </code>
 +
 +
 +<code powershell>
 +Set-AzContext -Tenant "xyz"
 +
 +foreach ($sub in Get-AzSubscription) {
 +    Set-AzContext -Subscription $sub.id | Out-Null
 +    #
 +    # Other commands
 +    #
 +}
 +</code>
 +
 ====== Loop Over Resource Groups ====== ====== Loop Over Resource Groups ======
 <code powershell> <code powershell>
Line 57: Line 95:
 Get-AzResourceGroup | foreach { Get-AzResourceGroup | foreach {
   $_.ResourceGroupName   $_.ResourceGroupName
 +  #
 +  # Do something with RG info
 +  #
 } }
 +</code>
 +
 +====== List Policy Assignments ======
 +<code powershell>
 +Set-AzContext -Tenant "xxxx…"
 +
 +foreach ($sub in Get-AzSubscription) {
 +    Set-AzContext -Subscription $sub.Id
 +    $p += Get-AzPolicyAssignment
 +}
 +
 +$p | select -ExpandProperty Properties | select DisplayName,Scope
 </code> </code>
  
Line 81: Line 134:
 } }
 </code> </code>
 +
 +====== List Subnets with no associated NSG ======
 +<code powershell>
 +foreach ($sub in Get-AzSubscription -TenantId "xxxx") {
 +    Set-AzContext -Subscription $sub.id | Out-Null
 +
 +    (Get-AzVirtualNetwork | foreach {$_.Subnets} | where {$_.NetworkSecurityGroup -eq $null}).Name
 +
 +}
 +</code>
 +
  
 ====== Enumerating Resources ====== ====== Enumerating Resources ======
Line 97: Line 161:
 Get-AzResourceGroup | foreach {Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName} Get-AzResourceGroup | foreach {Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName}
 </code> </code>
 +
 +====== Related ======
 +  * [[:powershell|Powershell]]
  
  • azure/qnd/azure_powershell.1663070330.txt.gz
  • Last modified: 2022/09/13 11:58
  • by mgupton