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