使用Powershell脚本进行无提示安装



我正在尝试使用PowerShell静默脚本安装一个客户端的软件。下面是我创建的脚本,它不起作用并抛出如下错误:

无法验证参数"参数列表"上的参数。参数为 null、空或参数集合的元素包含 null 值。提供不包含任何 null 值的集合,然后重试该命令

以下行是否正确或此处有任何错误 我在这里遇到问题。

$Args = @("/S", "/L1033", -INSTALL_TYPE=PRESERVE_VERSION, START_MENU=AStartMenuFolderSoftwareproduction)

完整脚本:

$INSTALLDIR = "C:SoftwareSoftware.exe"
$Args = @("/S", "/L1033", -INSTALL_TYPE=PRESERVE_VERSION, START_MENU=AStartMenuFolderSoftwareproduction)
$logfile = "D:BACKUPInstall_Logfile.txt"
$ErrorActionPreference = 'Stop'
try {
$exitcode = (Start-Process $Installer -ArgumentList $Args -NoNewWindow -Wait -Passthru).ExitCode    
if ($exitcode -eq 0) {
[System.Windows.MessageBox]::Show('Installation Completed Successfully')
} else {
[System.Windows.MessageBox]::Show('Installation Failled')
}
} catch {
"$_" | Out-File $logfile -Append
{[System.Windows.MessageBox]::Show('Installation Failled')}
}

编辑:

$Installer = "C:OTEOTE.exe"
$params = @("/S", "/L1033", "-INSTALL_TYPE=PRESERVE_VERSION", "-START_MENU=AStartMenuFolderOTEproduction")
$logfile = "C:Install_Logfile.txt"
$ErrorActionPreference = 'Stop'
& $Installer @params
if ($LastExitCode -eq 0) {
[Windows.MessageBox]::Show('Installation Completed Successfully')
} else {
"$_" | out-file $logfile -append
[Windows.MessageBox]::Show('Installation Failled')
}

在上面的脚本中,我收到如下错误,

无法验证参数"参数列表"上的参数。参数为 null、空或参数集合的元素包含 null 值。提供不包含任何 null 值的集合,然后重试该命令。

您可能需要在代码中修复以下几点:

  • $args是一个自动变量。不要试图覆盖它。使用不同的变量名称,例如$params.正如其他人已经提到的,在定义数组时将参数放在引号中。
  • 除非您有特定原因使用Start-Process否则使用呼叫运算符和拼接会更容易。
  • 外部程序不会引发 PowerShell 异常,因此对它们使用try/catch毫无意义。
  • PowerShell会自动将外部程序的退出代码记录在自动变量$LastExitCode中。
$installer = 'C:SoftwareSoftware.exe'
$params = '/S', '/L1033', '-INSTALL_TYPE=PRESERVE_VERSION',
'START_MENU=AStartMenuFolderSoftwareproduction'
& $Installer @params
if ($LastExitCode -eq 0) {
[Windows.MessageBox]::Show('Installation Completed Successfully')
} else {
[Windows.MessageBox]::Show('Installation Failled')
}

创建数组时,您的项目需要在引号内:

$Args=@("/S", "/L1033", "-INSTALL_TYPE=PRESERVE_VERSION", "START_MENU=AStartMenuFolderSoftwareproduction")

最新更新