Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| azure:qnd:azure_powershell [2022/09/02 13:53] – [Loop Over All Subscriptions] mgupton | azure: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 |
| Get-AzSubscription | Get-AzSubscription | ||
| Select-AzSubscription -SubscriptionId <id> | Select-AzSubscription -SubscriptionId <id> | ||
| + | </ | ||
| + | |||
| + | ====== Command Context ====== | ||
| + | Get Azure tenants that there is an authenticated session for. | ||
| + | < | ||
| + | Get-AzTenant | select id,name | ||
| </ | </ | ||
| Line 25: | Line 31: | ||
| </ | </ | ||
| + | ==== Remove item from available context ==== | ||
| + | |||
| + | <code powershell> | ||
| + | (Get-AzContext -ListAvailable).Name | ||
| + | </ | ||
| + | |||
| + | <code powershell> | ||
| + | Remove-Context -Name <context name> | ||
| + | </ | ||
| + | |||
| + | ====== Get Resources ====== | ||
| + | List all resources in the current subscription. | ||
| + | <code powershell> | ||
| + | Get-AzResource | ||
| + | </ | ||
| + | |||
| + | <code powershell> | ||
| + | Get-AzResource | select Name, | ||
| + | </ | ||
| ====== Loop Over All Subscriptions ====== | ====== Loop Over All Subscriptions ====== | ||
| Line 46: | Line 71: | ||
| <code powershell> | <code powershell> | ||
| foreach ($sub in Get-AzSubscription -TenantId " | foreach ($sub in Get-AzSubscription -TenantId " | ||
| - | Set-AzContext -Subscription $sub.id | + | Set-AzContext -Subscription $sub.id |
| # | # | ||
| # Other commands | # Other commands | ||
| Line 52: | Line 77: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | |||
| + | <code powershell> | ||
| + | Set-AzContext -Tenant " | ||
| + | |||
| + | foreach ($sub in Get-AzSubscription) { | ||
| + | Set-AzContext -Subscription $sub.id | Out-Null | ||
| + | # | ||
| + | # Other commands | ||
| + | # | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ====== Loop Over Resource Groups ====== | ||
| + | <code powershell> | ||
| + | |||
| + | Get-AzResourceGroup | foreach { | ||
| + | $_.ResourceGroupName | ||
| + | # | ||
| + | # Do something with RG info | ||
| + | # | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ====== List Policy Assignments ====== | ||
| + | <code powershell> | ||
| + | Set-AzContext -Tenant " | ||
| + | |||
| + | foreach ($sub in Get-AzSubscription) { | ||
| + | Set-AzContext -Subscription $sub.Id | ||
| + | $p += Get-AzPolicyAssignment | ||
| + | } | ||
| + | |||
| + | $p | select -ExpandProperty Properties | select DisplayName, | ||
| + | </ | ||
| + | |||
| + | ====== List All Extension on VMs ====== | ||
| + | <code powershell> | ||
| + | function AvGet-VMExtensions ($TenantId) { | ||
| + | $extList = @() | ||
| + | foreach ($sub in Get-AzSubscription -TenantId $TenantId) { | ||
| + | Get-AzVM | foreach { | ||
| + | $vm = $_.Name | ||
| + | $ext = Get-AzVMExtension -VMName $_.Name -ResourceGroupName $_.ResourceGroupName | ||
| + | $ext | foreach { | ||
| + | $obj = [PSCustomObject]@{ | ||
| + | Name = $vm | ||
| + | ExtName = $_.Name | ||
| + | } | ||
| + | $extList += $obj | ||
| + | } | ||
| + | $extList | Format-Table -AutoSize | ||
| + | } | ||
| + | $extList | Tee-Object $env: | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ====== List Subnets with no associated NSG ====== | ||
| + | <code powershell> | ||
| + | foreach ($sub in Get-AzSubscription -TenantId " | ||
| + | Set-AzContext -Subscription $sub.id | Out-Null | ||
| + | |||
| + | (Get-AzVirtualNetwork | foreach {$_.Subnets} | where {$_.NetworkSecurityGroup -eq $null}).Name | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| ====== Enumerating Resources ====== | ====== Enumerating Resources ====== | ||
| Line 67: | Line 161: | ||
| Get-AzResourceGroup | foreach {Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName} | Get-AzResourceGroup | foreach {Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName} | ||
| </ | </ | ||
| + | |||
| + | ====== Related ====== | ||
| + | * [[: | ||