如何获取当前 Windows DSC 工作文件夹(即“\DSC.0”)?

How do I obtain the current Windows DSC working folder (ie. "\DSC.0")?

有人知道如何获取当前 DSC 工作文件夹吗?

在服务器上,当 DSC 运行时,它会生成如下所示的文件夹结构:

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

唯一的问题是当 DSC 获得新版本时,它会增加“.X”文件夹。但是,当 DSC 运行时,没有明确的方法可以预测地从脚本块中解析当前文件夹:

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

想法?提前致谢!!!

DSC 扩展没有公开变量或函数来获取当前工作目录,但由于您的脚本/模块在目录中,您应该能够使用 PSScriptRoot 来获取目录。这是一个例子:

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} }
    }
}