Powershell - 메시지를 표시하고 사용자 입력을 로그 파일에 저장하는 방법

Powershell - 메시지를 표시하고 사용자 입력을 로그 파일에 저장하는 방법

나는 이것을 달성하기 위한 가장 좋고 가장 간단한 방법을 연구하는 데 대부분의 시간을 보냈지만 하나도 없는 것 같습니다. 나는 약간의 조각을 찾았지만 성공적으로 작동시키지 못했습니다. 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 없이 스크립트에 그래픽 요소를 추가하는 방법에 대한 세션을 발표했습니다. 제가 시연한 항목 중 하나는 사용자가 버튼을 클릭하도록 요구하거나 설정된 시간이 지나면 자동으로 닫힐 수 있는 그래픽 팝업입니다. 이것이 익숙하게 들린다면, 그렇습니다. 그것은 우리의 오랜 친구인 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

관련 정보