Powershell - メッセージを表示し、ユーザー入力をログ ファイルに保存する方法

Powershell - メッセージを表示し、ユーザー入力をログ ファイルに保存する方法

私は、これを実現するための最良かつ最も簡単な方法を調査するのに 1 日の大半を費やしましたが、どうやらそのような方法はないようです。いくつかの方法は見つけましたが、うまく機能させることができませんでした。Powershell スクリプトを実行して、ユーザーにポップアップ メッセージを表示したいだけです。スクリプトで次の操作を実行したいと思います。

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. 

以下のコードは、タイマーを除いて、必要なことを実行しますが、ユーザーにポップアップ メッセージを表示しません。Powershell コンソールにメッセージを表示するだけです。同じことをポップアップ メッセージで実行するにはどうすればよいですか?

$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

答え1

を見てみましょうhttps://jdhitsolutions.com/blog/powershell/2976/powershell-popup/

最近の PowerShell Summit で、WinForms や WPF を必要とせずにスクリプトにグラフィカル要素を追加するセッションを紹介しました。私が実演した項目の 1 つは、ユーザーにボタンをクリックさせるか、または一定時間後に自動的に閉じることができるグラフィカル ポップアップです。聞き覚えがあるように思われるかもしれませんが、これは私たちの古い友人である VBScript と Wscript.Shell オブジェクトの Popup メソッドです。PowerShell は COM オブジェクトを作成して使用できるので、これを利用しない手はありません。

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

答え2

ポップアップ メッセージを提供するコードについては、Hans Hubert Vogts に感謝します。ポップアップは提供されますが、スクリプトをログ ファイルに出力する必要がありました。そのためのコードを変更しました。以下がその例です。

#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

関連情報