【问题标题】:Powershell script to fetch Azure WebApp details用于获取 Azure WebApp 详细信息的 Powershell 脚本
【发布时间】:2019-10-17 16:39:13
【问题描述】:

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

我需要在 CSV 文件的单行中获取该 webapp 的 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”类型 通过参数“资源组名称”。不支持指定的方法。

PSVersion 5.1.17763.771
AzureRM 5.7.0

【问题讨论】:

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


    【解决方案1】:

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

    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
    

    【讨论】:

    • 谢谢。导出到 csv 是否在您的脚本中运行良好?
    • @Venky - 抱歉,我第一次复制和粘贴时遗漏了一些内容。循环中的第一行需要引用 $_ 或根本不需要参数。我已经在我的回答中更正了。是的,您可以将| Export-Csv -Path <path> -NoTypeInformation 添加到末尾,就像您拥有它一样,它工作正常。如果这对您有用,请投票并标记为答案! :)
    • 第二次Get-AzureRmWebapp 出现此错误The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input。我还没有使用Az cmdlet。
    • @Venky - 抱歉,我没有 AzureRM cmdlet。我更新了答案以使用非管道方法。
    • 现在出现这个错误Get-AzureRmWebApp : Cannot validate argument on parameter 'ResourceGroupName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多