用于获取 Azure WebApp 详细信息的 Powershell 脚本

Powershell script to fetch Azure WebApp details

我正在尝试编写 powershell 脚本,它获取所有网络应用程序和每个 azure 网络应用程序的一些属性。下面是我试过的脚本,但它给我 Http20Enabled is not valid 属性 错误。我想不知何故我使用了错误的范围。

我需要在 CSV 文件的一行中获取该 Web 应用程序的 WebappSiteConfig 的属性。

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"

更新:

试过这个:

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
    } 
}

收到错误 -

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

PS 版本 5.1.17763.771
AzureRM 5.7.0

我认为您的意思是将 $webapp 分配给 Get-AzureRMWebApp 命令的输出。我也不太确定 Select-Object 命令最后应该如何工作,但我假设您希望稍后使用一个对象。因此,您可以使用 New-Object cmdlet,然后将 $webapp 对象的值作为属性传入。不需要展开siteconfig 属性,直接用点号引用属性即可。这 运行 在我的系统上,并在代码段下方为我提供了输出。

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

第二个问题的答案

我直接从您的问题中复制并粘贴了它,效果很好。您的 AzureRM 模块可以使用一些更新。

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