1
votes

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

1 Answers

1
votes

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