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

prevent early resolving of Powershell variables by using .NET Combine #126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stehlih
Copy link

@stehlih stehlih commented May 1, 2022

prevent early resolving of Powershell variables by using .NET Combine instead of Join-Path

We are using code like the follwoing in the datum.yml:

ResolutionPrecedence:
...

  • LCM$($ro=$Node.Role.Split(''); $ro[0])\LCM$($env:BuildLcmMode)_AddOn
    ...
  • LCM\LCM_$($env:BuildLcmMode)

In this case the original Join-Path produces an runtime error (illegal path) which can be avoid by using the .NET Combine.

@gaelcolas
Copy link
Owner

gaelcolas commented May 1, 2022

The combine usually does not play well with psdrive.
Could you 'convert-path' first?

@stehlih
Copy link
Author

stehlih commented May 1, 2022

In the Powershell documentation I found the note:
Convert-Path only converts existing paths. It cannot be used to convert a location that does not exist yet.

But in the ResolutionPrecedence only the childpath is prepared.

The problem is the use of the following code:
$($env:BuildLcmMode)

which produce the following error: Drive doesn't exist. A drive with name "Roles..." was not found.

Join-Path : Das Laufwerk wurde nicht gefunden. Ein Laufwerk mit dem Namen "Roles$($ro=$Node.Role.Split('_'); $ro[0])$($env" ist nicht vorhanden.
In D:\Repository\WSP-Core\Impl\DscRefConfig\Output\output\RequiredModules\datum\0.40.1\datum.psm1:1995 Zeichen:26

  • ... urrentSearch = Join-Path -Path $searchPrefix -ChildPath $PropertyPath
    • CategoryInfo : ObjectNotFound: (Roles$($ro=$No... $ro[0])$($env:String) [Join-Path], DriveNotFoundException
    • FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

After drop of this part Join-Path works without errors. But I can't found a working replacement to get the value of an environment variable which is required to build the right YAML file name.

@raandree
Copy link
Contributor

raandree commented May 1, 2022

@gaelcolas, remember. When going through the code we were not sure why we replaces Join-Path with [System.IO.Path]::Combine.

Besides in Pester tests, when are we dealing with PSDrives other then the normal file drives?

@raandree
Copy link
Contributor

@gaelcolas, I don't see a reason why we should stick to Join-Path over [System.IO.Path]::Combine in this case.

When trying to do something similar like @stehlih, I get this error:

Join-Path : Cannot find drive. A drive with the name 'Baselines\$($env' does not exist.
At D:\DscWorkshop-1\output\RequiredModules\datum\0.40.1\datum.psm1:1995 char:26
+ ... urrentSearch = Join-Path -Path $searchPrefix -ChildPath $PropertyPath
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Baselines\$($env:String) [Join-Path], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

I have assigned a value to an environment variable:

$env:DscBaseline = 'DscLcm'

and used it in the datum.yml like this:

ResolutionPrecedence:
  - AllNodes\$($Node.Environment)\$($Node.NodeName)
  - Environment\$($Node.Environment)
  - Locations\$($Node.Location)
  - Roles\$($Node.Role)
  - Baselines\Security
  - Baselines\$($Node.Baseline)
  - Baselines\$($env:DscBaseline)

Everything works fine with the proposed change.

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

Successfully merging this pull request may close these issues.

3 participants