As far as I can tell, the only way to run a PowerShell script in the same directory as where the current script resides (which may not be the current directory) is to use $PSScriptRoot (or similar variables in previous versions) in an Invoke-Expression command, like so:
iex "$PSScriptRoot\second.ps1 -SomeArg `$someData"
This will work fine if the path contains no spaces. If there are spaces, though, the fully-qualified path will be split apart and not recognized. That can be worked around by using some quotes and the call operator:
iex "& '$PSScriptRoot\second.ps1' -SomeArg `$someData"
This, however, will have problems if the path contains single quotes. To fix that, we end up with this somewhat unwieldy construct:
$scriptPath = "$PSScriptRoot\second.ps1".Replace("'", "''")
iex "& '$scriptPath' -SomeArg `$someData"
No comments:
Post a Comment