0
0

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 Scriptを使ってWindowsの画面ロックが掛からないようにする方法

Posted at

まだ先ですがVBScriptがデフォルトで無効になるとのことで
VBScriptを使ってWindowsの画面ロックが掛からないようにする方法
https://qiita.com/vantline/items/251adf0d96e058e495bf
こちらをPowershellで動かすようにしたものです。

param (
    [int]$sleepTime = 60000,
    [string]$sendKey = '{F16}'
)

function Check-Processes {
    param (
        [string]$scriptName
    )
    $query = "SELECT * FROM Win32_Process WHERE (Name = 'powershell.exe' OR Name = 'pwsh.exe') AND CommandLine LIKE '%$scriptName%'"
    return Get-WmiObject -Query $query
}

function Stop-Script {
    param (
        [string]$key,
        [int]$sleepTime
    )

    Add-Type -AssemblyName System.Windows.Forms
    $wshell = New-Object -ComObject wscript.shell
    while ($true) {
        $wshell.SendKeys($key)
        Start-Sleep -Milliseconds $sleepTime
    }
}

$key = $sendKey
$sleepTimeInSeconds = $sleepTime / 1000
$processes = Check-Processes -scriptName $MyInvocation.MyCommand.Name

if ($processes.Count -gt 1) {
    $message = "送信処理を停止します。"
    $result = [System.Windows.Forms.MessageBox]::Show($message, "確認", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question)

    if ($result -eq [System.Windows.Forms.DialogResult]::Yes) {
        $cnt = 0
        foreach ($process in $processes) {
            $cnt++
            if ($cnt -ne $processes.Count) {
                $process.Terminate()
            }
        }
        exit 0
    }
} else {
    $message = "$sleepTimeInSeconds 秒毎に $key を送信します。"
    $result = [System.Windows.Forms.MessageBox]::Show($message, "確認", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question)

    if ($result -eq [System.Windows.Forms.DialogResult]::Yes) {
        Stop-Script -key $key -sleepTime $sleepTime
    }
}

exit 0

ショートカットを作成、リンク先を

powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\Scripts\SendKeyScript.ps1"

などにする。
用法用量を守って正しくお使いください。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?