Powershell - How to Display a Message and Save User Input to Log File

Powershell - How to Display a Message and Save User Input to Log File

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

Antwort1

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)

Antwort2

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

verwandte Informationen