1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PowerShell】Windowsのスクリーンセーバーのロックを回避

Posted at

【PowerShell】

■はじめに

  • この記事は?
    Windowsのスクリーンセーバーのロックを回避するスクリプトを解説

■スクリプトの概要

  • 何をするスクリプトか?
    マウスを常に動かして、Windowsのスクリーンセーバーのロックを回避
  • どんな場面で役に立つか?
    複数のPCで作業時に、未操作のPCの画面ロックを回避する

■動作確認環境

  • Windows 11 Pro 24H2 (OS ビルド 26100.4351)
  • PowerShell 5.1.26100.2161

■スクリプトのソースコード

furufuru.ps1
#################################################################################
# furufuru                       
#################################################################################
function furufuru {
<#
.SYNOPSIS
    スクリーンセーバー・ロック回避用マウス移動ツール
.DESCRIPTION
    一定間隔でマウスカーソルをわずかに動かし、スクリーンセーバーや画面ロックを防止します。
.EXAMPLE
    furufuru
.NOTES
    Ctrl+C で停止してください。
#>

    # .NETのCursorクラスを利用するためにSystem.Windows.Formsをロード
    Add-Type -AssemblyName System.Windows.Forms

    # mouse_event の定義(既に定義済みならスキップ)
    if (-not ("Win32Functions.Win32MouseEventNew" -as [type])) {
        $signature = @'
            [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'@
        Add-Type -MemberDefinition $signature -Name "Win32MouseEventNew" -Namespace Win32Functions -PassThru | Out-Null
    }

    Write-Host "マウスふるふるを起動します。"
    Write-Host "Ctrl+Cで終了します。"

    # 定数・変数定義
    $MOUSEEVENTF_MOVE = 0x00000001
    $SleepSec = 50
    $MoveMouseDistance = 1
    $MoveMouseDistanceX = 1
    [bool]$Flag = $true

    try {
        while ($true) {
            Start-Sleep -Seconds $SleepSec

            # 現在位置取得
            $x = [System.Windows.Forms.Cursor]::Position.X
            $y = [System.Windows.Forms.Cursor]::Position.Y

            # マウスを左右にわずかに移動(物理的な動き)
            [Win32Functions.Win32MouseEventNew]::mouse_event($MOUSEEVENTF_MOVE, -$MoveMouseDistance, 0, 0, 0)
            [Win32Functions.Win32MouseEventNew]::mouse_event($MOUSEEVENTF_MOVE, $MoveMouseDistance, 0, 0, 0)

            # 座標を変更(アプリの座標監視対策)
            if ($Flag) {
                $x += $MoveMouseDistanceX
            } else {
                $x -= $MoveMouseDistanceX
            }
            $Flag = -not $Flag

            [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
        }
    } finally {
        Write-Host "`nマウスふるふるを停止しました。"
    }
}

# --- 関数実行用 ---
# --- 関数を実行 ---
furufuru
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?