创建函数以返回网站的 DSC BindingInfo

问题描述 投票:0回答:1

我正在尝试创建一个返回网站绑定信息的函数。这是为了降低我的 dsc 资源文件的复杂性,该文件将有 20/30 个网站具有基于节点名称的类似绑定信息。以下是我目前所拥有的,但我遇到了错误,而且我不知道如何解决它。对此的任何帮助将不胜感激。

这就是我现在所拥有的:

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

这是我得到的错误:

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                                                            
powershell iis dsc
1个回答
1
投票

不支持这种直接指定CimInstance的方式。您只能在配置块下创建 MSFT_xWebBindingInformation 对象。这是示例:

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
© www.soinside.com 2019 - 2024. All rights reserved.