PowerShell在System.Object(PSCustomObject)中创建系列



有没有一种更简单的方法可以在不键入序列的情况下实现这一点?我尝试了一些关于数组和取消引用的想法,但都没有成功。

$rowP = "" | Select-Object Course,"5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35"

我更喜欢以下方法。

  1. 创建有序字典:
$properties = [ordered]@{
Course = $null
}
  1. 使用..范围运算符生成整数5到35的范围,将空条目添加到字典中:
5..35 |ForEach-Object {
$properties["${_}"] = $null
}
  1. 将字典转换为PSCustomObject:
$object = [pscustomobject]$properties

或者,您也可以使用范围运算符生成整数范围,并将其作为属性名称传递给Select-Object:

"" |Select-Object -Property @('Course'; 5..35 -as [string[]])

相关内容

  • 没有找到相关文章

最新更新