Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't run script that runs another script found in its $PSScriptRoot #5

Closed
sba923 opened this issue May 3, 2020 · 2 comments
Closed

Comments

@sba923
Copy link

sba923 commented May 3, 2020

Consider this Parent.ps1:

$script = Join-Path -Path $PSScriptRoot -ChildPath 'Child.ps1'
& $script -arg 'somearg'

that invokes this Child.ps1:

param([string] $arg)
Write-Host("Child.ps1 called with '-arg $arg'")

Running Parent.ps1 (expectedly) yields:

Child.ps1 called with '-arg somearg'

but Measure-Script -Path Parent.ps1 fails:

Measure-Script : Cannot bind argument to parameter 'Path' because it is an empty string.
At line:1 char:1
+ Measure-Script -path ../Parent.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Measure-Script], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Measure-Script
@IISResetMe
Copy link
Owner

Thanks for reporting this @sba923!

This is due to how we "rewrite" the AST in-memory, so by the time we execute it, it's no longer actually a script.

We might be able to inject meaningful $PSScriptRoot/$PSCommandPath vars, but $MyInvocation will still not reflect the original script (just FYI)

@IISResetMe
Copy link
Owner

This is fixed in #8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants