我正在尝试使用splatting安装.MSI:
$InstallerArgs @{
"DATABASENAME" = "my_database";
"LOCALIP" = "127.0.0.1";
"USERNAME" = "username1";
"/i" = "C:\Files\Installer.msi";
}
Run Code Online (Sandbox Code Playgroud)
然后我打电话 Start-Process:
Start-Process -FilePath msiexec.exe -ArgumentList @InstallerArgs -Wait
Run Code Online (Sandbox Code Playgroud)
这会返回错误: Missing an argument for parameter 'ArgumentList'. Specify a parameter of type 'System.String[]' and try again.
是不是可以使用splatting Start-Process?
这实际上应该有效:
$InstallerArgs = @{
FilePath = 'msiexec.exe'
ArgumentList = @(
'/i',
'C:\Files\Installer.msi',
'LOCALIP="127.0.0.1"',
'USERNAME="username1"'
)
Wait = $True
}
Start-Process @InstallerArgs
Run Code Online (Sandbox Code Playgroud)
@<varName>不支持Splatting()作为参数值(参数); 相反,splatted本身的哈希表表示一组参数名称 - 值对.
奥拉夫的有用的答案,相反,演示正确使用泼洒,在哈希表中包含的参数名称-值对的Start-Process 整体,与直通-TO- msiexec指定为数组参数ArgumentList哈希表项.
如果你想msiexec在单独的数据结构中只维护传递给参数,那么使用一个数组并传递它,因为它-ArgumentList确实需要一个字符串数组作为它的参数([string[]]):
$InstallerArgs = @(
"DATABASENAME=my_database"
"LOCALIP=127.0.0.1"
"USERNAME=username1"
"/i"
"C:\Files\Installer.msi"
)
# Note: NO splatting
Start-Process -FilePath msiexec.exe -ArgumentList $InstallerArgs -Wait
Run Code Online (Sandbox Code Playgroud)
以上结果执行以下操作:
msiexec.exe DATABASENAME=my_database LOCALIP=127.0.0.1 USERNAME=username1 /i C:\Files\Installer.msi
Run Code Online (Sandbox Code Playgroud)
注意,如果右边的值`"需要双引号 - 例如,因为它们有嵌入的空格,你必须 - 遗憾的是 - 显式地嵌入转义双引号(msiexec ... "C:\Files A\Installer.msi"); 例如,
"`"C:\Files A\Installer.msi`""
Run Code Online (Sandbox Code Playgroud)
这将导致-<key>:<value>通过.
警告重新抨击外部程序:
使用哈希表进行splatting时,PowerShell
:会将哈希表条目转换为单独的参数,这些参数适用于PowerShell命令,但可能不符合外部实用程序所期望的参数语法,例如msiexec.
一个简单的例子:
# Define hashtable with parameter name-value pairs.
$htParams = @{
foo = 'bar none' # parameter -foo with value 'bar none'
}
# Pass the hashtable via splatting (note the use of @ instead of $).
baz.exe @htParams
Run Code Online (Sandbox Code Playgroud)
-<key> <value> 然后会看到以下参数:
-foo:"bar none"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1176 次 |
| 最近记录: |