Hi Everyone, I’m looking for help from the community…I cobbled together a script partially found online but customized for our purposes. We frequently get asked to help our customers troubleshoot issues with scanners so i thought a popup with the steps the user can take would help to decrease phone calls.
The script below runs fine when executed from the script library and sent to an asset. And it runs when i paste it into powershell on my Windows10 desktop. However i would like it to be run by the user executing a one-liner shortcut on the Tray icon, that they can trigger on demand; like we do with other things such as clearing the printer queue.
I have tried and cannot get it to work. hopefully someone else can see what i am missing. Thanks in advance!
# Minimize all objects and applications on the screen
$shell = New-Object -ComObject "Shell.Application"
$shell.minimizeall()
# Load the .NET Framework classes
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build out your custom message box
# .Text is your Message Title
# .Size is your Box size measured in pixels (Width, Height)
$form = New-Object System.Windows.Forms.Form
$form.Text = 'SCANNER RESET INSTRUCTIONS'
$form.Size = New-Object System.Drawing.Size(420,200)
$form.StartPosition = 'CenterScreen'
# Create your Yes button
# .Location is your starting point (Pixels from Left, Pixels from Top)
# .Size is your button size (Width, Height)
# .Text is your Button Text
$yesButton = New-Object System.Windows.Forms.Button
$yesButton.Location = New-Object System.Drawing.Point(67,130)
$yesButton.Size = New-Object System.Drawing.Size(100,23)
$yesButton.Text = 'Restart Now'
$yesButton.DialogResult = [System.Windows.Forms.DialogResult]::Yes
$form.AcceptButton = $yesButton
$form.Controls.Add($yesButton)
# Create your No button
# .Location is your starting point (Pixels from Left, Pixels from Top)
# .Size is your button size (Width, Height)
# .Text is your Button Text
$noButton = New-Object System.Windows.Forms.Button
$noButton.Location = New-Object System.Drawing.Point(233,130)
$noButton.Size = New-Object System.Drawing.Size(100,23)
$noButton.Text = 'Restart Later'
$noButton.DialogResult = [System.Windows.Forms.DialogResult]::No
$form.CancelButton = $noButton
$form.Controls.Add($noButton)
# Create your "Help" button (I will be hiding this button behind the others)
# .Location is your starting point (Pixels from Left, Pixels from Top)
# .Size is your button size (Width, Height)
# .Text is your Button Text
$helpButton = New-Object System.Windows.Forms.Button
$helpButton.Location = New-Object System.Drawing.Point(250,125)
$helpButton.Size = New-Object System.Drawing.Size(1,1)
$helpButton.Text = 'Click Elsewhere'
$helpButton.DialogResult = [System.Windows.Forms.DialogResult]::None
$form.HelpButton = $helpButton
$form.Controls.Add($helpButton)
# Create your message body label.
# .Location is your starting point based on the bottom left of the title block
# .Size is the size of your text block for the message body
# .Text is your message's body!
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,10)
$label.Size = New-Object System.Drawing.Size(800,300)
$label.Text = 'SCANNER PROBLEMS? FOLLOW THESE STEPS:
1: Unplug all cables from behind the scanner.
2: Restart the computer.
3: Login to computer after restarting.
4: When your icons load, plug the cables into the scanner.
5: Try to scan.'
$form.Controls.Add($label)
# Ensure that the Message box is TopMost (always on top of other apps)
$form.Topmost = $true
# Ensure the keyboard focus starts on the useless button
$form.Add_Shown({$helpButton.Select()})
$result = $form.ShowDialog()
# Assign what the "Yes" and "No" Buttons do
if ($result -eq [System.Windows.Forms.DialogResult]::Yes) {
Restart-Computer
} elseif ($result -eq [System.Windows.Forms.DialogResult]::No) {
Return
}
I appreciate the help. tried this many times and i cannot get it to work. Will have to look to try something else. I didnt think a text box popping up with some instructions would be so difficult but im not a coding person.
So when you create a PS1 script, it will look like this:
$code = {
# Minimize all objects and applications on the screen
$shell = New-Object -ComObject "Shell.Application"
$shell.minimizeall()
# Load the .NET Framework classes
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build out your custom message box
# .Text is your Message Title
# .Size is your Box size measured in pixels (Width, Height)
$form = New-Object System.Windows.Forms.Form
$form.Text = 'SCANNER RESET INSTRUCTIONS'
$form.Size = New-Object System.Drawing.Size(420,200)
$form.StartPosition = 'CenterScreen'
# Create your Yes button
# .Location is your starting point (Pixels from Left, Pixels from Top)
# .Size is your button size (Width, Height)
# .Text is your Button Text
$yesButton = New-Object System.Windows.Forms.Button
$yesButton.Location = New-Object System.Drawing.Point(67,130)
$yesButton.Size = New-Object System.Drawing.Size(100,23)
$yesButton.Text = 'Restart Now'
$yesButton.DialogResult = [System.Windows.Forms.DialogResult]::Yes
$form.AcceptButton = $yesButton
$form.Controls.Add($yesButton)
# Create your No button
# .Location is your starting point (Pixels from Left, Pixels from Top)
# .Size is your button size (Width, Height)
# .Text is your Button Text
$noButton = New-Object System.Windows.Forms.Button
$noButton.Location = New-Object System.Drawing.Point(233,130)
$noButton.Size = New-Object System.Drawing.Size(100,23)
$noButton.Text = 'Restart Later'
$noButton.DialogResult = [System.Windows.Forms.DialogResult]::No
$form.CancelButton = $noButton
$form.Controls.Add($noButton)
# Create your "Help" button (I will be hiding this button behind the others)
# .Location is your starting point (Pixels from Left, Pixels from Top)
# .Size is your button size (Width, Height)
# .Text is your Button Text
$helpButton = New-Object System.Windows.Forms.Button
$helpButton.Location = New-Object System.Drawing.Point(250,125)
$helpButton.Size = New-Object System.Drawing.Size(1,1)
$helpButton.Text = 'Click Elsewhere'
$helpButton.DialogResult = [System.Windows.Forms.DialogResult]::None
$form.HelpButton = $helpButton
$form.Controls.Add($helpButton)
# Create your message body label.
# .Location is your starting point based on the bottom left of the title block
# .Size is the size of your text block for the message body
# .Text is your message's body!
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,10)
$label.Size = New-Object System.Drawing.Size(800,300)
$label.Text = 'SCANNER PROBLEMS? FOLLOW THESE STEPS:
1: Unplug all cables from behind the scanner.
2: Restart the computer.
3: Login to computer after restarting.
4: When your icons load, plug the cables into the scanner.
5: Try to scan.'
$form.Controls.Add($label)
# Ensure that the Message box is TopMost (always on top of other apps)
$form.Topmost = $true
# Ensure the keyboard focus starts on the useless button
$form.Add_Shown({$helpButton.Select()})
$result = $form.ShowDialog()
# Assign what the "Yes" and "No" Buttons do
if ($result -eq [System.Windows.Forms.DialogResult]::Yes) {
Restart-Computer
} elseif ($result -eq [System.Windows.Forms.DialogResult]::No) {
Return
}
}
[convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))
Then you open Powershell as an Admin, navigate to the folder of your PS1 script, and run the .ps1 script and get this result: