invoke 使って文字列をスクリプトとして実行
と似たような話ですが。
PowerShellは参照するプロパティ名を変数を使って動的に変更できます。
例えば、以下のコードでは、A
・B
の二つのプロパティを持つPSObjectを作成し、$nameOfProperty
の値によって参照するプロパティを変更しています。
powershell
1[psobject]$pso = New-Object -TypeName psobject -Property @{A=1;B=2}
2
3Write-Output -InputObject $pso
4
5[string]$nameOfProperty = 'a'
6'{0} = {1}' -f $nameOfProperty , $pso.$nameOfProperty # => 1
7
8$nameOfProperty = 'B'
9'{0} = {1}' -f $nameOfProperty , $pso.$nameOfProperty # => 2
10
11<# 結果
12A B
13- -
141 2
15a = 1
16B = 2
17#>
別の質問のコードのようにSelect-Object
コマンドレットでプロパティを定義してるのであれば、
以下のようにプロパティ名の配列を作ることで、比較的簡潔にプロパティを設定できると思います。
powershell
1[string[]]$Property =
2 'No,Category,Type,PutName,ListPrice,Purchase,PrimeCost,SellingPrice,Maker,DisplayOrderNo,EquipmentName,CharacterSymbol' -split ','
3
4$Psobject = New-Object psobject | Select-Object -Property $Property
5$列数 = $Property.Length
6
7for ($i = 1; $i -le $列数; $i++)
8{
9 $Psobject.($Property[$i-1]) = $代入したい値[$i]
10}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/08 01:01