I am writing a script which checks for a registry value and and exits if is 0. (It will proceed if the value is 1.)
if ((Get-ItemProperty -path HKLM:\SOFTWARE\ICT\LoginScript).proceed -eq 0) {
$form.close()
exit
}
When I run the script with the reg value at 0, it fails to exit and throws an exception instead:
System.Management.Automation.ExitException: System error.
at System.Management.Automation.FlowControlNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
at System.Management.Automation.StatementListNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
...
The script is using windows forms - not sure if that is relevant?
EDIT:
I have reduced the script to the following to test:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.Add_Shown({ $Form.Activate(); start-sleep -s 3; exit; $form.close() })
$Form.ShowDialog()
And this still gives the error. If I run the start-sleep -s 3; exit
code on a non-windows form it works fine, so it looks like the issue is tied in with Windows Forms.
Thanks,
Ben