@@ -38,7 +38,7 @@ $commonModule = (Join-Path -Path $scriptDir -ChildPath $configParameters["Common
3838
3939if (! (Get-Module - Name (Get-Item $commonModule ).BaseName)) { Import-Module - Name $commonModule }
4040
41- Enter-Script - ScriptType " Export"
41+ Enter-Script - ScriptType " Export" - ErrorObject $Error
4242
4343function Export-CSEntries
4444{
@@ -58,7 +58,7 @@ function Export-CSEntries
5858
5959 foreach ($csentryChange in $CSEntries )
6060 {
61- $Global :CmdStatus = $null
61+ $Error .Clear () = $null
6262 $newAnchorTable = @ {}
6363
6464 $dn = Get-CSEntryChangeDN $csentryChange
@@ -91,12 +91,12 @@ function Export-CSEntries
9191 }
9292 catch
9393 {
94- $ Global :CmdStatus = " $_ "
94+ Write-Error " $_ "
9595 }
9696
97- if (Test-Variable " CmdStatus " " Global " )
97+ if ($Error )
9898 {
99- $csentryChangeResult = New-CSEntryChangeExportError - CSEntryChangeIdentifier $csentryChange.Identifier - ErrorObject $Global :CmdStatus
99+ $csentryChangeResult = New-CSEntryChangeExportError - CSEntryChangeIdentifier $csentryChange.Identifier - ErrorObject $Error
100100 }
101101 else
102102 {
@@ -148,13 +148,11 @@ function Export-User
148148 $cmd += " -DomainController '$preferredDomainController '"
149149 }
150150
151- $cmd += " -ErrorVariable 'Global:CmdStatus' -ErrorAction 'SilentlyContinue'"
152-
153151 Write-Debug " Invoking $cmd for user: $dn "
154152
155153 $x = Invoke-Expression $cmd
156154
157- if (! $Global :CmdStatus )
155+ if (! $Error )
158156 {
159157 $newAnchorTable.Add (" Guid" , $x.Guid.ToByteArray ())
160158 }
@@ -289,7 +287,7 @@ function Invoke-EnableCsUserCommand
289287 $CSEntryChange
290288 )
291289
292- if (! $Global :CmdStatus )
290+ if (! $Error )
293291 {
294292 $dn = Get-CSEntryChangeDN $CSEntryChange
295293 $identity = Get-CsIdentity $CSEntryChange
@@ -315,8 +313,6 @@ function Invoke-EnableCsUserCommand
315313 $cmd += " -SipAddressType '$sipAddressType '"
316314 }
317315
318- $cmd += " -ErrorVariable 'Global:CmdStatus' -ErrorAction 'SilentlyContinue'"
319-
320316 Write-Debug " Invoking $cmd for user: $dn "
321317
322318 Invoke-Expression $cmd | Out-Null
@@ -339,7 +335,7 @@ function Invoke-SetCsUserCommand
339335 $CSEntryChange
340336 )
341337
342- if (! $Global :CmdStatus )
338+ if (! $Error )
343339 {
344340 $dn = Get-CSEntryChangeDN $CSEntryChange
345341 $identity = Get-CsIdentity $CSEntryChange
@@ -369,8 +365,6 @@ function Invoke-SetCsUserCommand
369365 if ($remoteCallControlTelephonyEnabled -ne $null ) { $cmd += " -RemoteCallControlTelephonyEnabled `$ $remoteCallControlTelephonyEnabled " }
370366 if ($sipAddress -ne $null ) { $cmd += " -SipAddress '$sipAddress '" }
371367
372- $cmd += " -ErrorVariable 'Global:CmdStatus' -ErrorAction 'SilentlyContinue'"
373-
374368 Write-Debug " Invoking $cmd for user: $dn "
375369
376370 Invoke-Expression $cmd | Out-Null
@@ -394,7 +388,7 @@ function Invoke-GrantCsPolicyCommands
394388 )
395389
396390
397- if (! $Global :CmdStatus )
391+ if (! $Error )
398392 {
399393 $dn = Get-CSEntryChangeDN $CSEntryChange
400394 $identity = Get-CsIdentity $CSEntryChange
@@ -439,8 +433,6 @@ function Invoke-GrantCsPolicyCommands
439433 if ($presencePolicyChanged ) { $cmd += " | Grant-CsPresencePolicy -PolicyName '$presencePolicy ' -PassThru" }
440434 if ($voicePolicyChanged ) { $cmd += " | Grant-CsVoicePolicy -PolicyName '$voicePolicy ' -PassThru" }
441435
442- $cmd += " -ErrorVariable 'Global:CmdStatus' -ErrorAction 'SilentlyContinue'"
443-
444436 Write-Debug " Invoking $cmd for user: $dn "
445437
446438 Invoke-Expression $cmd | Out-Null
@@ -463,14 +455,14 @@ function Invoke-MoveCsUserCommand
463455 $CSEntryChange
464456 )
465457
466- if (! $Global :CmdStatus )
458+ if (! $Error )
467459 {
468460 $dn = Get-CSEntryChangeDN $CSEntryChange
469461 $identity = Get-CsIdentity $CSEntryChange
470462 $registrarPool = Get-CSEntryChangeValue - CSEntryChange $CSEntryChange - AttributeName " RegistrarPool"
471463 if ($registrarPool )
472464 {
473- $cmd = " Move-CsUser -Identity '$identity ' -Target $registrarPool -Force:`$ $forceMove -Confirm:`$ $false -ErrorVariable 'Global:CmdStatus' -ErrorAction 'SilentlyContinue '"
465+ $cmd = " Move-CsUser -Identity '$identity ' -Target $registrarPool -Force:`$ $forceMove -Confirm:`$ $false '"
474466 if (! [string ]::IsNullOrEmpty($preferredDomainController ))
475467 {
476468 $cmd += " -DomainController '$preferredDomainController '"
@@ -499,7 +491,7 @@ function Invoke-DisableCsUserCommand
499491 $CSEntryChange
500492 )
501493
502- if (! $Global :CmdStatus )
494+ if (! $Error )
503495 {
504496 $dn = Get-CSEntryChangeDN $CSEntryChange
505497 $identity = Get-CsIdentity $CSEntryChange
@@ -510,29 +502,9 @@ function Invoke-DisableCsUserCommand
510502 $cmd += " -DomainController '$preferredDomainController '"
511503 }
512504
513- $cmd += " -ErrorVariable 'Global:CmdStatus' -ErrorAction 'SilentlyContinue'"
514-
515505 Write-Debug " Invoking $cmd for user: $dn "
516506
517507 Invoke-Expression $cmd | Out-Null
518-
519- # quick and dirty check for object deletion in AD. will probably need localisation.
520- # we could query AD as well, but that will again work only with PreferredDomainController setting in a multi-domain setup since we want to use objectGUID and not DN to bind.
521- if ($Global :CmdStatus )
522- {
523- foreach ($cmdStatus in $Global :CmdStatus )
524- {
525- $exceptionMessage = $cmdStatus.ToString ()
526- if ($exceptionMessage.Contains (" Management object not found for identity" ))
527- {
528- Write-Warning (" CSEntry Identifier: {0}. Error: {1}" -f $CSEntryChange.Identifier , $exceptionMessage )
529-
530- $Global :CmdStatus = $null # ignore the error if the object is already deleted from AD.
531- }
532-
533- break # process the first error and stop
534- }
535- }
536508 }
537509}
538510
@@ -554,7 +526,7 @@ function Get-CsIdentity
554526 $CSEntryChange
555527 )
556528
557- if (! $Global :CmdStatus )
529+ if (! $Error )
558530 {
559531 $dn = Get-CSEntryChangeDN $CSEntryChange
560532
@@ -586,5 +558,5 @@ $forceMove = (Get-ConfigParameter -ConfigParameters $configParameters -Parameter
586558
587559Export-CSEntries
588560
589- Exit-Script - ScriptType " Export" - SuppressErrorCheck
561+ Exit-Script - ScriptType " Export" - SuppressErrorCheck - ErrorObject $Error
590562
0 commit comments