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

Inconsistent handling of $PWD #249

Closed
BernieWhite opened this issue Jul 30, 2019 · 4 comments · Fixed by #251
Closed

Inconsistent handling of $PWD #249

BernieWhite opened this issue Jul 30, 2019 · 4 comments · Fixed by #251
Assignees
Labels
bug Something isn't working
Milestone

Comments

@BernieWhite
Copy link
Member

Description of the issue

$PWD is not always set as expected during PSRule execution.

To Reproduce

Steps to reproduce the issue:

# Synopsis: Test $PWD automatic variable
Rule 'WithPWD' {
    $PWD.ToString() -eq $TargetObject.PWD.ToString()

}
$testObject = [PSCustomObject]@{
    PWD = $PWD
}

$testObject | Invoke-PSRule -Path $ruleFilePath -Name 'WithPWD';

Expected behaviour

$PWD should use the same path before and during PSRule execution.

Module in use and version:

  • Module: PSRule
  • Version: 0.8.0-B190716

Captured output from $PSVersionTable:

Name                           Value
----                           -----
PSVersion                      6.2.1
PSEdition                      Core
GitCommitId                    6.2.1
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
@BernieWhite BernieWhite added the bug Something isn't working label Jul 30, 2019
@BernieWhite BernieWhite added this to the v0.8.0 milestone Jul 30, 2019
@BernieWhite BernieWhite self-assigned this Jul 30, 2019
@LaurentDardenne
Copy link

What drive location do we need: that of Powershell runspace, that of the module containing the rule, that of the script calling Invoke-PsRule ?
Is the addition of a specific variable to Psrule feasible (needed?) ?

@BernieWhite
Copy link
Member Author

@LaurentDardenne $PWD is built-in to PowerShell, set to the current working directory. The issue is that the value or $PWD differs between the runspace calling Invoke-PSRule and what a rule sees during rule execution.

It doesn't happen in all cases, but I notice the issue when I was trying to load a file from disk within a rule.

Yeah, the fix is to just set the variable inside the rule runspace to match the calling process.

@LaurentDardenne
Copy link

It doesn't happen in all cases, but I notice the issue when I was trying to load a file from disk within a rule.

It is not this case ?

@BernieWhite
Copy link
Member Author

@LaurentDardenne No not in this case. It seems to be related to how the process is run, possibly only affecting PowerShell Core. In this case it was defaulting $PWD to C:\WINDOWS\System32.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants