8
0
Skriptentwickung/test/Custom_Messagebox.ps1
2024-01-24 16:42:38 +01:00

26 lines
4.3 KiB
PowerShell
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#Not everyone likes command linebased arguments. Some people want to be provided with a message box pop-up for
#YES or NO options or information/error messages. In this article, we will discuss how to show pop-up messages using
#PowerShell to serve such purposes. We will focus our discussion on how to present YES/NO, YES/NO/CANCEL, and OK/CANCEL kinds of pop-up messages.
#
#Pop-up messages can be generated in PowerShell using Windows Forms. Although I dont see any problems with them,
#they look a bit old and legacy-like. The Windows Presentation Framework provides similar functionality with rich-looking UIs.
#In this article, we will be using some of the classes from the Windows Presentation Framework assembly to create pop-up messages.
#
#First, you need to import the Presentation Frameworkrelated libraries into the current PowerShell session.
#You can do this using the following Add-Type statement. Without doing this, we cannot call the MessageBox-related classes that help in pop-up generation.
Add-Type -AssemblyName PresentationCore,PresentationFramework
#This helps us access the classes in the Windows Presentation Framework. Now we need to decide what kind of message box we want to show
#(for example, a simple message box with an OK button, or a message box with “Yes” and “No” prompts).
#For demonstration purposes, well assume you want to show a pop-up message box with YES and NO buttons. So well put that in a variable using the following command:
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
#Now decide on the title for the pop-up message and the message you want to display:
$MessageboxTitle = Test pop-up message title
$Messageboxbody = Are you sure you want to stop this script execution?
#Also decide what kind of message you are displaying. Is it an error, a warning, or an informational message? Lets assume it is a warning message:
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
#Now, lets generate a pop-up message box with the data above. This uses the Show method of the System.Windows.MessageBox class.
[System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$messageicon)