【问题标题】:Return Values of Single Column in PowerShell ArrayPowerShell 数组中单列的返回值
【发布时间】:2021-11-17 05:03:51
【问题描述】:

我正在为 DPM 创建一个 PowerShell 脚本,该脚本输出场外准备好的磁带。下面的代码需要几秒钟来完成 DPM 连接并返回查询,这很好。

# Connect to local DPM server
$DPMServer = $env:COMPUTERNAME
Connect-DPMServer -DPMServerName $DPMServer | Out-Null

# Get the DPM libary
$DPMLib = Get-DPMLibrary

#Format output
$Formatting = @{Expression={$_.Barcode}; Label="Barcode "; Width=12}, 
     @{Expression={"{0:MM/dd/yyyy HH:mm tt}" -f $_.CreationDate}; Label="Creation Date      "; Width=23}, 
     @{Expression={$_.DisplayString}; Label="Tape Label              "; Width=28}, 
     @{Expression={"{0,15:N0}" -f $_.DataWrittenDisplayString}; Label="Data Written   "}

#Calculate Monday at midnight of this week
$Monday = (Get-Date).AddDays((-1 * (Get-Date).DayOfWeek.Value__) + 1).Date

# Get specific tapes
$Tapes = Get-DPMTape -DPMLibrary $DPMLib | 
    Where-Object {$_.Barcode -notlike "CLN*"} |
    Where-Object {$_.DisplayString -notlike "Free"} |
    Where-Object {$_.CreationDate -gt $Monday} |
    Sort-Object Barcode |
    Format-Table -Property $Formatting

Write-Host "`nOffsite Ready Tapes:" -ForegroundColor Cyan

#Output tapes
$Tapes

输出如下:

我的问题是我现在如何只选择 $Tapes 数组中列出的条形码,并将它们(低于我已有的)输出为逗号分隔列表,而无需再次运行 DPM 查询。我尝试了各种各样的事情都没有运气,我错过了一些明显的东西。

如果我与 DPM 进行第二次连接,我可以这样做,但我试图避免将运行时间加倍。这是我正在努力改进的原始新手脚本的一部分。

# Define DPM server to connect to
$DPMServer = $env:COMPUTERNAME
Connect-DPMServer -DPMServerName $DPMServer | Out-Null

# Get the DPM libary
$DPMLib = Get-DPMLibrary

# Get tape display strings and barcodes, sort by barcode
$Tapes = Get-DPMTape -DPMLibrary $DPMLib | Select-Object CreationDate, DisplayString, Barcode | Sort-Object Barcode

# Create empty array
$DPMTapesForOffsite = @()

Write-Host "`nOffsite Ready Tapes:`n" -ForegroundColor Cyan

foreach ($_ in $Tapes) {
    # Exclude cleaning tapes
    if ($_.Barcode -notlike "CLN*") {
        # Exclude marked as free
        if ($_.DisplayString -notlike "Free") {
            $TimeStamp = Get-Date $_.CreationDate
            # Timestamp is from this week
            if ($Timestamp -gt $Monday) {
                $DPMTapesForOffsite = $DPMTapesForOffsite + $_.barcode
                Write-Host $_.barcode
            }
        }
    }
}

# Format tape list as comma delimited
$DPMTapesForOffsite = $DPMTapesForOffsite -join ","

我遗漏了一些明显的东西,任何帮助将不胜感激。

【问题讨论】:

    标签: arrays powershell select return


    【解决方案1】:
    # Omit Format-Table initially, so as to store actual *data* in $tapes,
    # not *formatting instructions*, which is what Format-* cmdlets return.
    $tapes = Get-DPMTape -DPMLibrary $DPMLib | 
        Where-Object {$_.Barcode -notlike "CLN*"} |
        Where-Object {$_.DisplayString -notlike "Free"} |
        Where-Object {$_.CreationDate -gt $Monday} |
        Sort-Object Barcode
    
    # Now you can apply the desired formatting.
    Write-Host "`nOffsite Ready Tapes:" -ForegroundColor Cyan
    $tapes | Format-Table -Property $Formatting
    
    # Thanks to PowerShell's member enumeration feature,
    # accessing property .Barcode on the entire $tapes *collection* 
    # conveniently returns its *elements'* property values.
    # -join ',' joins them with commas.
    $tapes.Barcode -join ','
    

    Format-* cmdlet 输出对象,其唯一目的是为 PowerShell 的输出格式化系统提供格式化指令 - 请参阅 this answer
    简而言之:永远只使用Format-* cmdlet 来格式化数据用于显示,从不用于后续编程处理

    This answer 解释成员枚举

    【讨论】:

    • 谢谢,格式是问题!我还需要深入挖掘以获取以下值:$Tapes.Barcode | Select-Object -ExpandProperty Value
    • 很高兴听到这个消息,@TechFA。我曾假设.Barcode 直接返回感兴趣的值;看起来您可以简化为 $tapes.Barcode.Value - 您可以嵌套成员枚举。
    • 啊,更好,感谢您的提示!
    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多