Powershell: cómo mostrar un mensaje y guardar la entrada del usuario en un archivo de registro

Powershell: cómo mostrar un mensaje y guardar la entrada del usuario en un archivo de registro

He pasado la mayor parte del día investigando la mejor y más sencilla forma de lograr esto y no parece haber ninguna. He encontrado fragmentos pero no he podido hacerlos funcionar con éxito. Simplemente quiero mostrar un mensaje emergente al usuario ejecutando un script de Powershell. Me gustaría que el script hiciera lo siguiente:

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. 

El siguiente código hace lo que quiero, menos el temporizador, pero no muestra un mensaje emergente al usuario. Simplemente da el mensaje en una consola Powershell. ¿Cómo puedo hacer que haga lo mismo pero en un mensaje emergente?

$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

Respuesta1

Echa un vistazo ahttps://jdhitsolutions.com/blog/powershell/2976/powershell-popup/

En la reciente Cumbre de PowerShell presenté una sesión sobre cómo agregar elementos gráficos a su script sin la necesidad de WinForms o WPF. Uno de los elementos que demostré es una ventana emergente gráfica que puede requerir que el usuario haga clic en un botón o cerrarse automáticamente después de un período de tiempo determinado. Si esto le suena familiar, sí, es nuestro viejo amigo VBScript y el método Popup del objeto Wscript.Shell. Dado que PowerShell puede crear y utilizar objetos COM, ¿por qué no aprovechar esto?

$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
$wshell.Popup("Are you looking at me?",0,"Hey!",48+4)

Respuesta2

Le damos crédito a Hans Hubert Vogts por el código que proporciona el mensaje emergente. Proporciona la ventana emergente. Sin embargo, necesitaba que el script se generara en un archivo de registro. Modifiqué el código para hacer eso. Está debajo.

#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

información relacionada