Windows下的滑鼠自動移動程式(免安裝任何程式)
最近有需要上網觀看一些線上學習課程,依據SCORM標準規範必須監控學習過程,上網查了一些資訊,要達成在Windows作業系統下自動移動滑鼠,都需要下載安裝特定的執行檔(這種小程式好像在疫情期間特別興盛,可能是老闆想要遠端監控你有沒有乖乖的在家上班),但我怕有病毒不想下載。順手利用powershell
撰寫了一個滑鼠定時自動移動的程式碼,供給有需要的人參考使用。
滑鼠定時自動移動程式碼
- 請開啟記事本,將下列程式碼貼上:
param (
[int]$iSeconds = 10
)
Clear-Host
$host.ui.RawUI.WindowTitle = "@ Mouse Mover @"
$Host.UI.RawUI.BackgroundColor = "Black"
$Host.UI.RawUI.ForegroundColor = "White"
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
Write-Host "_ _ ____ _ _ ____ ____ _ _ ____ _ _ ____ ____ " -ForegroundColor Magenta
Write-Host "|\/| | | | | [__ |___ |\/| | | | | |___ |__/ " -ForegroundColor Magenta
Write-Host "| | |__| |__| ___] |___ | | |__| \/ |___ | \ " -ForegroundColor Magenta
Write-Host ""
Write-Host "This program will continuously move your mouse cursor at an interval of every " -NoNewline
Write-Host "$iSeconds" -ForegroundColor Cyan -NoNewline
Write-Host " seconds."
Write-Host ""
Write-Host "Just " -NoNewline
Write-Host "CLOSE" -ForegroundColor Red -NoNewline
Write-Host " this window to " -NoNewline
Write-Host "STOP" -ForegroundColor Green -NoNewline
Write-Host " mouse auto-moving."
Write-Host ""
[int]$iCounter = 6
while($iCounter -gt 0) {
[Console]::SetCursorPosition(0, [Console]::CursorTop)
Write-Host "Starting countdown: $iCounter" -NoNewline
$iCounter -= 1
Start-Sleep -Seconds 1
}
function Hide-ConsoleWindow {
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
'
$oConsole = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($oConsole, 6)
return $null
}
$null = Hide-ConsoleWindow
[Console]::SetCursorPosition(0, [Console]::CursorTop)
Write-Host "... ### Running now ### ..." -NoNewline
Add-Type -AssemblyName System.Windows.Forms
$screenWidth = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width
$screenHeight = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height
while(1) {
$x = Get-Random -Minimum 0 -Maximum $screenWidth
$y = Get-Random -Minimum 0 -Maximum $screenHeight
[Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
Start-Sleep -Seconds $iSeconds
}
儲存文字檔,並將檔名設定成
MouseMover.ps1
放置於桌面。針對檔案點選右鍵,選擇
用powershell執行
即可。
程式碼每間隔10秒鐘會亂數移動一次滑鼠的座標位置,如果不喜歡的話,可以自行修改程式碼的第一行將10
這個數字改掉,例如你想要一分鐘執行一次,可以改成60
。
[int]$iSeconds = 60
如果不想動程式碼的話,可以到桌面的路徑使用powershell
帶入參數的方式執行,例如下方指令指定成每分鐘執行一次:
PS C:\Users\Slashview\Desktop> powershell .\MouseMover.ps1 60
Powershell執行權限問題(提升Powershell權限)
如果啟動執行後出現意外,系統彈出下列這樣之類的訊息,代表你的Powershell的執行權限不足,這時候還是得必須使用管理者
身分來提升權限。
.\MouseMover.ps1 : 因為這個系統上已停用指令碼執行,所以無法載入 C:\Users\Slashview\Desktop\MouseMover.ps1 檔案。如需詳細資訊
,請參閱 about_Execution_Policies,網址為 https:/go.microsoft.com/fwlink/?LinkID=135170。
位於 線路:1 字元:1
+ .\MouseMover.ps1
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
按下Windows + R
,輸入powershell
後,同時按住Ctrl + Shift
並按下Enter
,系統會提示是否使用最高權限執行,點選是
後在視窗中輸入下列指令提升powershell權限:
Set-ExecutionPolicy Unrestricted
系統會彈出下列詢問字串,選擇A
直接允許全部指令碼即可:
執行原則變更
執行原則有助於防範您不信任的指令碼。如果變更執行原則,可能會使您接觸到 about_Execution_Policies 說明主題 (網址為
https:/go.microsoft.com/fwlink/?LinkID=135170) 中所述的安全性風險。您要變更執行原則嗎?
[Y] 是(Y) [A] 全部皆是(A) [N] 否(N) [L] 全部皆否(L) [S] 暫停(S) [?] 說明 (預設值為 "N"): A
完成後再去執行MouseMover.ps1
應該就可以順利執行了。