Powershell script to fetch Azure WebApp details

爷,独闯天下 提交于 2021-02-20 04:50:39

问题


I'm trying to write powershell script which gets all webapps and few properties of each azure web app. Below is the script i tried but it's giving me Http20Enabled is not valid property error. I think somehow i'm using the wrong scope.

I need to get properties of Webapp and SiteConfig of that webapp in a single row in CSV file.

Get-AzureRmWebApp | ForEach-Object {
  ($webapp = $_) | Get-AzureRmWebApp -ResourceGroupName {$webapp.ResourceGroup} -Name {$webapp.Name} | select -ExpandProperty SiteConfig | Select-Object @{ 

    Http20Enabled = $_.Http20Enabled
    MinTlsVersion = $_.MinTlsVersion
    AlwaysOn = $_.AlwaysOn
    Cors = $_.Cors
    Owner = {$webapp.Tags.Owner}
    Name = {$webapp.Name}
    ResourceGroup = {$webapp.ResourceGroup}
    HttpsOnly = {$webapp.HttpsOnly}
    ClientAffinityEnabled = {$webapp.ClientAffinityEnabled}
  } 
}| Export-Csv "C:apps1\test.csv"

Updated:

Tried this :

Get-AzureRMWebApp | ForEach-Object {
    $webapp = Get-AzureRMWebApp -ResourceGroupName $_.ResourceGroup -Name $_.Name 

    New-Object -TypeName psobject -property @{
      Http20Enabled = $webapp.siteconfig.Http20Enabled
      MinTlsVersion = $webapp.siteconfig.MinTlsVersion
      AlwaysOn = $webapp.siteconfig.AlwaysOn
      Cors = $webapp.siteconfig.Cors
      Owner = $webapp.Tags.Owner
      Name = $webapp.Name
      ResourceGroup = $webapp.ResourceGroup
      HttpsOnly = $webapp.HttpsOnly
      ClientAffinityEnabled = $webapp.ClientAffinityEnabled
    } 
}

Got the error -

Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ResourceGroupName'. Specified method is not supported.

PSVersion 5.1.17763.771
AzureRM 5.7.0


回答1:


I think what you mean to do was assign $webapp to the output of the Get-AzureRMWebApp command. I am also not really sure how the Select-Object command is supposed to work at the end, but I am assuming you want to have an object to use later. So you can use the New-Object cmdlet and then pass in the values from the $webapp object as properties. You don't need to expand the siteconfig property, you can reference the properties directly with dot notation. This ran on my system and gave me the output below the snippet.

Get-AzureRMWebApp | ForEach-Object {
    $webapp = Get-AzureRMWebApp -ResourceGroupName $_.ResourceGroup -Name $_.Name 

    New-Object -TypeName psobject -property @{
      Http20Enabled = $webapp.siteconfig.Http20Enabled
      MinTlsVersion = $webapp.siteconfig.MinTlsVersion
      AlwaysOn = $webapp.siteconfig.AlwaysOn
      Cors = $webapp.siteconfig.Cors
      Owner = $webapp.Tags.Owner
      Name = $webapp.Name
      ResourceGroup = $webapp.ResourceGroup
      HttpsOnly = $webapp.HttpsOnly
      ClientAffinityEnabled = $webapp.ClientAffinityEnabled
    } 
}


MinTlsVersion         : 1.0
HttpsOnly             : False
Http20Enabled         : False
AlwaysOn              : False
Owner                 :
Name                  : WEBAPP-NAME2
ResourceGroup         : RG-NAME1
Cors                  :
ClientAffinityEnabled : True

MinTlsVersion         : 1.0
HttpsOnly             : False
Http20Enabled         : False
AlwaysOn              : False
Owner                 :
Name                  : WEBAPP-NAME2
ResourceGroup         : RG-NAME1
Cors                  :
ClientAffinityEnabled : True

Answer to second question

I copied and pasted straight from your question and it worked just fine. Your AzureRM module could use some updating.

PS C:\> $Results = Get-AzureRMWebApp | ForEach-Object {
>>     $webapp = Get-AzureRMWebApp -ResourceGroupName $_.ResourceGroup -Name $_.Name
>>
>>     New-Object -TypeName psobject -property @{
>>       Http20Enabled = $webapp.siteconfig.Http20Enabled
>>       MinTlsVersion = $webapp.siteconfig.MinTlsVersion
>>       AlwaysOn = $webapp.siteconfig.AlwaysOn
>>       Cors = $webapp.siteconfig.Cors
>>       Owner = $webapp.Tags.Owner
>>       Name = $webapp.Name
>>       ResourceGroup = $webapp.ResourceGroup
>>       HttpsOnly = $webapp.HttpsOnly
>>       ClientAffinityEnabled = $webapp.ClientAffinityEnabled
>>     }
>> }
PS C:\> $Results.count
15

PS C:\> Get-Module AzureRM

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     6.13.1     AzureRM


来源:https://stackoverflow.com/questions/58437227/powershell-script-to-fetch-azure-webapp-details

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!