【问题标题】:Calculated properties, complex objects and arrays in PowerShellPowerShell 中的计算属性、复杂对象和数组
【发布时间】:2014-06-07 04:51:49
【问题描述】:

我目前正在使用 PowerShell,但在计算属性和数组方面遇到了一些难题,例如,我目前正在使用 Web 管理/IIS 模块:

Get-Website |
    ? { $_.State -eq "Stopped" } |
        select Name, @{ n = "Binding"; Expression = { $_.Bindings | select [string]::Join(", ", $_.Collection) } } |
            ConvertTo-Html > "C:\iis.html"

该脚本应该抓取所有当前关闭的站点,并输出站点名称及其与 HTML 的绑定。问题在于计算属性的表达式部分来获取绑定 - 它通过 string.Join() 调用连接绑定集合(它是一个数组)。这行得通 - 但输出是这样的:

@{ [string]::Join(", ", $_.Collection) =http *:80:blueberryboat.local, http *:80:test.co.uk}

有什么想法吗?

干杯

【问题讨论】:

    标签: powershell


    【解决方案1】:

    有趣的语法...但这不是正确的方法。 怎么样:

    Expression = { $_.Bindings | Foreach-Object { $_.Collection -join ', ' }}
    

    【讨论】:

    • 啊哈,太棒了 - 我曾尝试使用“加入”开关,但缺少必要的 foreach。再次感谢。
    猜你喜欢
    • 2021-01-02
    • 2015-05-17
    • 1970-01-01
    • 2019-08-23
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    相关资源
    最近更新 更多