Azure Migrate
A central hub of Azure cloud migration services and tools to discover, assess, and migrate workloads to the cloud.
818 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I've been tasked to develop a script, or automate via other means (ARM template and pipeline for example).
The automation must do the following:
I'm certain there is very likely a PowerShell script or a template and pipeline out there that can achieve the requirement, but I've yet to find one.
I have started working on a basic PowerShell script (below) to get the nuts and bolts in there while testing the latest AZ commands, and it works, but now I must build the logic in the script to automate #'s 5 - 7 above.
#Prompts User for AZ Tenant and Subscription ID's
$TenantID = Read-Host "Please Enter the Tenant ID"
$SubId = Read-Host "Please Enter the Subscription ID"
#Authenticates User to Azure
Connect-AzAccount -TenantId $TenantID
Set-AzContext -Subscription $SubId
#Prompts User for RG Name and Creates Log File
$RGName = Read-Host "Please Enter the Name of the Resource Group"
$LogFile = "c:\temp\pslogfiles\"+$RGName+".txt"
#Gets All Resources in the RG and Appends Data in the Log File
Get-AzResource -ResourceGroupName $RGName | select Name,Type,Location,Tags,Id | Out-File $LogFile
#Creates Tags in the Resource Group and Assigns Tags to the Designated Resource
$ResourceId = Read-Host "Open the Log File Created in c:\temp\pslogfiles and Copy & Paste the Full Id of the Resource You Wish to Edit"
$TagName = Read-Host "Please Enter the Tag Name"
$TagValue = Read-Host "Please Enter the Tag Value"
$Tag = @{"$TagName"="$TagValue"}
Set-AzResourceGroup -Name $RGName -Tag $Tag
New-AzTag -ResourceId $ResourceId -Tag $Tag
Thank you!
Try this:
# Prompt for Azure subscription ID (or Name)
$subscriptionId = Read-Host "Please enter the Azure subscription ID or Name"
# Prompt for Azure credentials
$azureCredentials = Get-Credential -Message "Please enter your Azure credentials"
# Set Azure context
Connect-AzAccount -Credential $azureCredentials -Subscription $subscriptionId | Out-Null
# Set the Tag Name and Tag Value
$tagName = Read-Host "Please enter the Tag Name"
$tagValue = Read-Host "Please enter the Tag Value"
# Get a list of all Resource Groups in that Subscription and export that list to a text file
$rgList = Get-AzResourceGroup | Select-Object ResourceGroupName | Out-File -FilePath "ResourceGroups.txt"
# For each Resource Group listed in the text file, get the resource inventory in that RG, and export that RG's inventory into a separate text file which will list each resource by Name, Type, Location, Tags
foreach ($rg in Get-Content "ResourceGroups.txt") {
$rgInventory = Get-AzResource -ResourceGroupName $rg | Select-Object Name, Type, Location, Tags | Out-File -FilePath "$rg-Inventory.txt"
# For each inventory item (resource) in the text file, check the Tags property and either create the Tag Name and Value (noted in #3 above) OR if it does not match the Name and value in #3, update the Tag
foreach ($resource in Get-Content "$rg-Inventory.txt") {
$resourceTags = ($resource | ConvertFrom-Json).Tags
if ($resourceTags.ContainsKey($tagName) -and $resourceTags[$tagName] -eq $tagValue) {
Write-Host "Resource $resource already has the correct tag"
}
else {
$resourceId = ($resource | ConvertFrom-Json).ResourceId
$newTag = @{ $tagName = $tagValue }
Set-AzResource -ResourceId $resourceId -Tag $newTag
Write-Host "Tag added to resource $resource"
}
}
}