public/Set-AzureResourceTags.ps1


function Set-AzureResourceTags {

    [cmdletbinding()]

    Param (

        [parameter(Mandatory=$true,
            ValueFromPipeline=$true)]
        [string]$jsonobject
    
    )

    Process 
    {

        # check to see if local token exists (ran Login-AzureRMAccount)
        if (($null -eq (Get-AzureRmContext).Account)) {
            Write-Warning "Please run < Login-AzureRMAccount > first to create a session token...exiting."
            break
        } 
    
        # convert json literal string to system.object
        Try {
            $targetresources = $jsonobject | ConvertFrom-Json -ErrorAction Stop -Verbose
        }
        Catch {
            $error[0].Exception
            break
        }

        $currentsubscription = (Get-AzureRmContext).subscription.Name

        if (($null -eq $currentsubscription)) {
            Write-Warning "No subscription selected. Run Select-AzureRMSubscription to target a specific subscription, before running this tool again."
            break
        }
        else {
            Write-Verbose "Current subscription is '$currentsubscription'. "
        }

        # get resources in current subscription and reconcile with input data
        Try {
            $resources = Get-AzureRmResource -ErrorAction Stop -Verbose
        }
        Catch {
            $error[0].Exception
            break
        }
        
        $resourcesnotfound = @()
        $foundresources = @()
        $targetresources | ForEach-Object {

        Write-Debug "validating resource name $($_.'resource name')"
        
            if ("$($_.'Resource Name')" -in $resources.name) {
                Write-Verbose "Found $($_.'Resource Name') in current subscription's resources"
                $foundresources += $_
            }
            elseif ($_."resource name" -notin $resources.name) {
                Write-Warning "$($_.'Resource Name') not found in current subscription's resource list"
                $resourcesnotfound += $_
            }
            else {
                Write-Warning "resource reconciliation condition failed"
                break
            }    
        } # end foreach jsonobject loop

        foreach ($resource in $foundresources) {
        
            $totag = $resources | Where-Object {$_.name -eq "$($resource.'Resource Name')"}

            Try {
                
                $parameters = @{
                    ResourceGroupName = "$($totag.resourcegroupname)";
                    ResourceName      = "$($totag.resourcename)";
                    ResourceType      = "$($totag.resourcetype)";
                    Tags              = @{
                                            'Resource Name' = "$($resource.'Resource Name')"
                                            'Resource Type' = "$($resource.'Resource Type')"
                                            'Resource Class' = "$($resource.'Resource Class')"
                                            'Resource Role' = "$($resource.'Resource Role')"
                                            'Cost Center' = "$($resource.'Cost Center')"
                                            'Business Value' = "$($resource.'Business Value')"
                                            'Customer' = "$($_.'Customer')"
                                            'Primary Contact' = "$($_.'Primary Contact')"
                                            'Notes' = "$($_.'notes')"
                                        }
                }
                
                $tagoperation = Set-AzureRmResource @parameters -ErrorAction Stop -Verbose -Force
            }
            Catch {
                $error[0].Exception
                break
            }
        }

        if (($resourcesnotfound.Count -ne 0)) {
            Write-Warning "Some resources were not found in the current subscription..."
            return $resourcesnotfound
        }
        else {
            Write-Verbose "All resources accounted for in current subscription."
        }


    } # end process block

} # end Set-AzureResourceTags function