我想在此腳本中使用$a
變量來處理中間步驟,以便我不必重複使用$array[$array.Count-1]
。同樣,對於$prop
也是如此。但是,值將被循環中的最後一個值覆蓋。使用中間變量處理數組(參考類型)
$guests = Import-Csv -Path C:\Users\shant_000\Desktop\UploadGuest_test.csv
$output = gc '.\Sample Json.json' | ConvertFrom-Json
$array = New-Object System.Collections.ArrayList;
foreach ($g in $guests) {
$array.Add($output);
$a = $array[$array.Count-1];
$a.Username = $g.'EmailAddress';
$a.DisplayName = $g.'FirstName' + ' ' + $g.'LastName';
$a.Password = $g.'LastName' + '123';
$a.Email = $g.'EmailAddress';
foreach ($i in $a.ProfileProperties.Count) {
$j = $i - 1;
$prop = $a.ProfileProperties[$j];
if ($prop.PropertyName -eq "FirstName") {
$prop.PropertyValue = $g.'FirstName';
} elseif ($prop.PropertyName -eq "LastName") {
$prop.PropertyValue = $g.'LastName';
}
$a.ProfileProperties[$j] = $prop;
}
$array[$array.Count-1] = $a;
}
$array;
'$ output = gc'。\ Sample Json.json'| ConvertFrom-Json' - >'$ outputJson = gc'。\ Sample Json.json'','$ array.Add($ output)' - >'[void] $ array.Add(($ outputJson | ConvertFrom-Json) )' – PetSerAl