4

The argument to Split-Path below is incorrect and it should have been $delZipExe.
This makes the $delZipCmd hash be set to nothing.
I would expect WorkingDirectory value to be set to nothing in the $delZipCmd hash.

Why does this happens?

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
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
opedroso
  • 103
  • 7
  • 1
    I do not understand your question, what error you are getting, what you expect, nor what the comment on line 10 of your code means. Can you clarify please? – Kory Gill Nov 22 '16 at 17:34
  • On the last line, $delZipCmd is $null. As I said in the question, I would like to understand why that happenned, since I expected that WorkingDirectory would have the value $null, but that all the other entries would be set correctly. – opedroso Dec 04 '16 at 10:15

1 Answers1

4

Since validation of the parameter argument to Split-Path throws a terminating error during construction of the hash table, the entire expression is terminated.

You can isolate the Split-Path statement in a subexpression ($()) to avoid this:

$delZipCmd = @{ 
    FilePath = $delZipExe;
    ArgumentList = $delZipArgs;
    NoNewWindow = $true;
    WorkingDirectory = $(Split-Path $delZipCmd);   # <== notice the $
    Wait = $true;
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Why are you using $delZipCmd for Split-Path, *inside* the declaration for $delZipCmd hash table? This code does not make sense to me. – Kory Gill Nov 22 '16 at 18:07
  • @KoryGill Because OP specifically asks about the behavior *when he does exactly this* - please re-read the question – Mathias R. Jessen Nov 22 '16 at 18:16
  • With strict mode, I would expect the original code to not run because $delZipCmd is not set (which is what you get when you run it from a new/clean powershell window without any prev variables set). so to me, the question asserts something in correct as a premise. moving on... – Kory Gill Nov 22 '16 at 18:24