I made a program that opens a certain program, then Ctrl+C it after x amount of time.
我现在正在使用这个[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:
通过窗口句柄查找进程ID https://stackoverflow.com/questions/18184654/find-process-id-by-windows-handle
发送信号并非易事,但感谢 @Nemo1024、@KindDragon 和 Stack Overflow,它已经解决了:
我可以向 Windows 上的应用程序发送 ctrl-C (SIGINT) 吗? https://stackoverflow.com/questions/813086/can-i-send-a-ctrl-c-sigint-to-an-application-on-windows
不幸的是,使用我能找到的最佳方法也终止了调用 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"
是的,我知道这非常丑陋。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)