Question about the Script Resource

This topic contains 2 replies, has 2 voices, and was last updated by  Michael Felkins 3 years, 1 month ago.

  • Author
    Posts
  • #19493

    Michael Felkins
    Participant

    I find that I am using this resource a lot in my configuration. I am wondering if I understand it completely.

    1. I t appears that I have to re load my variables in the setscript and testscript sections in every script block.
    .
    2. It also appears that I have Import any modules that I am using in each block.
    So 18 time for 18 web sites. I notice that the DSC configuration imports every cmdlet every time it runs, (18 times).

    Am I missing a trick that would allow me to import the module just once? How about the variables?
    Make them Global?

    a sample of my work:

    script Websites18 {
    SetScript = {
    Import-Module WebAdministration
    $site = "Site18"
    $IISPath = "IIS:\Sites\site18"
    $pathweb = "E:\websites"
    New-item $IISPath -physicalpath $pathweb\Site18\main\www\57043 -id 57043 -bindings @{protocol="http";bindinginformation="*:8043:"}, @{protocol="net.tcp";bindinginformation="57043:*"}
    Set-ItemProperty $IISPath -name applicationDefaults.enabledProtocols -value "http,net.tcp"
    Set-ItemProperty $IISPath -name applicationPool -value Site18AppPool
    Set-ItemProperty $IISPath -name logFile.directory -value "$pathlogs\Site18Service"

    New-Item $IISPath\ISCTService -physicalPath $pathweb\Site18\main\www\57043\Site18Service -type Application
    Set-ItemProperty $IISPath\ISCTService -name applicationPool -value Site18AppPool
    }
    GetScript = {return}
    TestScript = {Import-Module WebAdministration
    $site = "Site18"
    If (get-website | where { $_.Name -eq $site } )
    {$True}
    else
    {$false}
    }
    }

    Still working on understanding the GetScript block.

    I am using this rather than the xWebSite resource because it does not support setting the ID which is a requirement here.
    Also, it does not support the net.tcp protocol.

    TIA,

    OldDog

  • #19499

    Dave Wyatt
    Moderator

    One trick you can use with the script resource is to define your variables out in the configuration, and then use the $using scope modifier inside your blocks. As far as the MOF file goes, the end result is the same: DSC will inject those variables into the scripts for you, but it cleans up your code a bit.

    You do probably need to leave the call to Import-Module there, at least in the Set script. If you were calling an actual cmdlet from the WebAdministration module (such as Get-Website in your test script), you could just let module auto-loading take care of it, but since you're just using the IIS drive and the generic cmdlets, the explicit import is likely necessary.

    Try something like this:

    configuration Test
    {
        node SomeNode
        {
            $site = "Site18"
            $IISPath = "IIS:\Sites\site18"
            $pathweb = "E:\websites"
    
            script Websites18 {
                SetScript = {
                    Import-Module WebAdministration
                    New-item $using:IISPath -physicalpath $using:pathweb\$using:site\main\www\57043 -id 57043 -bindings @{protocol="http";bindinginformation="*:8043:"}, @{protocol="net.tcp";bindinginformation="57043:*"}
                    Set-ItemProperty $using:IISPath -name applicationDefaults.enabledProtocols -value "http,net.tcp"
                    Set-ItemProperty $using:IISPath -name applicationPool -value "${using:site}AppPool"
                    Set-ItemProperty $using:IISPath -name logFile.directory -value "$using:pathlogs\${using:site}Service"
    
                    New-Item $using:IISPath\ISCTService -physicalPath $using:pathweb\Site18\main\www\57043\${using:site}Service -type Application
                    Set-ItemProperty $using:IISPath\ISCTService -name applicationPool -value "${using:site}AppPool"
                }
                GetScript = {return}
                TestScript = {Import-Module WebAdministration
                    If (get-website | where { $_.Name -eq $using:site } )
                    {
                        $true
                    }
                    else
                    {
                        $false
                    }
                }
            }
        }
    }
    
  • #19505

    Michael Felkins
    Participant

    Thanks, I had not run across $using before. I'll give it a go.
    Sounds like I don't need to import the module in my test script.
    I'll try that as well.

    OldDog

You must be logged in to reply to this topic.