Skip to content

Instantly share code, notes, and snippets.

@jmconway
Last active May 11, 2021 18:10
Show Gist options
  • Save jmconway/2e96b959fcc4c4a06ca23bb5f65e0c77 to your computer and use it in GitHub Desktop.
Save jmconway/2e96b959fcc4c4a06ca23bb5f65e0c77 to your computer and use it in GitHub Desktop.
Basic user logon script to show a warning window using a bit of .NET. My use case initially was just to get users to acknowledge the prompt, so my "exit" command can be replaced with commands you want run depending on what the user selects. See Microsoft Docs article in the comments for more on prompt options with .NET.
#-----------------------------------------------
# Warning Prompt - Windows User Logon Script
#-----------------------------------------------
# Set some variables that we'll use to craft the prompt in the next section
$message = ""
$title = ""
# We'll use a bit of .NET framework here...
# First, we have to add the assembly as it does not exist by default in PowerShell
Add-Type -AssemblyName PresentationFramework
# Now we can use the MessageBox class in the System.Windows namespace
## See https://docs.microsoft.com/en-us/dotnet/api/system.windows.messagebox?view=netframework-4.7.2
# Show a prompt using the Show method of the MessageBox class
# This constructor show's our message with a title, "Yes" or "No" buttons, and a warning icon
$prompt = [System.Windows.MessageBox]::Show($message, $title, 'OkCancel', 'Warning')
# Depending on which button the user clicks...
switch ($prompt) {
# If Okay, disappear and end script
"Ok" {
exit
} #If Cancel, disappear and end script
"Cancel" {
exit
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment