gigi
gigi

Reputation: 916

Create Function to return DSC BindingInfo for a website

I'm trying to create a function that returns the bindinginfo for a website. This is intended to reduce the complexity of my dsc resource file that will have 20/30 websites with similar bindinginfo based on the node name. Below is what I have at the moment but I'm getting an error and I don't know exactly how to sort it out. Any help on this would be really appreciated.

This is what I have at the moment:

configuration DscTest
{
    Import-DscResource -ModuleName xWebAdministration;

    Node localhost
    {
        xWebsite TestWebSite
        {
            Ensure = "Present"
            Name = "TestWebSite"
            PhysicalPath = "C:\inetpub\test"
            BindingInfo = (Get-TestBindingInformation $Node)
        }
    }
}

function Get-TestBindingInformation
{
    [OutputType([Microsoft.Management.Infrastructure.CimInstance[]])]
    param(
        [System.Collections.Hashtable] $node
    )

    return @(
        New-CimInstance -ClassName MSFT_xWebBindingInformation -Namespace root/microsoft/Windows/DesiredStateConfiguration -Property @{
            Port                  = 80
            Protocol              = "HTTP"
            HostName              = "test1"
        } -ClientOnly

        New-CimInstance -ClassName MSFT_xWebBindingInformation -Namespace root/microsoft/Windows/DesiredStateConfiguration -Property @{
            Port                  = 80
            Protocol              = "HTTP"
            HostName              = "test2"
        } -ClientOnly
    )
}

DscTest

And this is the error I get:

Write-NodeMOFFile : Invalid MOF definition for node 'localhost': Exception calling "ValidateInstanceText" with "1" argument(s): 
"Convert property 'BindingInfo' value from type 'STRING[]' to type 'INSTANCE[]' failed                                          
 At line:22, char:2                                                                                                             
 Buffer:                                                                                                                        
onName = "DscTest";                                                                                                             
};^                                                                                                                             
insta                                                                                                                           
"                                                                                                                               
At C:\windows\system32\windowspowershell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:2193 char:21 
+ ...             Write-NodeMOFFile $Name $mofNode $Script:NodeInstanceAlia ...                                                 
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                     
    + CategoryInfo          : InvalidOperation: (:) [Write-Error], InvalidOperationException                                    
    + FullyQualifiedErrorId : InvalidMOFDefinition,Write-NodeMOFFile                                                            

Upvotes: 2

Views: 759

Answers (1)

N.Gupta
N.Gupta

Reputation: 326

This way of directly specifying CimInstance is not supported. You can only create object of MSFT_xWebBindingInformation and that too under configuration block. Here is the sample:

configuration DscTest
{
    Import-DscResource -ModuleName xWebAdministration;

    Node localhost
    {
        $bindingInfo = @()
        Get-TestBindingInformation $Node| foreach {
            $bindingInfo += MSFT_xWebBindingInformation {Port = $_.Port; Protocol = $_.Protocol; HostName = $_.HostName}
        }
        xWebsite TestWebSite
        {
            Ensure = "Present"
            Name = "TestWebSite"
            PhysicalPath = "C:\inetpub\test"
            BindingInfo = $bindingInfo
        }
    }
}

function Get-TestBindingInformation
{
    [OutputType([Microsoft.Management.Infrastructure.CimInstance[]])]
    param(
        [System.Collections.Hashtable] $node
    )

    return @(
        New-CimInstance -ClassName MSFT_xWebBindingInformation -Namespace root/microsoft/Windows/DesiredStateConfiguration -Property @{
            Port                  = 80
            Protocol              = "HTTP"
            HostName              = "test1"
        } -ClientOnly

        New-CimInstance -ClassName MSFT_xWebBindingInformation -Namespace root/microsoft/Windows/DesiredStateConfiguration -Property @{
            Port                  = 80
            Protocol              = "HTTP"
            HostName              = "test2"
        } -ClientOnly
    )
}

DscTest

Upvotes: 1

Related Questions