我有一个 PowerShell 脚本,它通过模拟击键执行和单步执行 NSIS 安装程序。我想找到一种在我的 Jenkins 声明式管道中调用此 PowerShell 脚本的方法。
$installerPath = $args[0]
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
Write-Output "[INFO] Starting installer at: " $installerPath
$installerProcess = Start-Process -FilePath $installerPath -PassThru
$installerWindowName = "Installer Setup"
Write-Output "[INFO] Wait for installer to initialize"
while (-not ($installerProcess.MainWindowTitle -match "$installerWindowName")) {
Start-Sleep -Milliseconds 100
$installerProcess.Refresh()
}
Write-Output "[INFO] Bring installer window to focus"
[Microsoft.VisualBasic.Interaction]::AppActivate($installerWindowName)
Write-Output "[INFO] - Welcome to Installer"
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Write-Output "[INFO] - License Agreement"
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Write-Output "[INFO] - Choose Location"
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Write-Output "[INFO] - Check Box"
[System.Windows.Forms.SendKeys]::SendWait("{DOWN}")
[System.Windows.Forms.SendKeys]::SendWait(" ")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Write-Output "[INFO] - Wait for Installation"
Start-Sleep -Seconds 100
Write-Output "[INFO] - Confirm Installation Complete"
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Write-Output "[INFO] Exit Installer"
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
注意:我无法使用“静默”标志 (/S) 进行安装,因为默认情况下我必须与其他安装程序交互。所以我必须在安装程序中选中一个框以不包含该弹出窗口。
试过...
使用此行以普通权限命令提示符调用脚本可以完美地按预期工作(这样我要安装的对象完成并存在于预期的目的地):
powershell -NoProfile -ExecutionPolicy Unrestricted -Command "Start-Process Powershell -Verb RunAs -Wait -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File C:\full\path\to\script\myScript.ps1 C:\full\path\to\installer\myInstaller.exe -Wait'"
但是,当我尝试在我的管道中运行相同的脚本时,它将无限期执行,直到我手动停止管道。例如这个脚本通常需要大约一分钟才能完成,但它会持续 20 多分钟,我可以看到应该安装的对象不存在:
stage('Run Installer') {
steps {
script {
dir('my-workspace') {
def installerPath = "C:\full\path\to\installer\myInstaller.exe"
def scriptPath = "C:\full\path\to\script\myScript.ps1"
def scriptCall = "-NoProfile -ExecutionPolicy Unrestricted -File ${scriptPath} ${installerPath} -Wait"
def powershellCommand = "Start-Process Powershell -Verb RunAs -Wait -ArgumentList '${scriptCall}'"
// powershell """
// powershell -NoProfile -ExecutionPolicy Unrestricted -Command "${powershellCommand}"
// """
}
}
}
}
有什么想法吗?非常感谢您的宝贵时间!