スクリプトを使用して、以下のような特定の値を持つ新しいオブジェクトを作成しています。これは$ Namesが単一の文字列値を返す場合に機能しますが、配列があり、$ newobjを出力する前にgetは次のエラーを返します。 $ Namesのforeachステートメントを追加したときに、何が原因でエラーが発生しているのかを調べようとしました。また、$ xの値は$ names配列の両方の値John、Jakeの "London"と同じです。Add-Memberを使用して配列からオブジェクトを作成する
現在の出力では、StudentNameは両方とも "John"と表示され、-Forceパラメータを使用するとStudentNameの値は両方とも "Jake"になります。これらの値を別々にする必要があります。 ?誰かが私を助けてくださいでし
Add-Member : Cannot add a member with the name "StudentName" because a member with that name already exists. To overwrite the
member anyway, add the Force parameter to your command.
At E:\test.ps1:93 char:14
+ $newobj | Add-Member -MemberType NoteProperty -Name StudentName -Value $_
スクリプト:
# $DCNames is an array
Foreach($x in $DCNames){
$newobj = New-Object psobject
$newobj | Add-Member -MemberType NoteProperty -Name DC -Value $x
###### $Names in most cases is a single string or it could be also an array. For example: it could be a value Adam or {John Jake….} and so I would need to Add each member in $Names as StudentName object below.
$Names [email protected]()
$Names = $List | Where-Object{$_.Name -eq $x} | Select -ExpandProperty Name
$Names | foreach {
$newobj | Add-Member -MemberType NoteProperty -Name StudentName -Value $_
# $ClassName is a single string
$newobj | Add-Member -MemberType NoteProperty -Name Class -Value $ClassName
$newobj
}
電流出力:
Name: Vegas
StudentName: Adam
Class: 10
Name: London
StudentName: John
Class: 12
Name: London
StudentName: John (the script should take the second value “Jake” here from $Names array, but is not happening for some reason)
Class: 11
ただ、全体としてそれを追加し、それは、配列または単一の値でそれを列挙しないでください。他の選択肢はありません。 – wOxxOm
私は$ newobjをCSVにエクスポートすると、System.Object []が同じで、実際の値を読み取ることができません。どのように私はそれを可読配列オブジェクトに変換するべきですか? – user3331975
ああ! CSVは取引を変更します。したがって、すべての名前を1つの列セルに保持したいとします。つまり、セミコロンで区切られた1つの文字列に '-join'する必要があります。 – wOxxOm