【问题标题】:Powershell splat destroy variablePowershell splat 破坏变量
【发布时间】:2017-04-06 11:46:33
【问题描述】:

下面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

【问题讨论】:

  • 我不明白你的问题,你得到了什么错误,你期望什么,也不明白你的代码第 10 行的注释是什么意思。你能澄清一下吗?
  • 在最后一行,$delZipCmd 是 $null。正如我在问题中所说,我想了解为什么会发生这种情况,因为我预计 WorkingDirectory 的值为 $null,但所有其他条目都将正确设置。

标签: powershell hash splat


【解决方案1】:

由于对Split-Path 的参数参数的验证引发了一个终止错误哈希表的构造过程中,整个表达式被终止。

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

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

【讨论】:

  • 你为什么要使用 $delZipCmd 作为 Split-Path,inside 是 $delZipCmd 哈希表的声明?这段代码对我来说没有意义。
  • @KoryGill 因为 OP 特别询问了行为当他这样做时 - 请重新阅读问题
  • 在严格模式下,我希望原始代码不会运行,因为 $delZipCmd 未设置(这是您在没有设置任何 prev 变量的新/干净的 powershell 窗口运行它时得到的) .所以对我来说,这个问题断言了一些正确的前提。继续……
猜你喜欢
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 2013-12-21
  • 2014-01-01
  • 2018-04-25
  • 1970-01-01
  • 2020-01-08
  • 1970-01-01
相关资源
最近更新 更多