Skip to content

Commit 69859c4

Browse files
LyncConnector - Updated to run under RestrictedLanguage mode. Hopefully works as I could not test myself.
1 parent 0432a56 commit 69859c4

11 files changed

+63
-95
lines changed

src/LyncConnector/Scripts/1. Lync.Common.psm1

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Set-StrictMode -Version "2.0"
1414

1515
$Global:ConnectorName = "LyncPowerShellConnector"
1616
$Global:RemoteSessionName = "LyncPowerShellConnector"
17-
$Error.Clear()
17+
$Global:Error.Clear()
1818

1919
#endregion "Global Variables"
2020

@@ -37,13 +37,20 @@ function Enter-Script
3737
param(
3838
[parameter(Mandatory = $true)]
3939
[string]
40-
$ScriptType
40+
$ScriptType,
41+
[parameter(Mandatory = $false)]
42+
[ValidateNotNull()]
43+
[System.Collections.ArrayList]
44+
$ErrorObject
4145
)
4246

4347
process
4448
{
4549
Write-Verbose "$Global:ConnectorName - $ScriptType Script: Execution Started..."
46-
$Error.Clear()
50+
if ($ErrorObject)
51+
{
52+
$ErrorObject.Clear()
53+
}
4754
}
4855
}
4956

@@ -62,6 +69,10 @@ function Exit-Script
6269
[string]
6370
$ScriptType,
6471
[parameter(Mandatory = $false)]
72+
[ValidateNotNull()]
73+
[System.Collections.ArrayList]
74+
$ErrorObject,
75+
[parameter(Mandatory = $false)]
6576
[switch]
6677
$SuppressErrorCheck,
6778
[parameter(Mandatory = $false)]
@@ -71,15 +82,19 @@ function Exit-Script
7182

7283
process
7384
{
74-
if ($Error.Count -ne 0 -and !$SuppressErrorCheck)
85+
if (!$SuppressErrorCheck -and $ErrorObject -and $ErrorObject.Count -ne 0)
7586
{
76-
$errorMessage = [string]$Error[0]
87+
# Take the first one otherwise you get "An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute.."
88+
# Seems like a bug in Remote PSH
89+
$errorMessage = $ErrorObject[0] # | Out-String -ErrorAction SilentlyContinue
7790

7891
if ($ExceptionRaisedOnErrorCheck -eq $null)
7992
{
8093
$ExceptionRaisedOnErrorCheck = [Microsoft.MetadirectoryServices.ExtensibleExtensionException]
8194
}
8295

96+
$ErrorObject.Clear()
97+
8398
throw $errorMessage -as $ExceptionRaisedOnErrorCheck
8499
}
85100

@@ -866,40 +881,19 @@ function New-CSEntryChangeExportError
866881
[Guid]
867882
$CSEntryChangeIdentifier,
868883
[parameter(Mandatory = $true)]
869-
[object]
884+
[System.Collections.ArrayList]
870885
$ErrorObject
871886
)
872887

