Powershell DSC 配置:安装选择性 Windows 功能

Powershell DSC Configuration: Installing selective Windows Feature

我有 powershell 4,想安装选择性 windows 功能,例如只安装文件服务器 FS-FileServer 和文件服务器资源管理器 FS-Resource-Manager。

[X] File and Storage Services FileAndStorage-Services Installed [X] File Server FS-FileServer Installed [X] File Server Resource Manager FS-Resource-Manager Installed

为此,我的示例代码如下所示

Configuration JSwebDeploy2
{ 
   Import-DscResource -ModuleName PSDesiredStateConfiguration

    node "localhost"
    { 

         WindowsFeature FS-FileServer
         {
            Name = "FS-FileServer"
            Ensure = 'Present'

         }
          WindowsFeature FS-Resource-Manager
         {
            Name = "FS-Resource-Manager"
            Ensure = 'Present'

         }
    }
} 

JSwebDeploy2

这是处理的正确方法还是有办法将所有子功能组合在一起。我遇到了 WindowsFeatureSet,但它仅在 Powershell 5.0 及更高版本中可用。

正如 TheMadTechnician 所说,您通常应该使用版本 5,但您可以通过在循环中生成配置来以某种方式对功能进行分组:

Configuration JSwebDeploy2
{ 
   Import-DscResource -ModuleName PSDesiredStateConfiguration

    node "localhost"
    { 

         @('FS-FileServer','FS-Resource-Manager').ForEach({

             WindowsFeature $_
             {
                Name = $_
                Ensure = 'Present'
             }
         }
    }
} 

JSwebDeploy2

使用您选择的循环结构,并且您可能希望参数化配置而不是对数组进行硬编码,也许使用 -ConfigurationData 等,但概念是相同的:在以下情况下使用循环和变量你 build/generate 你的配置。

这只是旁注,但是版本 5 具有更多用于调试和测试配置的功能,包括 Invoke-DscResource cmdlet;很有用。

但请注意 WindowsFeatureSet 是复合资源,该特定 cmdlet 不支持它。