8

I'm currently trying to automate the build of a VM running Windows Server 2012 R2. At the moment the challenge is automating the addition of roles and features. Within the roles and features wizard there is an option to export an XML configuration file which can be run in PowerShell.

However, after looking through the XML file I can see it it is specific to the server it is running on - it contains fields such as "ComputerName".

What if I want to run a script which installs roles and features on many VMs? I need a configuration file which is generalized, not personalized to a specific computer.

Does anyone have an input on this issue?

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24

3 Answers3

12

Yes, for both Linux and Windows you can build desired state config files that can:

  • Enable or disable server roles and features
  • Manage registry settings
  • Manage files and directories
  • Start, stop, and manage processes and services
  • Manage groups and user accounts
  • Deploy new software
  • Manage environment variables
  • Run Windows PowerShell scripts
  • Fix a configuration that has drifted away from the desired state
  • Discover the actual configuration state on a given node

Here is a sample config file that will enable IIS, ensure that the website files are in the right folder, and if any of these things are not installed or missing, to install or copy them as appropriate (note that $websitefilepath is presumed to be predefined as the source for the website files):

    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"
          }
       }
    }

For more details see Windows PowerShell Desired State Configuration Overview and Get Started with Windows PowerShell Desired State Configuration.

So why would you use this instead of simply the install-windowsfeature cmdlet? The real power of using DSC instead of a script is that I can define a location where I can store configurations to be pushed to or pulled from (with respect to the target machine) see Push and Pull Configuration Modes. The configuration doesn't care if the machine is physical or virtual, but I believe it takes at least 2012 to get the server to boot to pull DSC.

Giacomo1968
  • 3,522
  • 25
  • 38
Jim B
  • 23,938
  • 4
  • 35
  • 58
7

You can do it all in PowerShell

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

Copy the xml where is needs to go to, somewhere the new server can access it.

Import-Clixml <path to xml>\installed.xml | Install-WindowsFeature
Drifter104
  • 3,693
  • 2
  • 22
  • 39
0
Import-Module servermanager
Install-WindowsFeature Feature,
    Feature, 
    Feature, 
    etc

The above will install a list of features. You can hard code them or just save them in a file, one per line, and then use this to install them:

Import-Module servermanager
$features = get-content C:\Features.txt
Install-WindowsFeature $features
Deadly-Bagel
  • 161
  • 4
  • For server 2012r2 it is Install-WindowsFeature – Drifter104 Sep 04 '15 at 13:18
  • Ah. We primarily use 2008 so I was not aware. Thanks for pointing that out. – Deadly-Bagel Sep 04 '15 at 13:20
  • When using the GUI to install roles there are a few configurations you need to make using the dropdowns - how are these configured when set through powershell using your method? Are they all set to their defaults? – Cameron McAuley Sep 07 '15 at 10:21
  • Hmm. In that case it looks like you need to use the `-configurationfilepath` parameter and point it to an exported configuration file you mentioned in your OP. A bit odd it contains PC-specific information as you said but all evidence points to that it will install it on the local computer by default and pointing it to one of those files will configure everything as necessary so it's probably not used. It might just be a reference for which computer you got the config from or something. – Deadly-Bagel Sep 07 '15 at 11:14
  • But yes it will use the default options without the file. – Deadly-Bagel Sep 07 '15 at 11:15
  • Thanks. Yea I think you're right in regards to the configuration file. The same thing happens when exporting a configuration file of all the roles and features of a server (using Get-WindowsFeature) – Cameron McAuley Sep 08 '15 at 08:08