首页 > 解决方案 > 模拟 PowerShell 脚本中的选择选项

问题描述

我正在使用 PowerShell 脚本安装监视代理,该脚本要求我在运行脚本期间从一组选项中进行选择(幸运的是,我需要的选项是默认选项,因此在手动运行时,我只需按“Enter”并继续。

现在我正在使用下面的脚本,它显示了在最后一行之后选择的选项。如何模拟 Enter 按钮以便脚本成功完成。我尝试使用下面的 sendkeys(~) 代码,但它不起作用。

[Net.ServicePointManager]::SecurityProtocol = 'tls12, tls'
        (New-Object System.Net.WebClient).DownloadFile("https://github.com/newrelic/newrelic-cli/releases/latest/download/NewRelicCLIInstaller.msi", "$env:TEMP\\NewRelicCLIInstaller.msi")
        
        msiexec.exe /qn /i $env:TEMP\\NewRelicCLIInstaller.msi | Out-Null


        
        $env:NEW_RELIC_API_KEY='NRAKABC'
        $env:NEW_RELIC_ACCOUNT_ID='ABC'
        

        # I Think it is prompting for input when userdata reaches this point
        & 'C:\\Program Files\\New Relic\\New Relic CLI\\newrelic.exe' install --skipLoggingInstall

    $wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Administrator: Windows PowerShell')
Sleep 1
        
$wshell.SendKeys('~')

我将上面的脚本插入到厨师食谱中并从那里运行。知道如何解决这个问题。TIA

标签: powershellchef-infra

解决方案


所以解决方案是我们必须在安装新的 relic 基础设施代理后通过 -y。-y 当你通过它时假设都是肯定的。

无需使用发送键方法或任何其他方法来模拟输入。下面的代码工作正常。

[Net.ServicePointManager]::SecurityProtocol = 'tls12, tls'
        (New-Object System.Net.WebClient).DownloadFile("https://github.com/newrelic/newrelic-cli/releases/latest/download/NewRelicCLIInstaller.msi", "$env:TEMP\\NewRelicCLIInstaller.msi")
        
        msiexec.exe /qn /i $env:TEMP\\NewRelicCLIInstaller.msi | Out-Null


        
        $env:NEW_RELIC_API_KEY='NRAKABC'
        $env:NEW_RELIC_ACCOUNT_ID='ABC'
        

        # I Think it is prompting for input when userdata reaches this point
        & 'C:\\Program Files\\New Relic\\New Relic CLI\\newrelic.exe' install -y 
    --skipLoggingInstall

推荐阅读