【问题标题】:Create Function to return DSC BindingInfo for a website创建函数以返回网站的 DSC BindingInfo
【发布时间】:2016-04-19 15:28:04
【问题描述】:

我正在尝试创建一个返回网站绑定信息的函数。这是为了降低我的 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                                                            

【问题讨论】:

  • 你能弄清楚如何预处理/计算你的绑定吗?我也有类似情况
  • 似乎不可能。至少不使用当前版本的 ps/dsc

标签: powershell iis dsc


【解决方案1】:

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

配置 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
    }
}

}

函数 Get-TestBindingInformation { [输出类型([Microsoft.Management.Infrastructure.CimInstance[]])] 参数( [System.Collections.Hashtable] $节点 )

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
)

}

Dsc测试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 2011-09-10
    • 2012-10-25
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多