我正试着一个接一个地安装一堆..msi。但是当我运行我的powershell脚本时,msiexec /呢?好像我的论点是错的。我在这里错过了什么?
$Path = Get-ChildItem -Path *my path goes here* -Recurse -Filter *.MSI
foreach ( $Installer in ( Get-ChildItem -Path $Path.DirectoryName -Filter *.MSI ) ) {
Start-Process -Wait -FilePath C:\windows\system32\msiexec.exe -ArgumentList "/i '$Installer.FullName'"
}发布于 2020-08-07 23:07:20
奥拉夫的回答包含很好的指针,但让我从概念上将其归纳如下:
你的尝试有两个不相关的问题:
"...")中,只能使用简单变量引用($Installer);表达式($Installer.FullName)需要$(),子表达式运算符:$($Installer.FullName) -参见这个答案中可扩展字符串(字符串内插)的概述。msiexec的参数通过-ArgumentList作为单个字符串传递,因此只支持嵌入式双引用( "..." ),而不支持'...' (单引号)。因此,使用以下方法(为了简洁起见,-FilePath和-ArgumentList参数按位置传递给Start-Process):
Get-ChildItem $Path.DirectoryName -Recurse -Filter *.MSI | ForEach-Object {
Start-Process -Wait C:\windows\system32\msiexec.exe "/i `"$($_.FullName)`""
}注意:
-ArgumentList参数是数组类型的([string[]]).理想情况下,您可以单独传递参数,作为数组的元素:'/i', $_.FullName,这将不需要考虑在单个字符串中嵌入引用。
不幸的是,-ArgumentList,所以健壮的解决方案是使用一个单参数,其中包括 all e 139参数,以及E 240E 141嵌入式double-quoting,(必要时)E 244,如上面所示。
有关更详细的讨论,请参见这个答案。
发布于 2020-08-07 21:18:21
语法"/i '$Installer.FullName'"不正确。在您的代码中应该是"/i", $Installer.FullName。
在双引号中围绕PowerShell对象将触发字符串扩展。当发生这种情况时,只有变量本身被替换为它的值,然后其余的字符(而不是名称的一部分)将被视为字符串。当您运行以下代码段时,可以看到对象执行了一个ToString(),然后将字符串.FullName添加到其中。
foreach ( $Installer in ( Get-ChildItem -Path $Path.DirectoryName -Filter *.MSI ) ) {
"/i $Installer.FullName"
}如果必须使用双引号,则可以使用子表达式运算符$()。解析器将内部的任何内容作为表达式处理。因此,从技术上讲,越冗长的"/i '$($Installer.FullName)'"就能工作。
完整的代码应该是:
$Path = Get-ChildItem -Path *my path goes here* -Recurse -Filter *.MSI
foreach ( $Installer in ( Get-ChildItem -Path $Path.DirectoryName -Filter *.MSI ) ) {
Start-Process -Wait -FilePath C:\windows\system32\msiexec.exe -ArgumentList "/i", $Installer.FullName
}发布于 2020-08-08 03:27:09
在Powershell 5.1中,使用msi,您还可以:
install-package $installer.fullnamehttps://stackoverflow.com/questions/63308697
复制相似问题