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

时间:2019-10-17 16:39:13

标签: azure-web-app-service azure-powershell azure-app-service-plans

我正在尝试编写Powershell脚本,该脚本将获取所有Web应用程序以及每个Azure Web应用程序的少量属性。以下是我尝试过的脚本,但它给了我Http20Enabled无效的属性错误。我想我以某种方式使用了错误的范围。

我需要在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
    } 
}

发现错误-

  

无法将“ System.Object []”转换为所需的“ System.String”类型   通过参数“ ResourceGroupName”。不支持指定的方法。

PSVersion 5.1.17763.771
AzureRM 5.7.0

1 个答案:

答案 0 :(得分:1)

我认为您的意思是将$ 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