0

I have spent most the day researching the best and most simplest way to accomplish this and there doesn't seem to be one. I have found bits and pieces but I haven't been able to successfully make them work. I just simply want to display a pop up message to the user by running a Powershell script. I would like to have the script do the following:

1. The popup would give an action for input by the user, 
2. The message will tell them their computer will be restarted to complete a Windows 1809 upgrade, 
3. The user will click ok and the input, along with the username and machine name will be sent to a log file. 
4. The script would have a timer on it that will restart the computer within 30 minutes whether the input is given or not. 

The code below does what I want, minus the timer, but doesn't bring a pop up message to the user. It just gives the message in a Powershell console. How can I get it to do the same thing but in a pop-up message instead?

$HostName = $env:COMPUTERNAME
$PatchLocation = "c:\Temp\"
$LogFile = "responses.log"
$LogPath = $PatchLocation + $LogFile
$date = Get-Date

$response = Invoke-Command -ComputerName $HostName -ScriptBlock{ Read-Host "Your computer will be restarted in 30 minutes. Please save your work. Type 'Agree' If you have saved your work"}

"Response for $HostName is $response - $date"|Out-File -FilePath $LogPath -Append

2 Answers 2

2

Take a look at https://jdhitsolutions.com/blog/powershell/2976/powershell-popup/

At the recent PowerShell Summit I presented a session on adding graphical elements to your script without the need for WinForms or WPF. One of the items I demonstrated is a graphical popup that can either require the user to click a button or automatically dismiss after a set time period. If this sounds familiar, yes, it is our old friend VBScript and the Popup method of the Wscript.Shell object. Because PowerShell can create and use COM objects, why not take advantage of this?

$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
$wshell.Popup("Are you looking at me?",0,"Hey!",48+4)
4
  • thank you for the answer. How would I use this to have the message display for a time frame, and submit the response of the user to a log file? I looked at your link and tested the code. I'm using Version 5.0 so that code doesn't work with what I need to do.
    – webby68
    Commented Dec 17, 2019 at 14:50
  • I see nothing about the suggestion that would not support PowerShell 5.1 (the current and only version of PowerShell built-into Windows 10 1511+). You might have better luck using a Windows notification. Instead of trying to create something in PowerShell, like creating create a popup that automatically is dismissed, something like Software Center might be easier. in order to dismiss a popup, which will block the executaiton of a PowerShell script, you would have to implement it multi-threaded which will be tough.
    – Ramhound
    Commented Dec 17, 2019 at 15:21
  • $wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop $result = $wshell.Popup("Are you looking at me?",10,"Hey!",48+4) With this code (tested with Powershell 5) the popup will automatically close after 10 seconds. The variable $result will hold a number for the pressed button or -1 if the timeout has happened. There is no counting display for the timeout, if you expect something like this. Commented Dec 17, 2019 at 16:09
  • @HansHubertVogts Thank you for your assistance. This does some of what I wanted but the answer didn't address everything I needed. I edited your answer, giving you credit, to add the rest of the code I needed.
    – webby68
    Commented Dec 18, 2019 at 15:02
0

Giving credit to Hans Hubert Vogts, for the code that provides the popup message. It provides the popup, However, I needed the script to output to a log file. I modified the code to do that. It is below.

#https://jdhitsolutions.com/blog/powershell/2976/powershell-popup/
#For reference

$HostName = $env:COMPUTERNAME
$PatchLocation = "c:\Temp\"
$LogFile = "responses.log"
$LogPath = $PatchLocation + $LogFile
$date = Get-Date

$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop 
$result = $wshell.Popup("Your Computer will be Restarted in 30 Minutes",30,"Hey!",48+4)

"Response for $HostName is $result - $date"|Out-File -FilePath $LogPath -Append

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .