使用 Powershell 脚本进行静默安装 [英] Silent installation by using Powershell scripting
问题描述
我正在尝试使用 PowerShell 静默脚本安装一个客户端的软件.下面是我创建的脚本,它不工作并抛出如下错误:
I am trying to install one client's software by using PowerShell silent scripting. Below is the script which I have created and its not working and throwing errors like below:
无法验证参数ArgumentList"上的参数.参数为空、为空,或者参数集合的元素包含空值.提供一个不包含任何空值的集合,然后重试该命令
Cannot validate argument on parameter 'ArgumentList'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again
下面这行是正确的还是有任何错误在这里我遇到了问题.
Is the below line correct or any mistake in this Here I am getting issue.
$Args = @("/S", "/L1033", -INSTALL_TYPE=PRESERVE_VERSION, START_MENU=AStartMenuFolder\Software\production\)
完整脚本:
$INSTALLDIR = "C:\Software\Software.exe"
$Args = @("/S", "/L1033", -INSTALL_TYPE=PRESERVE_VERSION, START_MENU=AStartMenuFolder\Software\production\)
$logfile = "D:\BACKUP\Install_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:\OTE\OTE.exe"
$params = @("/S", "/L1033", "-INSTALL_TYPE=PRESERVE_VERSION", "-START_MENU=AStartMenuFolder\OTE\production\")
$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')
}
在上面的脚本中,我收到如下错误,
In the above script I am getting the error like below,
无法验证参数ArgumentList"上的参数.参数为空、为空,或者参数集合的元素包含空值.提供一个不包含任何空值的集合,然后重试该命令.
Cannot validate argument on parameter 'ArgumentList'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again.
推荐答案
您可能需要在代码中修复几个问题:
There are several things you may want to fix in your code:
$args
是一个 自动变量.不要试图覆盖它.使用不同的变量名,例如$params
.正如其他人已经提到的,在定义数组时将参数放在引号中.- 除非您有使用
Start-Process
的特定原因,否则使用 呼叫运算符 和 splatting. - 外部程序不会抛出 PowerShell 异常,因此对它们使用
try
/catch
毫无意义. - PowerShell 自动将外部程序的退出代码记录在自动变量
$LastExitCode
中.
$args
is an automatic variable. Don't try to overwrite it. Use a different variable name, e.g.$params
. As others have already mentioned, put the parameters in quotes when defining the array.- Unless you have specific reasons to use
Start-Process
it's easier to use the call operator and splatting. - External programs don't throw PowerShell exceptions, so using
try
/catch
on them is pointless. - PowerShell automatically records the exit code of an external program in the automatic variable
$LastExitCode
.
$installer = 'C:\Software\Software.exe'
$params = '/S', '/L1033', '-INSTALL_TYPE=PRESERVE_VERSION',
'START_MENU=AStartMenuFolder\Software\production'
& $Installer @params
if ($LastExitCode -eq 0) {
[Windows.MessageBox]::Show('Installation Completed Successfully')
} else {
[Windows.MessageBox]::Show('Installation Failled')
}
这篇关于使用 Powershell 脚本进行静默安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!