Forum Discussion
Trying to get a ScriptBlock to End the whole script from running? Is there a way to do this?
Hello PS Community,
I'm trying to utilize the below ScriptBlock for my msgBox. I created a Switch if Cancel is clicked it will exit the ScriptBlock but I want it to exit the whole Script, is there a way to do this? or should I just take my code out of the ScriptBlock and run it normally?
Thank you,
My Code:
Hi Vinod_5,
Close I was trying to get the script block of the msgbox to run and upon clicking cancel or "x" the script would exit completely.
So I recreated the above Script block for MsgBox to return a value to the $value variable, upon returning to the main function an if statement would check $value for a 0, and if it has a 0, it will exit the script
My Code:
# MsgBox ScriptBlock #
$MsgBox = {
Add-Type -AssemblyName PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OKCancel
$MesssageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageBody = "Please save anything open in your current Web Browser, this includes Google Chrome, Microsoft Edge, and Mozilla FireFox and close each one respectively. Click Ok when done saving and closing browsers. IT Will be deleting your Cache and Cookies.
-IT"
$MessageTitle = "Read Me"
$Result = [System.Windows.MessageBox]::Show($MessageBody, $MessageTitle,$ButtonType,$MesssageIcon)
Switch ($Result){
'Ok' {
Return "1"
}
'Cancel' {
Return "0"
}
}
}
# Invoke MsgBox as CurrentUser. #
$value = (invoke-ascurrentuser -scriptblock $MsgBox -CaptureOutput).TrimEnd()
if($value -eq '0'){
Write-Host "Script Exiting"
Write-Output "Script Exiting" >> C:\Users\$user\AppData\Local\Temp\Cookie_Cache.log
Exit
}Thank you for the suggestion Vinod!
2 Replies
- Vinod_5Copper ContributorHi tsmith92765,
Good day!
I feel your requirement is to display a certain message using message box in such a way that when user clicks buttons other than OK, which includes 'X' ( close message box), cancel button, message box should reappear with similar message until user clicks on OK button.
If yes, I wrote the below script. This script holds good even if user clicks on 'X' button in message box. Please try it.
$Mainfunction ={
$MsgBox1 = {
Add-Type - Assembly ame PresentationFramework
$ButtonType1 = [System.Windows.MessageBoxButton]:: OKCANCEL
$MessageIcon1 = [System.windows.messageboxbutton Image] :: Warning
$MessageBody_repeat = "Please do not ignore this message.
Please save anything open in your current Web browser, this includes Google chrome, Microsoft Edge, and Mozilla Firefox and close each one respectively. Click OK when done saving and closing browsers. It will be deleting your cache and cookies.
- IT"
$MessageTitle1 = "ReadMe"
$Result1 = [System.Windows.MessageBox]::Show($MessageBody_repeat,$MessageTitle1,$ButtonType1,$MessageIcon1)
Switch ( $Result1)
{'OK' {exit 0}
'Cancel' {&MsgBox1}
}
}
$MsgBox = {
Add-Type - Assembly ame PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]:: OKCANCEL
$MessageIcon = [System.windows.messageboxbutton Image] :: Warning
$MessageBody = "Please save anything open in your current Web browser, this includes Google chrome, Microsoft Edge, and Mozilla Firefox and close each one respectively. Click OK when done saving and closing browsers. It will be deleting your cache and cookies.
- IT"
$MessageTitle = "ReadMe"
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
Switch ( $Result)
{'OK' {exit 0}
'Cancel' {&MsgBox1}
}
}
&MsgBox
}
&Mainfunction
Please let me know if any issues or if my understanding is not as per your requirement. I will be happy to try once again. Thank you.- tsmith92765Copper Contributor
Hi Vinod_5,
Close I was trying to get the script block of the msgbox to run and upon clicking cancel or "x" the script would exit completely.
So I recreated the above Script block for MsgBox to return a value to the $value variable, upon returning to the main function an if statement would check $value for a 0, and if it has a 0, it will exit the script
My Code:
# MsgBox ScriptBlock #
$MsgBox = {
Add-Type -AssemblyName PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OKCancel
$MesssageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageBody = "Please save anything open in your current Web Browser, this includes Google Chrome, Microsoft Edge, and Mozilla FireFox and close each one respectively. Click Ok when done saving and closing browsers. IT Will be deleting your Cache and Cookies.
-IT"
$MessageTitle = "Read Me"
$Result = [System.Windows.MessageBox]::Show($MessageBody, $MessageTitle,$ButtonType,$MesssageIcon)
Switch ($Result){
'Ok' {
Return "1"
}
'Cancel' {
Return "0"
}
}
}
# Invoke MsgBox as CurrentUser. #
$value = (invoke-ascurrentuser -scriptblock $MsgBox -CaptureOutput).TrimEnd()
if($value -eq '0'){
Write-Host "Script Exiting"
Write-Output "Script Exiting" >> C:\Users\$user\AppData\Local\Temp\Cookie_Cache.log
Exit
}Thank you for the suggestion Vinod!