Skip to content

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

Open
stehlih wants to merge 3 commits into
gaelcolas:mainfrom
stehlih:main
Open

prevent early resolving of Powershell variables by using .NET Combine#126
stehlih wants to merge 3 commits into
gaelcolas:mainfrom
stehlih:main

Conversation

@stehlih

@stehlih stehlih commented May 1, 2022

Copy link
Copy Markdown

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

gaelcolas commented May 1, 2022

Copy link
Copy Markdown
Owner

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

@stehlih

stehlih commented May 1, 2022

Copy link
Copy Markdown
Author

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

raandree commented May 1, 2022

Copy link
Copy Markdown
Collaborator

@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
Copy Markdown
Collaborator

@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.

@raandree

raandree commented Mar 5, 2026

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@raandree

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@raandree

raandree commented May 5, 2026

Copy link
Copy Markdown
Collaborator

@Gael, shall we close this PR or merge it? In case we merge it, I would add a changelog entry in a separate PR.

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