locked
Selecting multiple Subscriptions in a Tenant, looping powershell script RRS feed

  • Question

  •  Hi


    I have a script below, you can see  Select-AzureRmSubscription -SubscriptionName "Subscription NAme", i have multiple subscriptions and need it to go through many subscriptions, how do i do this please??

    Also how do i add the subscription name in the OUTPUT?  csv and html please


    #Adding Azure Account and Subscription
    Login-AzureRmAccount

     #Selecting the Azure Subscription
     Select-AzureRmSubscription -SubscriptionName "Subscription NAme"


     #Getting all Azure Resources
     $resources = Get-AzureRmResource

     Function Set-AlternatingRows {
        <#
        .SYNOPSIS
            Simple function to alternate the row colors in an HTML table
        .DESCRIPTION
            This function accepts pipeline input from ConvertTo-HTML or any
            string with HTML in it.  It will then search for <tr> and replace
            it with <tr class=(something)>.  With the combination of CSS it
            can set alternating colors on table rows.

            CSS requirements:
            .odd  { background-color:#ffffff; }
            .even { background-color:#dddddd; }

            Classnames can be anything and are configurable when executing the
            function.  Colors can, of course, be set to your preference.

            This function does not add CSS to your report, so you must provide
            the style sheet, typically part of the ConvertTo-HTML cmdlet using
            the -Head parameter.
        .PARAMETER Line
            String containing the HTML line, typically piped in through the
            pipeline.
        .PARAMETER CSSEvenClass
            Define which CSS class is your "even" row and color.
        .PARAMETER CSSOddClass
            Define which CSS class is your "odd" row and color.
        .EXAMPLE $Report | ConvertTo-HTML -Head $Header | Set-AlternateRows -CSSEvenClass even -CSSOddClass odd | Out-File HTMLReport.html

            $Header can be defined with a here-string as:
            $Header = @"
            <style>
            TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
            TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
            TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
            .odd  { background-color:#ffffff; }
            .even { background-color:#dddddd; }
            </style>
            "@

            This will produce a table with alternating white and grey rows.  Custom CSS
            is defined in the $Header string and included with the table thanks to the -Head
            parameter in ConvertTo-HTML.
        .NOTES

            Changelog:
                1.1         Modified replace to include the <td> tag, as it was changing the class
                            for the TH row as well.
                1.0         Initial function release
        .LINK
            http://community.spiceworks.com/scripts/show/1745-set-alternatingrows-function-modify-your-html-table-to-have-alternating-row-colors
        .LINK
            http://thesurlyadmin.com/2013/01/21/how-to-create-html-reports/
        #>
        [CmdletBinding()]
           Param(
               [Parameter(Mandatory,ValueFromPipeline)]
            [string]$Line,

               [Parameter(Mandatory)]
               [string]$CSSEvenClass,

            [Parameter(Mandatory)]
               [string]$CSSOddClass
           )
        Begin {
            $ClassName = $CSSEvenClass
        }
        Process {
            If ($Line.Contains("<tr><td>"))
            {    $Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
                If ($ClassName -eq $CSSEvenClass)
                {    $ClassName = $CSSOddClass
                }
                Else
                {    $ClassName = $CSSEvenClass
                }
            }
            Return $Line
        }
    }


    #Declaring Variables
    $results = @()
    $TagsAsString = "KeepAlive"


    $Header = @"
    <style>
    TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
    TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
    TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
    .odd  { background-color:#ffffff; }
    .even { background-color:#dddddd; }
    </style>
    <title>
    KeepAlive Tag Report
    </title>
    "@
    $Pre = "KeepAlive Tag Report"
    $Post = "KeepAlive Tag Report"



     foreach($resource in $resources)
     {
         #Fetching Tags
         $Tags = $resource.Tags

         #Checking if tags is null or have value
         if($Tags -ne $null)
         {
             foreach($Tag in $Tags)
             {
                 $TagsAsString += $Tag.Name #+ ":" + $Tag.Value + ";"
             }
         }
         #else
         #{
            # $TagsAsString = "NULL"
         #//}


         #Adding to Results
         $details = @{             
                     Tags = $TagsAsString
                     Name = $resource.Name
                     ResourceId = $resource.ResourceId
                     ResourceName = $resource.ResourceName
                     ResourceType = $resource.ResourceType
                     ResourceGroupName =$resource.ResourceGroupName
                     Location = $resource.Location
                     SubscriptionId = $resource.SubscriptionId  
                     Sku = $resource.Sku
             }                            
             $results += New-Object PSObject -Property $details  


         #Clearing Variable
         $TagsAsString = ""
     }


    $results | where {$_.Tags -eq "KeepAlive"} | ConvertTo-Html -property Name,Tags,ResourceGroupName,ResourceName,ResourceType,SubscriptionId,Location,Sku,ResourceId -Head $Header -PreContent $Pre -PostContent $Post | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd | Out-file -FilePath "C:\Html\test.html"
    $results | where {$_.Tags -eq "KeepAlive"} | export-csv -Path "C:\Reports\KeepAliveTags.csv" -NoTypeInformation

                                                                                  
    • Edited by tallisar Monday, January 22, 2018 4:06 PM
    • Moved by Bill_Stewart Monday, March 12, 2018 8:35 PM This is not "fix/debug/rewrite my script for me" forum
    Monday, January 22, 2018 3:55 PM

All replies