简体   繁体   English

优化每个变量 powershell 脚本

[英]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.现在我正在使用上述脚本从所有订阅中获取 web 应用程序所需的所有数据。 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.目前,脚本需要时间来执行,我需要对其进行优化,并且脚本给出了重复的 output 所以最后添加了过滤器以通过唯一条目对其进行排序。

  1. It seems you are adding stuff to an array variable $allResources you don't use , so get rid of that.似乎您正在向不使用的数组变量$allResources添加东西,所以摆脱它。
  2. Instead of the costly (time/memory) $result += $obj , better let PowerShell collect the objects using $result = foreach(..)而不是昂贵的(时间/内存) $result += $obj ,最好让 PowerShell 使用$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.您在每次迭代的 foreach 循环中附加一个临时文件,然后导入该文件并在所有属性上对其进行过滤以使其成为唯一的。
  4. again, inside the loop you are exporting this uniqified data to a CSV file on every iteration再次,在循环中,您在每次迭代时都将这个 uniqified 数据导出到 CSV 文件
  5. You are calling upon cmdlet Get-AzAppServicePlan twice to get different properties.您正在调用 cmdlet Get-AzAppServicePlan两次以获取不同的属性。 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另外,您不应该使用名为$input的自定义变量,因为这是一个自动变量

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM