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で任意のキーを押下する

Posted at

はじめに

PowerShellで任意のキーを押下するには、System.Windows.Forms.SendKeysクラスを使用します。

[System.Windows.Forms.SendKeys]::SendWait("%{F15}")

この機能を使って、定期的にキーを送信するスクリプトを作成できます。

具体的な実装例

以下は、F15キーを定期的に送信するPowerShellスクリプトの例です:

# f15.ps1
Add-Type -AssemblyName System.Windows.Forms

while ($true) {
    [System.Windows.Forms.SendKeys]::SendWait("%{F15}")
    $d = (Get-Random -Minimum 60 -Maximum 180)
    Start-Sleep -Seconds $d
}

このスクリプトは以下の動作をします:

  • F15キーを送信
  • 60秒から180秒の間でランダムな時間待機
  • 上記を無限ループで繰り返し

バッチファイルでの実行

PowerShellスクリプトを隠れて実行するためのバッチファイルを作成します:

@echo off
powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File C:\Path\To\f15.ps1

このバッチファイルを実行することで、PowerShellウィンドウを表示せずにバックグラウンドでスクリプトが動作します。

なぜこんなことを?

このようなツールが必要になる理由について、よいこは「マウスジグラー」で調べてみましょう。

動作環境

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.26100.7462
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.26100.7462
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
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?