Hi All, I’m looking for clarification with the PS.AddScript method. I am using PowerShell version v2.0.50727 and encountered a “Missing closing '}' in statement block.” error while running the following logic.
Dim exRunSpace As Runspace = RunspaceFactory.CreateRunspace() Dim ps As PowerShell = PowerShell.Create Dim command As New PSCommand … ps.Streams.ClearStreams() ps.Streams.Error.Clear() ps.Commands.Clear() ps.AddScript("$recipient = Get-Recipient -identity " & upn) ps.AddScript("If ($recipient –ne $null) {") ps.AddScript("If ($recipient.RecipientTypeDetails –eq 'MailUser') {") ps.AddScript("Remove-MailUser -identity " & upn & " -domaincontroller " & dnsHostFQDName & " -WhatIf:$false -Confirm:$false }") ps.AddScript("If ($recipient.RecipientTypeDetails –eq 'RemoteUserMailbox') {") ps.AddScript("Remove-RemoteMailbox -identity " & upn & " -domaincontroller " & dnsHostFQDName & " -WhatIf:$false -Confirm:$false }") ps.AddScript("}") ps.AddCommand("Out-String") Dim commandResults As Collection(Of PSObject) = Nothing If updateOn Then commandResults = ps.Invoke() End If ' updateOn
The ps.Commands(0-8) CommandText contains the text listed below as observed in Debugger.
$recipient = Get-Recipient -identity 12345678@school.edu If ($recipient –ne $null) { If ($recipient.RecipientTypeDetails –eq 'MailUser') { Remove-MailUser -identity 12345678@school.edu -domaincontroller DOMCTRLR.college.school.tech -WhatIf:$false -Confirm:$false } If ($recipient.RecipientTypeDetails –eq 'RemoteUserMailbox') { Remove-RemoteMailbox -identity 12345678@school.edu -domaincontroller DOMCTRLR.college.school.tech -WhatIf:$false -Confirm:$false } } Out-String
This looks good and I can paste it into PowerShell_ISE and run it successfully.
The top of the Stack Trace was as follows:
at System.Management.Automation.Parser.ReportException(Object targetObject, Type exceptionType, Token errToken, String resourceIdAndErrorId, Object[] args)
at System.Management.Automation.Tokenizer.Require(TokenId tokenId, String resourceIdAndErrorId, Object[] args)
at System.Management.Automation.Parser.statementBlockRule()
at System.Management.Automation.Parser.IfStatementRule()
at System.Management.Automation.Parser.StatementRule()
After some experimenting I found that the parser prefers that commands not be spanned across separate AddScript entries at least when an “If” statement or curly brace (}) is involved. If I concatenate the “If” statements into a single AddScript command such as below the issue is resolved.
1. ps.Commands.Clear() 2. psScriptCmd = "" 3. psScriptCmd = "If ($recipient –ne $null) { " & _"If ($recipient.RecipientTypeDetails –eq 'MailUser') { " & _"Remove-MailUser -identity " & upn & " -domaincontroller " & dnsHostFQDName & " -WhatIf:$false -Confirm:$false } " & _"If ($recipient.RecipientTypeDetails –eq 'RemoteUserMailbox') { " & _"Remove-RemoteMailbox -identity " & upn & " -domaincontroller " & dnsHostFQDName & " -WhatIf:$false -Confirm:$false }}" 4. ps.AddScript("$recipient = Get-Recipient -identity " & upn) 5. ps.AddScript(psScriptCmd) 6. ps.AddCommand("Out-String")
Okay, I can adapt even though breaking the script down with different AddScript statements seems cleaner. The part that I struggle with is that I haven’t found a way to add all the commands like in Line 4 above to the psScriptCmd field where I set the PowerShell variable $recipient. Is there a way to add this line into the psScriptCmd field or is there a way that I can keep everything separate in different AddScript statements?