gpt4 book ai didi

powershell - 通过PowerShell安装exe时可以替代时间延迟吗?

转载 作者:行者123 更新时间:2023-12-03 01:04:52 25 4
gpt4 key购买 nike

我有一个试图通过PowerShell安装的软件exe。一切正常。我正在使用SendKeys浏览安装GUI。我给了两个SendKeys命令之间一些延迟,因为软件在两个步骤之间花费了一些时间,但是安装时间因计算机而异。

我的问题是如何绕过SendKeys中的此时间延迟依赖关系?我尝试了AppActivate,但对我来说毫无用处。是否有其他办法可以延误?

最佳答案

当然。

我已经将Nitesh's C# function转换为Powershell脚本

$signature_user32_GetForegroundWindow = @"
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
"@

$signature_user32_GetWindowText = @"
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
"@

$foo = `
Add-Type -MemberDefinition $signature_user32_GetForegroundWindow `
-Name 'user32_GetForegroundWindow' `
-Namespace 'Win32' `
-PassThru

$bar = `
Add-Type -MemberDefinition $signature_user32_GetWindowText `
-Name 'user32_GetWindowText' `
-Namespace 'Win32' `
-Using System.Text `
-PassThru

[int]$nChars = 256
[System.IntPtr] $handle = New-object 'System.IntPtr'
[System.Text.StringBuilder] $Buff = New-Object 'System.Text.StringBuilder' `
-ArgumentList $nChars

$handle = $foo::GetForegroundWindow()
$title_character_count = $bar::GetWindowText($handle, $Buff, $nChars)
If ($title_character_count -gt 0) { Write-Output $Buff.ToString() }

这里有很多事情。雷姆解释了我的所作所为。
  • 我创建了两个方法签名(here-string中的位);每个我们要调用的函数。
  • 我使用这些签名来创建相应的类型。同样,每种方法一个。
  • 对于GetWindowType(它将标题以字符串形式传递回来,并且需要对System.Text的引用),我在System.Text参数中传入-Using命名空间。
  • 在后台,PowerShell添加了对SystemSystem.Runtime.InteropServices的引用,因此无需担心它们。
  • 我创建我的字符串大小($nChars),窗口指针($handle)和窗口标题缓冲区($Buff)
  • 我通过类型指针调用函数:$foo...$bar...

  • 这是我运行所有这些程序后得到的...

    enter image description here

    每当我必须调用Windows API时(这并不是我的事),我都会引用以下两篇文章:
  • PowerShell P/Invoke Walkthrough
  • Use PowerShell to Interact with the Windows API: Part 1

  • 我希望这有帮助!

    关于powershell - 通过PowerShell安装exe时可以替代时间延迟吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50830501/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com