We have a command to get all the subscriptions Get-AzSubscription | Set-Azcontext and set for that to work with multiple subscription. How do we fetch specific subscriptions from Get-AzSubscription and pass it for Set-Azcontext.
In order to do that I have tried below :
I have tried something $subscription = Get-AzSubscription -TenantId "tenant id" | where-object{$_.Name -like 'required name*'}
$subscription | Set-AzContext
Is there any better way to do the same?
Regards
Deb
There is nothing wrong with your method as long as:
You can easily write the filter clause to include all the required subscriptions. If there is no consistent naming scheme you might end up with lots of ... -or ... -or ... clauses which is messy.
You are happy that your script will automatically start to act on any new subscriptions added to your tenancy which match your filter clause.
If however:
You don't have good naming scheme so it's difficult to write a simple filter.
The script must act only on the specified subscriptions.
Then you could do this:
$subs = #('Sub 1','Live Sub','2 Test','Deb')
$subs | get-azsubscription -SubscriptionName {$_} | set-azcontext
You can use the following command to select the particular subscription you want to use :
Select-AzureRmSubscription -SubscriptionId xxxxx-xxxxx-xxxxxx-xxxx
Related
My PowerShell is VERY rusty, so please bear with me. I've been tasked to bulk tag Azure Resources based on CSV data, specifically Azure VM's. In this CSV are 3 headers (VMName, TagName, TagValue). I've tried to automate this task with PowerShell and no matter how I format the code, I keep falling short. Can someone help me clear this up or perhaps point me in the direction of a known working PS script that will help me accomplish this?
`
Connect-AzAccount -TenantId ''
Set-AzContext -Subscription ''
$Import = Import-Csv -Path '...\Tags.csv' |
ForEach-Object {
$_.psobject.properties |
ForEach-Object {Set-Variable -Name $_.Name -Value $_.Tags}
foreach ($Name in $Import) {
$Tag = $_.Name.Tags
$Tag.Add($_.Tags)
Set-AzResource -ResourceId $Name.ResourceId -Tag -Force
}
}
`
I've tried a hash table and a fully customized script. It either only applies the Tag Name and not the Tag Value, and it needs both, or it shoots off error after error. Microsoft seems to want bulk tagging at the subscription and resource group level, so it's a bit difficult to get this right specific to resources. In the end, I want the script to read the server name in Row 1 Column 1, find that resource in Azure, and create the Tag using the Tag Name (Row 1 Column 2) and Tag Value (Row 1 Column 3).
After reproducing from my end, I could able to get this work using New-AzTag. Below is the complete script that worked for me.
$Import = Import-Csv -Path 'Tags.csv'
foreach($I in $Import) {
$Resource = Get-AzResource -Name $I.VMName
$TagName=$I.TagName
$TagValue=$I.TagValue
New-AzTag -ResourceId $Resource.ResourceId -Tag #{"$TagName"="$TagValue"}
}
RESULTS:
Tags.csv
In portal
Is there a way to fetch all details of routing from each subscription with Route-Table Name, subscriptions, next hop type address prefix.
I have tried 'Get-AzRouteTable -ResourceGroupName "" -Name "prod" | Get-AzRouteConfig | Export-Csv azureroutetable2.csv' but getting only from specific environment, is there a better way to do the same?
Regards
Devbrat
According to the help for Get-AzRouteTable it doesn't require any value to be specified for ResourceGroupName so we should just be able to loop through your available contexts. If you did have a large Azure Network infrastructure and they were logically organised by Resource Group, you may want to consider looping through Resource Groups too.
That being said, you can do something like this. It will create a csv for each subscription in your current session. Change the . at the beginning of the path value to set a full path if you would like.
$Contexts = Get-AzContext -ListAvailable
foreach ($Context in $Contexts) {
[void](Set-AzContext -SubscriptionId $Context.Subscription.Id)
$RouteConfig = Get-AzRouteTable -Name "prod" | Get-AzRouteConfig
# If you are using PowerShell 5.1, add '-NoTypeInformation' to the end of this command.
$RouteConfig | Export-Csv -Path ".\$($Context.Subscription.Name)_Routes.csv" -Encoding utf8 -NoClobber
}
I have a directory that has about 200k users in it. I need to pull back all the users that came from on-prem AD. This of course would exclude the multiple source guest users. I have to use source because not all users have a usertype.
If there was a way to search Department not equal to null that would work as well, but it doesn't appear to be part of the odata filter standard.
Get-AzureADUser -Filter "Department eq ''" | select DisplayName,`
UserPrincipalName,Mail,creationType,AccountEnabled,Department
To get users that come from on-prem AD you could do something like this
Get-AzureADUser -Filter "dirSyncEnabled eq true"
For selecting only a few, other operators like top can be used as well.. e.g.
Get-AzureADUser -top 5 -Filter "dirSyncEnabled eq true"
To get all users one shot you can do
Get-AzureADUser -All $true -Filter "dirSyncEnabled eq true"
or
Get-AzureADUser -all $true | where-object -property DirSyncEnabled -eq "True"
I'm new to powershell and azure and need to export all the LocalNetworkGateway information from multiple Subscriptions and Resource Groups.
I have a script to export from Resource Groups but I have to manually enter the ResourceGroupName for each one.
Is there a way to have a variable that contains all the ResourceGroupNames so that I don't have to run the script 40 times and manually enter a different ResourceGroupName for each?
Any help would be gratefully received.
I have code for one Resource Group at a time.
Get-AzLocalNetworkGateway -ResourceGroupName “RGName” | Export-Csv -Path "c:\Azure\LocalNetworkGateway.csv"
you can just iterate over resource groups:
$resourceGroups = Get-AzResourceGroup
$resourceGroups.foreach{
Get-AzLocalNetworkGateway -ResourceGroupName $_.ResourceGroupName |
Export-Csv -Path "c:\Azure\LocalNetworkGateway.csv" -Append
}
I have "billing reader" access to several hundred subscriptions in an EA.
I'm trying to get a list of virtual machines and their sizes across all subscriptions.
So currently when I run a "Get-AzureRMSubscription" it shows me all the subscriptions (hundreds of them), but i'm not sure how to actually run a script against all the subscriptions?
Would be great to get a "Get-AzureRMVM" across them all
Any suggestions?
Thanks in advance!
You can possibly do something like this:
$azureSubs = Get-AzureRMSubscription
$azureSubs | ForEach-Object {Select-AzureRMSubscription $_ | Out-Null; Get-AzureRMVM -WarningAction SilentlyContinue}
You are essentially setting an array variable to hold all your Azure Subscription and piping it to the ForEach-Object cmdlet to iterate all of the objects in the array. Then you pipe it to the Get-AzureRMVM cmdlet to list all VMs in each subscription.
This is definitely not optimized for performance and there might be better solutions out there, but at least you can run it and forget it.
The reason for the Out-Null and -WarningAction is to suppress the outputs you do not need.
You didn't ask but for classic resources we have the following script run on a regular basis and its output stored in a SQL Database.
$subscriptions = Get-AzureSubscription
foreach ($sub in $subscriptions)
{
$sub | Select-AzureSubscription
Get-AzureService | % {
Get-AzureDeployment -ServiceName $_.ServiceName
} | % {
New-Object -TypeName 'PSObject' -Property #{ 'ServiceName' = $_.ServiceName; 'Addresses' = $_.VirtualIPs.Address; }
} | sort Addresses | ft
}
% is ForEach-Object, ft is Format-Table although some kind souls may come along and try to edit this and make it harder to reuse. You can add/remove properties in the select statement to tailor your output as needed. Try it in one subscription to refined your needs, then create a script to make it easy to reuse.
We recently released Azure Resource Graph to support these types of searches across multiple subscriptions. See documentation here https://learn.microsoft.com/en-us/azure/governance/resource-graph/overview