You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从PowerShell的DPM磁带数组中提取Barcode列生成逗号分隔列表

问题根源

你当前无法从$Tapes提取Barcode的核心原因是:你把Format-Table的返回值直接赋值给了$Tapes。PowerShell中所有Format-*前缀的cmdlet返回的都是用于控制台渲染的格式化对象,而非原始的磁带业务数据对象,自然无法读取原始的Barcode属性。

优化后代码(无需二次查询DPM)

你只需要调整变量存储逻辑,先把筛选后的原始磁带数据存到单独变量,再做格式化输出即可:

# 连接本地DPM服务器
$DPMServer = $env:COMPUTERNAME
Connect-DPMServer -DPMServerName $DPMServer | Out-Null

# 获取DPM磁带库
$DPMLib = Get-DPMLibrary

# 定义输出格式
$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   "}

# 计算本周一零点的时间戳
$Monday = (Get-Date).AddDays((-1 * (Get-Date).DayOfWeek.value__) + 1).Date

# 【修改点1】先存储筛选后的原始磁带数据,不直接套用格式化命令
$RawTapes = Get-DPMTape -DPMLibrary $DPMLib | 
    Where-Object {$_.Barcode -notlike "CLN*"} |
    Where-Object {$_.DisplayString -notlike "Free"} |
    Where-Object {$_.CreationDate -gt $Monday} |
    Sort-Object Barcode

# 【修改点2】格式化输出单独处理,不覆盖原始业务数据
$Tapes = $RawTapes | Format-Table -Property $Formatting

Write-Host "`n可异地存储磁带清单:" -ForegroundColor Cyan

# 输出表格形式的清单
$Tapes

# 【修改点3】直接从原始数据提取条码拼接,无需二次查询DPM
$barcodeList = $RawTapes.Barcode -join ","
Write-Host "`n逗号分隔条码列表:" -ForegroundColor Cyan
$barcodeList

逻辑说明

  • 原始筛选后的磁带数据全部保留在$RawTapes变量中,包含所有原始属性
  • 格式化输出仅用于控制台展示,不覆盖原始业务数据
  • 直接调用$RawTapes.Barcode就可以批量提取所有条码,用-join运算符直接拼接为逗号分隔字符串,不需要额外循环处理

内容的提问来源于stack exchange,提问作者TechFA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 21:06:03