【问题标题】:Array not loading correctly [duplicate]数组未正确加载[重复]
【发布时间】:2020-08-14 06:11:09
【问题描述】:

我有一个命令将过滤的服务器列表加载到变量中:

$Computers = Get-adcomputer -filter "OperatingSystem -notlike '*server*'" -properties * |Where-Object {$_.LastLogonDate -gt $time -and ($_.Enabled -eq $true) -and ($_.name -ne 'Rose*')} |select name

我想使用 For each 循环来处理这些计算机名。当我刚刚输入命令时,我会得到一个顶部带有“名称”的列表和一个计算机名称列表

但是当我在循环中使用 Write out the contents of $Computer 时,我会得到一个 @{name=Computer1}

列表

发生了什么事?为什么我不能使用干净的计算机阵列?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    对于通过管道传送到select Name 的每个输入对象,它将创建一个具有单个属性Name 的新对象,其值从输入对象的相应属性中复制而来。

    要仅获取输入对象上Name 属性的,请使用Select-Object-ExpandProperty 参数:

    $Computers = Get-adcomputer -filter "OperatingSystem -notlike '*server*'" -properties * |Where-Object {$_.LastLogonDate -gt $time -and ($_.Enabled -eq $true) -and ($_.name -ne 'Rose*')} |Select -ExpandProperty Name
    

    现在,$computers 将是一个字符串数组,而不是具有 Name 属性的对象数组

    【讨论】:

    • 该死,我忘了-ExpandedProperty。成功了,谢谢!
    猜你喜欢
    • 2015-11-01
    • 2019-07-08
    • 2020-12-16
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2015-09-28
    • 2018-08-14
    • 2018-10-29
    相关资源
    最近更新 更多