873888
$csentryChangeResult = $null
874-
875-
try
876-
{
877-
foreach ($cmdStatus in $ErrorObject)
878-
{
879-
$exception = $cmdStatus.GetBaseException()
880-
$exceptionType = $exception.GetType().Name
881-
$exceptionMessage = $exception.Message
882-
883-
Write-Warning ("CSEntry Identifier: {0}. ErrorName: {1}. ErrorDetail: {2}" -f $CSEntryChangeIdentifier, $exceptionType, $exceptionMessage)
884-
$csentryChangeResult = [Microsoft.MetadirectoryServices.CSEntryChangeResult]::Create($CSEntryChangeIdentifier, $null, "ExportErrorCustomContinueRun", $exceptionType, $exceptionMessage)
885-
Write-Warning ("CSEntryChangeResult Identifier: {0}. ErrorCode: {1}. ErrorName: {2}. ErrorDetail: {3}" -f $csentryChangeResult.Identifier, $csentryChangeResult.ErrorCode, $csentryChangeResult.ErrorName, $csentryChangeResult.ErrorDetail)
886-
887-
break # report the first error and stop
888-
}
889-
}
890-
catch
891-
{
892-
foreach ($cmdStatus in $ErrorObject)
893-
{
894-
$exceptionType = "RUNTIME_EXCEPTION"
895-
$exceptionMessage = $cmdStatus.ToString()
896-
Write-Warning ("CSEntry Identifier: {0}. Error: {1}" -f $CSEntryChangeIdentifier, $exceptionMessage)
897-
$csentryChangeResult = [Microsoft.MetadirectoryServices.CSEntryChangeResult]::Create($CSEntryChangeIdentifier, $null, "ExportErrorCustomContinueRun", $exceptionType, $exceptionMessage)
898-
Write-Warning ("CSEntryChangeResult Identifier: {0}. ErrorCode: {1}. ErrorName: {2}. ErrorDetail: {3}" -f $csentryChangeResult.Identifier, $csentryChangeResult.ErrorCode, $csentryChangeResult.ErrorName, $csentryChangeResult.ErrorDetail)
899-
900-
break # report the first error and stop
901-
}
902-
}
889+
# Take the first one otherwise you get "An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute.."
890+
# Seems like a bug in Remote PSH
891+
$errorDetail = $ErrorObject[0] # | Out-String -ErrorAction SilentlyContinue
892+
Write-Warning ("CSEntry Identifier: {0}. ErrorCount: {1}. ErrorDetail: {2}" -f $CSEntryChangeIdentifier, $ErrorObject.Count, $errorDetail)
893+
$csentryChangeResult = [Microsoft.MetadirectoryServices.CSEntryChangeResult]::Create($CSEntryChangeIdentifier, $null, "ExportErrorCustomContinueRun", "RUNTIME_EXCEPTION", $errorDetail)
894+
Write-Warning ("CSEntryChangeResult Identifier: {0}. ErrorCode: {1}. ErrorName: {2}. ErrorDetail: {3}" -f $csentryChangeResult.Identifier, $csentryChangeResult.ErrorCode, $csentryChangeResult.ErrorName, $csentryChangeResult.ErrorDetail)
895+
896+
$ErrorObject.Clear()
903897

904898
return $csentryChangeResult
905899
}

