DSC:将一个配置应用于各种操作系统

时间:2017-06-07 12:08:49

标签: powershell dsc

这个问题在学习期间发生过,所以我不确定它在现实世界中的可能性有多大,但我仍然有兴趣知道处理这类事情的最佳方法。

我有以下复合配置:

configuration hDefaultServer {
    Param 
    (
        [Parameter(Mandatory)]
        [string]$myFileName,

        [Parameter()]
        [ValidateSet("Present","Absent")]
        [String]$Ensure = "Present"
    )

    WindowsFeature 'InstallGUI'
    {
        Ensure = $Ensure
        Name = 'Server-Gui-Shell'
    }
    File 'Temp'
    {
        Type = 'Directory'
        DestinationPath = "$($env:SystemDrive)\Temp"
        Ensure = $Ensure
    }
    File 'SomeFile'
    {
        Type = 'File'
        DestinationPath = "$($env:SystemDrive)\Temp\$myFileName"
        Contents = 'This file was created by DSC!'
        Ensure = $Ensure
    }    
}

使用以下命令将其编译为.MOF:

$ConfigData = @{
   AllNodes = @(
      @{ NodeName = "*"; PsDscAllowPlainTextPassword = $true },
      @{ NodeName = 'WMF5-1' }
   )
}


    Configuration DemoDefaultServer 
    {
        Param 
        (
            [string]$FileName
        )

        Import-DscResource -Name hDefaultServer

        hDefaultServer Demo2
        {
            myFileName = $FileName
            Ensure = "Present"
        }
    }

    DemoDefaultserver -ConfigurationData $ConfigData -OutputPath C:\Configurations\DemoDefaultServer -filename "SomeOtherFile.txt"

当我在Windows 10上运行它时,它会完成文件和文件夹的内容,但Windows 10客户端上不存在ServerManager的错误:

PowerShell DSC resource MSFT_RoleResource  failed to execute Test-TargetResource functionality with error message: Installing roles and features using PowerShell Desired State Configuration is supported only on Server 
SKU's. It is not supported on Client SKU.

这是公平的。处理这种情况的最佳方法是什么?

如果我这样做:

configuration hDefaultServer {
    [cmdletbinding()]
    Param 
    (
        [Parameter(Mandatory)]
        [string]$myFileName,

        [Parameter()]
        [ValidateSet("Present","Absent")]
        [String]$Ensure = "Present"
    )

    $ProductType = (Get-WmiObject -Class Win32_OperatingSystem).ProductType

    If($ProductType -eq 1)
    {
        Write-Verbose "Client OS ($ProductType)"
    }
    elseif($ProductType -eq 2)
    {
        Write-Verbose "Domain Controller ($ProductType)"

        WindowsFeature 'InstallGUI'
        {
            Ensure = $Ensure
            Name = 'Server-Gui-Shell'
        }
    }
    else
    {
        Write-Verbose "Server OS ($ProductType)"

        WindowsFeature 'InstallGUI'
        {
            Ensure = $Ensure
            Name = 'Server-Gui-Shell'
        }
    }
    File 'Temp'
    {
        Type = 'Directory'
        DestinationPath = "$($env:SystemDrive)\Temp"
        Ensure = $Ensure
    }
    File 'SomeFile'
    {
        Type = 'File'
        DestinationPath = "$($env:SystemDrive)\Temp\$myFileName"
        Contents = 'This file was created by DSC!'
        Ensure = $Ensure
    }    
}

在编译时,相应地调整.MOF文件。即添加或删除InstallGUI部分。

还有DependsOn,但从阅读开始,我相信这是在配置范围内定义应用程序的顺序。我没有弄清楚这是如何工作的模式,但是,例如,我想确保文件夹是在文件之前创建的,我可以做(并且只是为了证明它,我已经改变了顺序,所以文件是列在文件夹之前):

configuration hDefaultServer {
    [cmdletbinding()]
    Param 
    (
        [Parameter(Mandatory)]
        [string]$myFileName,

        [Parameter()]
        [ValidateSet("Present","Absent")]
        [String]$Ensure = "Present"
    )

    $ProductType = (Get-WmiObject -Class Win32_OperatingSystem).ProductType

    If($ProductType -eq 1)
    {
        Write-Verbose "Client OS ($ProductType)"
    }
    elseif($ProductType -eq 2)
    {
        Write-Verbose "Domain Controller ($ProductType)"

        WindowsFeature 'InstallGUI'
        {
            Ensure = $Ensure
            Name = 'Server-Gui-Shell'
        }
    }
    else
    {
        Write-Verbose "Server OS ($ProductType)"

        WindowsFeature 'InstallGUI'
        {
            Ensure = $Ensure
            Name = 'Server-Gui-Shell'
        }
    }
    File 'SomeFile'
    {
        Type = 'File'
        DestinationPath = "$($env:SystemDrive)\Temp\$myFileName"
        Contents = 'This file was created by DSC!'
        Ensure = $Ensure
        DependsOn = "[File]Temp"
    }
    File 'Temp'
    {
        Type = 'Directory'
        DestinationPath = "$($env:SystemDrive)\Temp"
        Ensure = $Ensure
    }   
}

Try / Catch有办法吗?与if语句类似。我确实尝试过,但.MOF包含InstallGUI部分。

我相信使用ConfigurationData可能有更好的方法。类似的东西:

$configData{
    AllNodes = @(
        @{
            NodeName = "WIN-AQEKG7L9SE8"
            Role = "Setup, WindowsFeatures, IE, SqlServer"
         }
    (
}

但我还没有解决这个问题。我发现的所有示例,使用它,似乎使用“角色”,我认为它是ServerManager的一部分,因此在Windows 10上不可用。

TIA。

1 个答案:

答案 0 :(得分:0)

我认为创建两个不同的DSC是个好主意。 这种检查没有任何意义。

DSC用于大规模配置。配置10个Web服务器,配置10个sql服务器等