Referencing local files in SetScript

Welcome Forums DSC (Desired State Configuration) Referencing local files in SetScript

This topic contains 3 replies, has 3 voices, and was last updated by

1 year ago.

  • Author
  • #100690

    Topics: 1
    Replies: 1
    Points: 0
    Rank: Member

    Hello all!

    What is the best approach to referencing and copying over files in a script resource? Does DSC actually store anything on the host that I can pull from? My development file structure looks like:

    Git repo
      > DSCResources
        > MyResource
          > myConfiguration.psd1
          > myConfiguration.psm1
          > myFolderOfFiles
            > randomFile.txt

    My script resource looks something like:

    $dscWorkingFolder = $PSScriptRoot
    Script FooBar {
            SetScript  = {
                $dscWorkingFolder = $using:dscWorkingFolder
                $pathToFile = "$($dscWorkingFolder)\myFolderOfFiles\randomFile.txt"
                ...use pathToFile variable...

    The error I am getting is:

    PowerShell DSC resource MSFT_ScriptResource  failed to execute Set-TargetResource functionality with error message: Exception calling \"ReadAllText\" with \"1\" argument(s): \"Could not find a part of the path...

    I visit the file path in the error message (without revealing personal info the path starts with "C:\Modules\User\") and the Modules folder on the C drive doesn't even exist! Any ideas?

    Thanks in advance!

  • #100719

    Topics: 6
    Replies: 115
    Points: 101
    Helping Hand
    Rank: Participant

    Hey Marshal,

    2 things to think about when trouble shooting these. When writing your resources pull back one step and treat it like a function so you can test it that way on its own. Also remember which context this is running under, localsystem.

    • #100750

      Topics: 1
      Replies: 1
      Points: 0
      Rank: Member

      Alex, while I appreciate the general advice, it doesn't address the question I asked. To reword/reiterate: When a host checks in and finds a new configuration to run does the configuration itself (the source if you will) get copied or de-complied to the host in any sense? The C:\Modules folder does not exist on the hosts that failed with the error above. Is DSC overriding the $PSScriptRoot variable to a fake path?

  • #100791

    Topics: 0
    Replies: 68
    Points: 0
    Rank: Member

    The expected location for the DSC resource files would be C:\Program Files\WindowsPowerShell\Modules\*ModuleName*\*ModuleVersion*\

The topic ‘Referencing local files in SetScript’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort