我制作了一个打开某个程序的程序,然后在 x 时间后Ctrl+C它。
我现在正在使用这个[System.Windows.Forms.SendKeys]::SendWait("^{c}")。这个目标是某个窗口还是随机发送到当前窗口?
如何将其更改为某个窗口?
这是我的代码:
Write-Host "Safe Botting V0.1"
Write-Host "Initializing..."
Start-Sleep -s 3
Write-Host "Program started successfully with no errors."
While($true)
{
    Write-Host "Starting bot..."
    Start-Sleep -s 3
    Start-Process -FilePath E:\Documents\bot.exe
    Write-Host "Bot started successfully"
    $rnd = Get-Random -Minimum 1800 -Maximum 10800
    Write-Host "The bot will run for:"
    Write-Host $rnd
    Start-Sleep -s $rnd
    Write-Host "Bot will now stop!"
    [System.Windows.Forms.SendKeys]::SendWait("^{c}") 
    Write-Host "Bot terminated"
    Write-Host "Starting cooldown time"
    $rnb = Get-Random -Minimum 14400 -Maximum 28800
    Write-Host "The bot will cooldown for"
    Write-host $rnb
    Start-Sleep -s $rnb
    Write-Host "Cooldown Finished, Restarting"
    Start-Sleep -s 5
}
如果您有进程 ID,则可以向进程发送 CTRL_C_EVENT 信号。在您的情况下,您可以从 Start-Process 获取(如果您不知道如何获取进程 ID,请阅读文档)。也可以从窗口句柄获取进程 ID:
发送信号很重要,但多亏了 @Nemo1024、@KindDragon 和 Stack Overflow,它已经解决了:
我可以向 Windows 上的应用程序发送 ctrl-C (SIGINT) 吗?
不幸的是,使用我能找到的最佳方法也终止了调用 PowerShell 进程,我能想出的唯一解决方法是从我启动的新 PowerShell 实例发送信号。
在 PowerShell 中,它看起来像这样:
# be sure to set $ProcessID properly. Sending CTRL_C_EVENT signal can disrupt or terminate a process
$ProcessID = 1234
$encodedCommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("Add-Type -Names 'w' -Name 'k' -M '[DllImport(""kernel32.dll"")]public static extern bool FreeConsole();[DllImport(""kernel32.dll"")]public static extern bool AttachConsole(uint p);[DllImport(""kernel32.dll"")]public static extern bool SetConsoleCtrlHandler(uint h, bool a);[DllImport(""kernel32.dll"")]public static extern bool GenerateConsoleCtrlEvent(uint e, uint p);public static void SendCtrlC(uint p){FreeConsole();AttachConsole(p);GenerateConsoleCtrlEvent(0, 0);}';[w.k]::SendCtrlC($ProcessID)"))
start-process powershell.exe -argument "-nologo -noprofile -executionpolicy bypass -EncodedCommand $encodedCommand"
是的,我知道这很丑陋。
| 归档时间: | 
 | 
| 查看次数: | 4925 次 | 
| 最近记录: |