DSC Hashtables, Variables, and ForEach Loops!!!

Welcome Forums DSC (Desired State Configuration) DSC Hashtables, Variables, and ForEach Loops!!!

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

 
Participant
1 week, 4 days ago.

  • Author
    Posts
  • #113549

    Participant
    Points: 1
    Rank: Member

    I'm having issues with the syntax in a ForEach loop. I've looked everywhere and I'm not finding something that will work. Perhaps someone can help by offering some advice or letting me know that what I'm doing will not work withing PowerShell DSC.

     

    I should also point out that this works GREAT if I only have 1 WebApp I want to install (for example, just the JoeTest line in the example below). The issue seems to be whenever there are 2 or more entries under the nested-hastable variable "WebsiteApps."

     

    Here's an example of my hashtable:

    $ServerData = @{
         AllNodes = @(
              @{  NodeName = 'SERVER1'
                  Agency          = 'ABC'
                  System          = 'ABC Prod'
                  BusinessEnv     = 'Production'
                  BusinessPurpose = 'Web'
                  FileServerRole  = 'WEB'
                  ServerType      = 'Virtual Server'
                  OperatingSystem = 'Windows Server 2016, Standard x64 Edition'
                  WebsiteName     = 'Default Web Site'
                  WebsiteApps     = @(
                    @{Name = 'JoeTest'; PhysicalPath = "D:\Web_Apps\JoeTest"; WebAppPool = 'JoeTest'; ManagedRunTimeVersion ='v4.0'; ManagedPipelineMode ='Integrated'; IdentityType = 'ApplicationPoolIdentity'}
                    @{Name = 'JessTest'; PhysicalPath = "D:\Web_Apps\JessTest"; WebAppPool = 'JessTest'; ManagedRunTimeVersion ='v4.0'; ManagedPipelineMode ='Integrated'; IdentityType = 'ApplicationPoolIdentity'}
                    )
             }
    

     

    Here's the portion of the cmdlet I'm trying to get working.  The "ForEach" part works great for the Name and WebPool fields as they are also equal to $_. The problem seems to be in knowing how to pull the other variables out of the embedded hashtable.

    $Current = $ServerData.AllNodes.Where{$_.NodeName -eq $CurrentServerName}
    
    $Current.WebsiteApps.Name.ForEach({ 
    
       xWebAppPool $_ {
    
          Ensure                  = "Present"
          Name                    = $_.Name
          AutoStart               = $true
          IdentityType            = $_.IdentityType
          ManagedPipelineMode     = $_.ManagedPipelineMode
          ManagedRunTimeVersion   = $_.ManagedRunTimeVersion }
    
       xWebApplication $_ { 
          Ensure                  = "Present"
          Website                 = $Current.WebsiteName
          Name                    = $_.Name
          WebAppPool              = $_.WebAppPool
          PhysicalPath            = $_.PhysicalPath
          PreloadEnabled          = $true
          ServiceAutoStartEnabled = $true }
       })
    

     

    After running the script and getting errors, here's what the variables report as:

    $ServerData.AllNodes.Where{$_.NodeName -eq $CurrentServerName}.WebsiteApps
    
    Name                           Value
    ----                           -----
    PhysicalPath                   D:\Web_Apps\JoeTest
    Name                           JoeTest
    IdentityType                   ApplicationPoolIdentity
    WebAppPool                     JoeTest
    ManagedRunTimeVersion          v.4.0
    ManagedPipelineMode            Integrated
    
    PhysicalPath                   D:\Web_Apps\JessTest
    Name                           JessTest
    IdentityType                   ApplicationPoolIdentity
    WebAppPool                     JessTest
    ManagedRunTimeVersion          v.4.0
    ManagedPipelineMode            Integrated
    
    
    
    $ServerData.AllNodes.Where{$_.NodeName -eq $CurrentServerName}.WebsiteApps.Name
    
    JoeTest
    JessTest   
    
    
    
    $ServerData.AllNodes.Where{$_.NodeName -eq $CurrentServerName}.WebsiteApps.PhysicalPath
    
    D:\Web_Apps\JoeTest
    D:\Web_Apps\JessTest  
    

     

    Here are the errors I get upon running the script:

    Mode                LastWriteTime         Length Name                                                                                                                                
    ----                -------------         ------ ----                                                                                                                                
    -a----       10/11/2018  10:04 AM          29042 Localhost.mof                                                                                                                       
    xWebAdministration\xWebAppPool : '' is not a valid value for property 'managedPipelineMode' on class 'xWebAppPool'. Please change the value to one of the following strings: 
    Integrated, Classic.
    At D:\PowerShell\ServerConfig.ps1:1607 char:13
    +             xWebAppPool $_ {                     # Create a new app p ...
    +             ~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Write-Error], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : InvalidValueForProperty,xWebAdministration\xWebAppPool
     
    xWebAdministration\xWebAppPool : '' is not a valid value for property 'managedRuntimeVersion' on class 'xWebAppPool'. Please change the value to one of the following strings: v4.0, 
    v2.0, .
    At D:\PowerShell\ServerConfig.ps1:1607 char:13
    +             xWebAppPool $_ {                     # Create a new app p ...
    +             ~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Write-Error], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : InvalidValueForProperty,xWebAdministration\xWebAppPool
     
    PSDesiredStateConfiguration\Node : Index operation failed; the array index evaluated to null.
    At D:\PowerShell\ServerConfig.ps1:1598 char:5
    +     Node Localhost
    +     ~~~~
        + CategoryInfo          : InvalidOperation: (:) [PSDesiredStateConfiguration\node], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : NullArrayIndex,PSDesiredStateConfiguration\node
     
    PSDesiredStateConfiguration\Node : An exception was raised while processing Node 'Localhost': Index operation failed; the array index evaluated to null.
    At D:\PowerShell\ServerConfig.ps1:1598 char:5
    +   Node
    At D:\PowerShell\ServerConfig.ps1:1598 char:5
    +     Node Localhost
    +     ~~~~
        + CategoryInfo          : InvalidOperation: (:) [Write-Error], InvalidOperationException
        + FullyQualifiedErrorId : FailToProcessNode,PSDesiredStateConfiguration\node
    Update-DependsOn : Index operation failed; the array index evaluated to null.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:2357 char:17
    + ...             Update-DependsOn $Script:NodesInThisConfiguration[$mofNod ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Update-DependsOn], RuntimeException
        + FullyQualifiedErrorId : NullArrayIndex,Update-DependsOn
    
  • #113795

    Participant
    Points: 65
    Published
    Rank: Member

    $Current.WebsiteApps.IdentityType and $Current.WebsiteApps.ManagedPipelineMode will have two values here. You have o do it in below way.

    $Current.WebsiteApps.ForEach({ 
       xWebAppPool $_ {
    
              Ensure                  = "Present"
              Name                    = $_.Name
              AutoStart               = $true
              IdentityType            = $_.IdentityType
              ManagedPipelineMode     = $_.ManagedPipelineMode
              ManagedRunTimeVersion   = $_.ManagedRunTimeVersion
          }
    
       xWebApplication $_ { 
              Ensure                  = "Present"
              Website                 = $Current.DTMBWebsiteName
              Name                    = $_.Name
              WebAppPool              = $_.WebAppPool
              PhysicalPath            = $_.PhysicalPath
              PreloadEnabled          = $true
              ServiceAutoStartEnabled = $true 
          }
      })
    

    PS: DTMBWebsiteName in $Current.DTMBWebsiteName not defined in the config data.

    Its better if you can post the error message that you are getting.

    • #113815

      Participant
      Points: 1
      Rank: Member

      I posted the errors per your request. I also updated the original post as I see I had some typos and wrong info in there. As it runs now – it creates the AppPools but not with the specific changes I requested. It does not create the websites.

  • #113845

    Participant
    Points: 65
    Published
    Rank: Member

    You are creating WebApplication not Websites, your $Current.DTMBWebsiteName is empty, I think you have to use $Current.WebsiteName instead.

    • #113866

      Participant
      Points: 1
      Rank: Member

      I mis-spoke...Yes, I am trying to create apps under the Default Web Site ($Current.WebsiteName). That part functions fine. If you look at the results I posted, you can see what the variable names are and what results they get. When creating the MOFs you can see the error messages stating:

      xWebAdministration\xWebAppPool : " is not a valid value for property 'managedPipelineMode' on class 'xWebAppPool'. Please change the value to one of the following strings:
      Integrated, Classic.

      So it's clear the variable syntax I'm using it's working for some reason. I'm trying to use $_.ManagedPipelineMode and it keeps thinking the data is ". Every time I use a variable for this – it doesn't work. For now, I can just use $_ for the name, pool, app name and everything works fine. But those other variables I need to change which is why they are in the hashtable and I'm trying to pull them within each loop. I'm not sure if this is a limitation of the modules that I'm using or what. To me this seems to be a simple issue which is why I think maybe this module isn't very interchangeable ?!

      I appreciate any information or experience you may have on this!

You must be logged in to reply to this topic.