是否可以创建通用配置文件以使用 PowerShell 安装 Windows 功能?

Cam*_*ley 8 automation powershell automated-install xml

我目前正在尝试自动构建运行 Windows Server 2012 R2 的 VM。目前的挑战是自动化添加角色和功能。在角色和功能向导中,有一个选项可以导出可在 PowerShell 中运行的 XML 配置文件。

但是,在查看 XML 文件后,我可以看到它特定于它正在运行的服务器 - 它包含诸如“ComputerName”之类的字段。

如果我想运行一个在许多 VM 上安装角色和功能的脚本怎么办?我需要一个通用的配置文件,而不是针对特定计算机进行个性化。

有没有人对这个问题有意见?

Jim*_*m B 12

是的,对于 Linux 和 Windows,您都可以构建所需的状态配置文件,这些文件可以:

  • 启用或禁用服务器角色和功能
  • 管理注册表设置
  • 管理文件和目录
  • 启动、停止和管理进程和服务
  • 管理组和用户帐户
  • 部署新软件
  • 管理环境变量
  • 运行 Windows PowerShell 脚本
  • 修复偏离所需状态的配置
  • 发现给定节点上的实际配置状态

这是一个示例配置文件,它将启用 IIS,确保网站文件位于正确的文件夹中,如果这些文件中的任何一个未安装或丢失,请根据需要安装或复制它们(注意 $websitefilepath 假定为预定义为网站文件的来源):

    Configuration MyWebConfig
    {
       # A Configuration block can have zero or more Node blocks
       Node "Myservername"
       {
          # Next, specify one or more resource blocks

          # WindowsFeature is one of the built-in resources you can use in a Node block
          # This example ensures the Web Server (IIS) role is installed
          WindowsFeature MyRoleExample
          {
              Ensure = "Present" # To uninstall the role, set Ensure to "Absent"
              Name = "Web-Server"
          }

          # File is a built-in resource you can use to manage files and directories
          # This example ensures files from the source directory are present in the destination directory
          File MyFileExample
          {
             Ensure = "Present"  # You can also set Ensure to "Absent"
             Type = "Directory“ # Default is “File”
             Recurse = $true
             # This is a path that has web files
             SourcePath = $WebsiteFilePath
             # The path where we want to ensure the web files are present
             DestinationPath = "C:\inetpub\wwwroot"
   # This ensures that MyRoleExample completes successfully before this block runs
            DependsOn = "[WindowsFeature]MyRoleExample"
          }
       }
    }
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅Windows PowerShell 所需状态配置概述Windows PowerShell 所需状态配置入门

那么为什么要使用它而不是简单的 install-windowsfeature cmdlet?使用 DSC 而不是脚本的真正强大之处在于我可以定义一个位置,我可以在其中存储要推送到或从中拉出的配置(相对于目标机器),请参阅推送和拉取配置模式。配置并不关心机器是物理的还是虚拟的,但我相信至少需要 2012 年才能让服务器启动以拉取 DSC。


Dri*_*104 7

您可以在 PowerShell 中完成所有操作

Get-WindowsFeature | ? { $_.Installed } | Export-Clixml .\installed.xml
Run Code Online (Sandbox Code Playgroud)

将xml复制到需要去的地方,新服务器可以访问的地方。

Import-Clixml <path to xml>\installed.xml | Install-WindowsFeature
Run Code Online (Sandbox Code Playgroud)