通过 PowerShell 安装 exe 时替代时间延迟?

Alternative to time delay while installing exe through PowerShell?

我有一个要通过 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() }

这里发生了很多事情。让我解释一下我所做的事情。

  1. 我创建了两个方法签名(here-string 中的位);我们调用的每个函数一个。
  2. 我使用这些签名来创建相应的类型。同样,每种方法一个。
  3. 对于 GetWindowType(它以字符串形式传回标题并需要对 System.Text 的引用),我在 -Using 参数中传入了 System.Text 命名空间。
  4. 在后台,PowerShell 添加了对 SystemSystem.Runtime.InteropServices 的引用,因此无需担心这些。
  5. 我创建了我的字符串大小 ($nChars)、window 指针 ($handle) 和 window 标题缓冲区 ($Buff)
  6. 我通过 type-pointer 调用函数:$foo...$bar...

这是我 运行 所有这些...

时得到的结果

每当我必须调用 Windows API(这不是我真正擅长的)时,我会参考以下两篇文章:

希望对您有所帮助!