Powershell splat 销毁变量

标签 powershell hash splat

下面的 Split-Path 参数不正确,它应该是 $delZipExe
这使得 $delZipCmd 哈希值被设置为空。
我希望 WorkingDirectory 值在 $delZipCmd 哈希中设置为空。

为什么会出现这种情况?

Set-StrictMode -Version latest
$delZipExe = '\\servername\ziptools\SP3DDeliverZips.exe'
$delZipDest = "D:\"
$delZipArgs = @( '/execute',
                 '/source', '/RAD ', '/debugpdb', '/wait'
               )
$delZipCmd = @{ FilePath = $delZipExe;
                ArgumentList = $delZipArgs;
                NoNewWindow = $true;
                WorkingDirectory = (Split-Path $delZipCmd);   # <== should be $delZipExe
                Wait = $true;
              }
$delZipCmd | ft

最佳答案

由于 Split-Path 的参数验证在构建哈希表期间会引发终止错误,因此整个表达式将终止。

您可以将 Split-Path 语句隔离在子表达式 ($()) 中以避免出现这种情况:

$delZipCmd = @{ 
    FilePath = $delZipExe;
    ArgumentList = $delZipArgs;
    NoNewWindow = $true;
    WorkingDirectory = $(Split-Path $delZipCmd);   # <== notice the $
    Wait = $true;
}

关于Powershell splat 销毁变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40747997/

相关文章:

powershell - 在 PowerShell 1.0 中使用带有制表符的字符串拆分

powershell - Azure Powershell 选择带有订阅 ID 的 AzureSubscription

string - 拆分文件名 - 奇怪的行为

javascript - 更改 window.location.href 后无法返回

ruby-on-rails - 如何将 yaml 文件解析为 ruby​​ 哈希和/或数组?

ruby:当总和为 21 时,添加数字并打印 true

ruby - 找到数组的第二个元素

sql-server - 是否可以在 Windows 10 上同时安装 SQLPS 和 SqlServer PowerShell 模块?

c++ - 我如何散列 std::unordered_map::const_iterator?

ruby - 在哪里使用 ruby​​ splat 运算符是合法的?