使用PowerShell的Ruby静默安装
我正在尝试通过PowerShell以静默模式安装Ruby,选项如下:使用PowerShell的Ruby静默安装,powershell,Powershell,我正在尝试通过PowerShell以静默模式安装Ruby,选项如下: echo "Installing Ruby 2.0.0" $ruby_inst_process = Start-Process "C:\Users\guest_new\Downloads\rubyinstaller-2.0.0-p648-x64.exe" /silent /tasks='assocfiles,modpath' -PassThru -Wait if ($ruby_inst_process -ne 0) {
echo "Installing Ruby 2.0.0"
$ruby_inst_process = Start-Process "C:\Users\guest_new\Downloads\rubyinstaller-2.0.0-p648-x64.exe" /silent /tasks='assocfiles,modpath' -PassThru -Wait
if ($ruby_inst_process -ne 0)
{
echo "Ruby 2.0.0 installation failed"
exit 0
}
我得到以下错误:
Start-Process : A positional parameter cannot be found that accepts argument '/tasks=assocfiles,modpath'.
+ ... t_process = Start-Process "C:\Users\guest_new\Downloads\rubyinstaller- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
我不确定我是否遗漏了什么,或者只是使用了不正确的语法。这与powershell如何解释空白有关,它试图将
/tasks=assocfiles,modpath
作为启动进程的参数传递给ruby安装程序。这个问题有两种解决方法。第一个是提供一个-argumentlist
参数,如下所示
Start-Process "C:\Users\guest_new\Downloads\rubyinstaller-2.0.0-p648-x64.exe" -argumentlist @("/silent","/tasks='assocfiles,modpath'") -PassThru -Wait
或者通过使用Invoke Expression
而不是Start Process
,将整个字符串作为单个命令执行
Invoke-Expression ""C:\Users\guest_new\Downloads\rubyinstaller-2.0.0-p648-x64.exe" /silent /tasks='assocfiles,modpath'"
注意:您可能需要使用引号,以便在
调用表达式上获得正确的顺序
使用-ArgumentList
参数传递参数
$ruby_inst_process = Start-Process -FilePath "C:\Users\guest_new\Downloads\rubyinstaller-2.0.0-p648-x64.exe" -ArgumentList "/silent /tasks='assocfiles,modpath'" -PassThru -Wait
为了让它更容易理解,可以使用变量来分解行
$exe = "C:\Users\guest_new\Downloads\rubyinstaller-2.0.0-p648-x64.exe"
$args = "/silent /tasks='assocfiles,modpath'"
$ruby_inst_process = Start-Process -FilePath $exe -ArgumentList $args -PassThru -Wait
这一行中还有一个错误:if($ruby\u inst\u process-ne 0)
从Start process-PassThru返回的是process
对象,而不是简单的数字或字符串。您可能需要的是此对象上的ExitCode
属性
if ($ruby_inst_process.ExitCode -ne 0) {
"Ruby 2.0.0 installation failed"
exit 0
}
使用-ArgumentList
参数传递参数$ruby\u inst\u process=Start process“C:\Users\guest\u new\Downloads\rubyinstaller-2.0.0-p648-x64.exe”-ArgumentList“/silent/tasks='assocfiles,modpath'”-PassThru-Wait
@RyanBemrose:这应该是一个答案,而不是评论。它起作用了,谢谢。但在检查退出值时,字符串类型出现另一个错误<代码>安装Ruby 2.0.0无法将值转换为System.String类型。在D:\VBox Shared\mcollective\u install.ps1:5 char:2+写入主机“$ruby\u inst\u process”+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:无效参数:(:)[],RuntimeException+FullyQualifiedErrorId:InvalidCastFromAnyTypeToString Ruby 2.0.0安装失败
Two nits:1)Powershell的行为类似于JavaScript的分号插入,因此始终将大括号放在与if
相同的一行,而不是下一行。2) 在PS中打印变量不需要将字符串与+
连接起来。只需将其插入字符串中,就像“$setname的第三个值是$($values[2])