簡體   English   中英

使用 PowerShell 將空格字符發送到桌面 - 從睡眠中喚醒屏幕

[英]Using PowerShell to send space character to desktop - to wake screen from sleep

我知道以下 2 行 PowerShell 腳本將空格字符發送到它創建的新“外殼”object。 這確實使我的睡眠屏幕擺脫了沉睡。 但這會立即打開 PowerShell window。 所以,如果屏幕真的睡着了——不是問題。

但是,一天中的某些時候,當屏幕沒有處於睡眠狀態時,它會運行。 因此,當這種情況發生時,用戶會看到創建的 Wscript.Shell 的一個非常短的惱人 flash

我只想將空格鍵發送到桌面 - 喚醒睡眠屏幕。 在沒有 Wscript.Shell 的瞬時打開和關閉的情況下,將“”字符發送到“桌面”而不是新的 shell object。 這可能嗎? 我對編寫 PowerShell 一無所知(顯然)。

$myshell=New-Object -com "Wscript.Shell" 
$myshell.sendkeys(" ")

也許有一種方法 PowerShell 腳本可以知道屏幕和/或計算機是否沒有處於睡眠狀態 state,因此這一切都可以避免在這種情況下運行? 任何幫助深表感謝。

它正在使用 Powershell 腳本進行設置,以在任務調度程序中創建任務。 該任務然后調用 2 行 PowerShell 腳本來發送“”鍵,以便在當天活動之前喚醒計算機。

我找到了我的答案。 由於我使用的是 AutoHotKey,以下代碼將使用 PowerShell 在任務調度程序中創建一個新任務。 通過使用cmd /c start /min "" powershell...樣式的命令,彈出 window 不會顯示 - 但計算機和屏幕會喚醒。

WakeUp:= A_Now
EnvAdd WakeUp, 10, Minutes       ; wake 10 minutes from now
UTC_Delta-= A_NowUTC, Seconds    ; Seconds is more accurate due to rounding issue
UTC_Delta:= Round(-UTC_Delta/60) ; Round to nearest minute to ensure accuracy
WakeUp   += UTC_Delta, Minutes   ; Apply offset to convert to UTC
FormatTime, WakeTime,% WakeUp, "yyyy-MM-ddTHH:mm:ssZ"
psScript =
(
$action = New-ScheduledTaskAction -Execute cmd -Argument '/c start /min "" powershell -WindowStyle Hidden -ExecutionPolicy Bypass -command {$myshell=New-Object -com "Wscript.Shell" $myshell.sendkeys(" ")}'
$settings = New-ScheduledTaskSettingsSet -WakeToRun
$trigger = New-ScheduledTaskTrigger -Once -At %WakeTime%
Register-ScheduledTask -TaskName wakeup -Action $action -Settings $settings -Trigger $trigger -Force
)
RunWait PowerShell.exe -Command &{%psScript%} ,, hide
;use the following if you want to see PowerShell output
;Run PowerShell.exe -NoExit -Command &{%psScript%}

這通過鼠標移動而不是鍵盤喚醒屏幕,但似乎對我有用。

基於windows OS 的電源選項或注冊表中的屏幕喚醒設置?

$Signature = @"
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, 
IntPtr lParam);

[DllImport("user32.dll")]
public static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);
"@

Try {
  $ShowWindowAsync = Add-Type -MemberDefinition $Signature -Name 
"Win32ShowWindowAsync" -Namespace Win32Functions -PassThru -ErrorAction Ignore }
Catch { }

$MONITOR_ON = -1;
$MONITOR_OFF = 2;
$MONITOR_STANBY = 1;

[System.Int64]$MOUSEEVENTF_MOVE = 0x0001;

[System.IntPtr]$HWND_BROADCAST = New-Object System.IntPtr(0xffff)
[System.UInt32]$WM_SYSCOMMAND = 0x0112
[System.IntPtr]$SC_MONITORPOWER = New-Object System.IntPtr(0xF170)

# this commands puts monitors to sleep
# $ShowWindowAsync::SendMessage($HWND_BROADCAST, $WM_SYSCOMMAND, $SC_MONITORPOWER, [System.IntPtr]$MONITOR_OFF);

# this command wakes monitors up
$ShowWindowAsync::mouse_event($MOUSEEVENTF_MOVE, 0, 1, 0, [System.UIntPtr]::Zero);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM