MicrosoftDocs / PowerShell-Docs-DSC

Documentation for PowerShell Desired State Configuration
https://learn.microsoft.com/powershell/dsc/overview
Creative Commons Attribution 4.0 International
31 stars 36 forks source link

Incorrect script #176

Closed ficzagergo closed 1 year ago

ficzagergo commented 1 year ago

Hi!

The last line from the HelloWorld script is missing. You should append for the last line: HelloWorld


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

sdwheeler commented 1 year ago

This is covered in the Compile the configuration section of the article.

https://github.com/MicrosoftDocs/PowerShell-Docs-DSC/blob/main/dsc/docs-conceptual/dsc-1.1/configurations/write-compile-apply-configuration.md#compile-the-configuration

ficzagergo commented 1 year ago

Please try it from the beginning. It won't work.

There is a script in the Write the configuration section:

Configuration HelloWorld {

    # Import the module that contains the File resource.
    Import-DscResource -ModuleName PsDesiredStateConfiguration

    # The Node statement specifies which targets to compile MOF files for, when
    # this configuration is executed.
    Node 'localhost' {

        # The File resource can ensure the state of files, or copy them from a
        # source to a destination with persistent updates.
        File HelloWorld {
            DestinationPath = "C:\Temp\HelloWorld.txt"
            Ensure = "Present"
            Contents   = "Hello World from DSC!"
        }
    }
}

It is just a simple function without call it. The last line of the script should be: HelloWorld This will call the function.

Without the call of the function it won't be executed and the mof file will not generated as described in the Compile the configuration section.

Please fix the script with this 1 line.

michaeltlombardi commented 1 year ago

@ficzagergo - The article is a how-to guide where each step builds on the last. After that codeblock is declared, the guide says:

Save the file as "HelloWorld.ps1".

In the next section, Compile the configuration, the how-to guide says:

For a DSC configuration to be applied to a node, it must first be compiled into a MOF file. Running the configuration, like a function, will compile one .mof file for every Node defined by the Node block. In order to run the configuration, you need to dot source your HelloWorld.ps1 script into the current scope. For more information, see about_Scripts.

Dot source your HelloWorld.ps1 script by typing in the path where you stored it, after the . (dot, space). You may then, run your configuration by calling it like a function. You could also invoke the configuration function at the bottom of the script so that you don't need to dot-source.

. C:\Scripts\HelloWorld.ps1
HelloWorld

This generates the following output:

Directory: C:\Scripts\HelloWorld

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/13/2017   5:20 PM           2746 localhost.mof

It is intentional that the compiling of the code is separate from loading the Configuration. The point of the section is to clearly show that configurations are executed like functions to generate a compiled configuration MOF.

Note the last line of the second paragraph in that section:

You could also invoke the configuration function at the bottom of the script so that you don't need to dot-source.

The script isn't broken, and the Configuration isn't a function - it's like a function, but it's a special type of command created by the configuration keyword:

Get-Command HelloWorld
CommandType     Name         Version    Source
-----------     ----         -------    ------
Configuration   HelloWorld

The codeblock in the Compile the configuration section that dot-sources the script file also executes the Configuration:

. C:\Scripts\HelloWorld.ps1
HelloWorld
Directory: C:\Scripts\HelloWorld

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/13/2017   5:20 PM           2746 localhost.mof