Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

Optimize for each variable powershell script

$allResources = @()
$subscriptions=Get-AzSubscription

ForEach ($vsub in $subscriptions){
Select-AzSubscription $vsub.SubscriptionID

Write-Host

Write-Host "Working on "$vsub

Write-Host

$allResources += $allResources |Select-Object $vsub.SubscriptionID,$vsub.Name

$result=@()  
$webapps = Get-AzWebApp
foreach($webapp in $webapps){
    $Tier = (Get-AzResource -ResourceId $webapp.ServerFarmId).Sku.Tier
    $SKU = (Get-AzAppServicePlan -ResourceGroupName $webapp.ResourceGroup).Sku.Size
    $AppServiceName = (Get-AzAppServicePlan -ResourceGroupName $webapp.ResourceGroup).Name
    $obj = [PSCustomObject]@{
        TenantId = $vsub.TenantId
    SubscriptionName = $vsub.Name
    WebappName = $webapp.Name
    ResourceGroup = $webapp.ResourceGroup
    Hostname = $WebApp.DefaultHostName
    PricingTier = $Tier
    SKU = ($SKU -join ',')
    AppServiceName = ($AppServiceName -join ',')
        #State = $webapp.State
        #Location = $webapp.Location 
        #AppType = $webapp.Kind
            
}
    $result += $obj

$result | Export-Csv -Path "E:webapps_filter.csv" -Append -NoTypeInformation

$input = 'E:webapps_filter.csv'

$inputCsv = Import-Csv $input | Sort-Object * -Unique

$inputCsv | Export-Csv "E:webapps.csv" -NoTypeInformation}}

Right now I am using the above script to fetch all the required data of web apps from all the subscriptions. Currently, the script is taking time to execute, I need to optimize it and also the script gives a duplicate output so in last have added the filter to sort out it by unique entry.

question from:https://stackoverflow.com/questions/65883150/optimize-for-each-variable-powershell-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. It seems you are adding stuff to an array variable $allResources you don't use, so get rid of that.
  2. Instead of the costly (time/memory) $result += $obj, better let PowerShell collect the objects using $result = foreach(..)
  3. You are appending a temporary file inside the foreach loop on each iteration, then import this file and filter it on all properties to become unique.
  4. again, inside the loop you are exporting this uniqified data to a CSV file on every iteration
  5. You are calling upon cmdlet Get-AzAppServicePlan twice to get different properties. Use it only once would save time
  6. as aside, you should not use a self-defined variable called $input as this is an Automatic variable

Try:

$subscriptions = Get-AzSubscription

$result = foreach ($vsub in $subscriptions){
    Select-AzSubscription $vsub.SubscriptionID

    Write-Host
    Write-Host "Working on $($vsub.Name)"
    Write-Host

    foreach($webapp in (Get-AzWebApp)){
        $Tier = (Get-AzResource -ResourceId $webapp.ServerFarmId).Sku.Tier
        $Plan = Get-AzAppServicePlan -ResourceGroupName $webapp.ResourceGroup

        # output the object so it gets collected in $result
        [PSCustomObject]@{
            TenantId         = $vsub.TenantId
            SubscriptionName = $vsub.Name
            SubscriptionID   = $vsub.SubscriptionID
            WebappName       = $webapp.Name
            ResourceGroup    = $webapp.ResourceGroup
            Hostname         = $webapp.DefaultHostName
            PricingTier      = $Tier
            SKU              = @($Plan.Sku.Size) -join ','
            AppServiceName   = @($Plan.Name) -join ','
            #State           = $webapp.State
            #Location        = $webapp.Location
            #AppType         = $webapp.Kind
        }
    }
}

# sort unique and export the file
$result | Sort-Object * -Unique | Export-Csv -Path "E:webapps.csv" -NoTypeInformation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...