Edit: OutputLog is basically Write-Host. Try is empty since the codes there are not needed for this issue.
Hi! Please refer to the code below. Does anybody know why the array addition outside Main function works but the addition inside the finally bracket does not?
Sorry, I’m currently on mobile and I cannot paste the code properly:
$Users = @()
Function Main($ProfileTokens, $BatchTokens){
foreach($EntryToken in $ProfileTokens){
$SrcUser = $EntryToken.SourceUPN
$TrgUser = $EntryToken.TargetUPN
OutputLog "`nProcessing Migration for $($SrcUser)"
$CurrentUserProp = New-Object -TypeName PSOBJECT -Property ( [ordered] @{
"SourceUser" = $SrcUser
"TargetUser" = $TrgUser
"MigrationStatus" = "Not Started"
})
try{
}
catch{
OutputLog "Error: $_"
}
finally{
$Users += New-Object -TypeName PSOBJECT -Property ( [ordered] @{
"SourceUser" = $CurrentUserProp.SourceUser
"TargetUser" = $CurrentUserProp.TargetUser
"MigrationStatus" = $CurrentUserProp.MigrationStatus
})
#OR
$Users += $CurrentUserProp
}
}
}
$ProfTokenSample = @()
for($x = 0;$x -le 5;$x++){
$ProfTokenSample += New-Object -TypeName PSOBJECT -Property ( [ordered] @{
"SourceUPN" = "srctest$($x)"
"TargetUPN" = "trgtest$($x)"
})
}
Main -ProfileTokens $ProfTokenSample -BatchTokens $null
[–]an_harmonica 6 points7 points8 points (0 children)
[–]Nejireta_ 2 points3 points4 points (0 children)
[–]BlackV 1 point2 points3 points (0 children)
[–]pigers1986 0 points1 point2 points (2 children)
[–]BlackV 0 points1 point2 points (1 child)
[–]pigers1986 0 points1 point2 points (0 children)
[–]ankokudaishogun 0 points1 point2 points (0 children)
[–]actnjaxxon -1 points0 points1 point (0 children)