src/LyncConnector/Scripts/10. ExportScript-Lync.ps1

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ $commonModule = (Join-Path -Path $scriptDir -ChildPath $configParameters["Common
3838

3939
if (!(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

4343
function 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

587559
Export-CSEntries
588560

589-
Exit-Script -ScriptType "Export" -SuppressErrorCheck
561+
Exit-Script -ScriptType "Export" -SuppressErrorCheck -ErrorObject $Error
590562

src/LyncConnector/Scripts/11. End-ExportScript-Lync.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ $commonModule = (Join-Path -Path $scriptDir -ChildPath $configParameters["Common
3939

4040
if (!(Get-Module -Name (Get-Item $commonModule).BaseName)) { Import-Module -Name $commonModule }
4141

42-
Enter-Script -ScriptType "End-Export"
42+
Enter-Script -ScriptType "End-Export" -ErrorObject $Error
4343

4444
if (Test-Variable -Name "Session" -Scope "Global")
4545
{
4646
Remove-PSSession $Global:Session
4747
}
4848

49-
Exit-Script -ScriptType "End-Export"
49+
Exit-Script -ScriptType "End-Export" -ErrorObject $Error

src/LyncConnector/Scripts/2. ValidationScript-Lync.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ $commonModule = (Join-Path -Path $ScriptDir -ChildPath $ConfigParameters["Common
3535

3636
if (!(Get-Module -Name (Get-Item $commonModule).BaseName)) { Import-Module -Name $commonModule }
3737

38-
Enter-Script -ScriptType "Validation"
38+
Enter-Script -ScriptType "Validation" -ErrorObject $Error
3939

4040
function Test-ConfigParameterPage
4141
{
@@ -235,4 +235,4 @@ function Test-GlobalConfigParameterPage
235235

236236
Test-ConfigParameterPage
237237

238-
Exit-Script -ScriptType "Validation"
238+
Exit-Script -ScriptType "Validation" -ErrorObject $Error

src/LyncConnector/Scripts/3. SchemaScript-Lync.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ $commonModule = (Join-Path -Path $ScriptDir -ChildPath $ConfigParameters["Common
3232

3333
if (!(Get-Module -Name (Get-Item $commonModule).BaseName)) { Import-Module -Name $commonModule }
3434

35-
Enter-Script -ScriptType "Schema"
35+
Enter-Script -ScriptType "Schema" -ErrorObject $Error
3636

3737
function Get-ConnectorSchema
3838
{
@@ -58,4 +58,4 @@ function Get-ConnectorSchema
5858

5959
Get-ConnectorSchema
6060

61-
Exit-Script -ScriptType "Schema"
61+
Exit-Script -ScriptType "Schema" -ErrorObject $Error

src/LyncConnector/Scripts/4. PartitionScript-Lync.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ $commonModule = (Join-Path -Path $ScriptDir -ChildPath $ConfigParameters["Common
3232

3333
if (!(Get-Module -Name (Get-Item $commonModule).BaseName)) { Import-Module -Name $commonModule }
3434

35-
Enter-Script -ScriptType "Partition"
35+
Enter-Script -ScriptType "Partition" -ErrorObject $Error
3636

3737
function Get-Partitions
3838
{
@@ -132,4 +132,4 @@ if ([string]::IsNullOrEmpty($preferredDomainController))
132132

133133
Get-Partitions
134134

135-
Exit-Script -ScriptType "Partition"
135+
Exit-Script -ScriptType "Partition" -ErrorObject $Error

src/LyncConnector/Scripts/5. HierarchyScript-Lync.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ $commonModule = (Join-Path -Path $ScriptDir -ChildPath $ConfigParameters["Common
3535

3636
if (!(Get-Module -Name (Get-Item $commonModule).BaseName)) { Import-Module -Name $commonModule }
3737

38-
Enter-Script -ScriptType "Hierarchy"
38+
Enter-Script -ScriptType "Hierarchy" -ErrorObject $Error
3939

4040
function Get-Hierarchy
4141
{
@@ -109,4 +109,4 @@ if (![string]::IsNullOrEmpty($preferredDomainController))
109109

110110
Get-Hierarchy
111111

112-
Exit-Script -ScriptType "Hierarchy"
112+
Exit-Script -ScriptType "Hierarchy" -ErrorObject $Error

src/LyncConnector/Scripts/6. Begin-ImportScript-Lync.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ $commonModule = (Join-Path -Path $ScriptDir -ChildPath $ConfigParameters["Common
3636

3737
if (!(Get-Module -Name (Get-Item $commonModule).BaseName)) { Import-Module -Name $commonModule }
3838

39-
Enter-Script -ScriptType "Begin-Import"
39+
Enter-Script -ScriptType "Begin-Import" -ErrorObject $Error
4040

4141
function Get-OpenImportConnectionResults
4242
{
@@ -132,6 +132,7 @@ if (![string]::IsNullOrEmpty($preferredDomainController))
132132
}
133133

134134
$session = Get-PSSession -Name $Global:RemoteSessionName -ErrorAction "SilentlyContinue"
135+
$Error.Clear() # Could use -ErrorAction "Igonre" in PSH v3.0
135136

136137
if (!$session)
137138
{
@@ -149,5 +150,5 @@ if (!$session)
149150
Get-OpenImportConnectionResults
150151

151152
$exceptionRaisedOnErrorCheck = [Microsoft.MetadirectoryServices.ServerDownException]
152-
Exit-Script -ScriptType "Begin-Import" -ExceptionRaisedOnErrorCheck $exceptionRaisedOnErrorCheck
153+
Exit-Script -ScriptType "Begin-Import" -ExceptionRaisedOnErrorCheck $exceptionRaisedOnErrorCheck -ErrorObject $Error
153154

0 commit comments

Comments
 (0)