3

Anyone know how to obtain the current DSC working folder?

On the server, when DSC runs, it generates a folder stucture like the following:

C:\Packages\Plugins\Microsoft.Powershell.DSC\2.18.0.0\DSCWork\DSC.0

The only trouble is when DSC gets a new version, it increments the ".X" folder.. However, when DSC runs, there is no clear way how to resolve the current folder from a script block predictably:

    Script GetDscCurrentPath
    {
        TestScript = {
            return $false
        }
        SetScript ={
            Write-Verbose (Get-Item 'C:\%path_to_dsc%\FileInsideMyDscAsset.txt')
        }
        GetScript = { @{Result = "GetDscCurrentPath"} }
    }

Thoughts? Thanks in advance!!!

webbexpert
  • 704
  • 9
  • 15

1 Answers1

3

The DSC Extension does not expose a variable or function to get the current working directory, but since your script / module is in the directory you should be able to use the PSScriptRoot to get the directory. Here is an example:

Write-Verbose -Message "PSScriptRoot: $PSScriptRoot" -Verbose

# Note PSScriptRoot will change when the extension executes
# your configuration function.  So, you need to save the value 
# when your configuration is loaded
$DscWorkingFolder = $PSScriptRoot

configuration foo {

    Script GetDscCurrentPath
    {
        TestScript = {
            return $false
        }
        SetScript ={
            Write-Verbose $using:DscWorkingFolder
        }
        GetScript = { @{Result = $using:DscWorkingFolder} }
    }
}
TravisEz13
  • 2,263
  • 1
  • 20
  • 28
  • I updated per the edit that you attempted. I disagree with the reviewers comments. – TravisEz13 Jun 05 '16 at 21:27
  • +1 In every others situation I've seen `$scriptPath = Split-Path $MyInvocation.MyCommand.Path` recommended as the way to get the working path. However, inside the DSC engine, `Split-Path` crashes and your method works. – sirdank Feb 15 '17 at 17:59
  • `$PSScriptRoot` was added in a later version of PowerShell, but it's been there along with DSC. It is much easier to use too. – TravisEz13 Feb 16 '17 at 01:13