是否有可能创build一个通用的configuration文件来安装使用PowerShell的Windowsfunction?

我目前正在尝试自动生成运行Windows Server 2012 R2的虚拟机。 目前的挑战是自动添加angular色和function。 在angular色和function向导中,有一个选项可以导出可以在PowerShell中运行的XMLconfiguration文件。

但是,通过查看XML文件,我可以看到它是特定于它所运行的服务器 – 它包含诸如“ComputerName”的字段。

如果我想运行一个在许多虚拟机上安装angular色和function的脚本,该怎么办? 我需要一个通用的configuration文件,而不是个人计算机的个性化。

有没有人有关于这个问题的投入?

是的,对于Linux和Windows,您都可以创build所需的状态configuration文件,可以:

  • 启用或禁用服务器angular色和function
  • pipe理registry设置
  • pipe理文件和目录
  • 启动,停止和pipe理stream程和服务
  • pipe理组和用户帐户
  • 部署新的软件
  • pipe理环境variables
  • 运行Windows PowerShell脚本
  • 修复已经偏离所需状态的configuration
  • 发现给定节点上的实际configuration状态

这里是一个示例configuration文件,它将启用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" } } } 

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

那么为什么你会使用这个而不是简单的install-windowsfeature cmdlet呢? 使用DSC代替脚本的实际function是可以定义一个位置,在该位置可以存储要从(相对于目标机器) 推入或拉出的configuration,请参阅“ 推送和拉取configuration模式” 。 configuration不关心,如果机器是物理或虚拟的,但我相信至less要到2012年才能启动服务器来拉DSC。

你可以在PowerShell中做到这一切

 Get-WindowsFeature | ? { $_.Installed } | Export-Clixml .\installed.xml 

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

 Import-Clixml <path to xml>\installed.xml | Install-WindowsFeature 
 Import-Module servermanager Install-WindowsFeature Feature, Feature, Feature, etc 

以上将安装function列表。 您可以对它们进行硬编码,或者将它们保存在一个文件中,每行一个,然后使用它来安装它们:

 Import-Module servermanager $features = get-content C:\Features.txt Install-WindowsFeature $features