Installing Roles, Role Services, and Features - Windows PowerShell for Administration: The Personal Trainer (2015)

Windows PowerShell for Administration: The Personal Trainer (2015)

Chapter 2. Installing Roles, Role Services, and Features

When you are working with Windows Server, you have many more configuration options than when you are working with Windows desktops. After you’ve installed a server running Windows Server, you can manage the server configuration by installing and configuring the following components:

· Server roles Server roles are related sets of software components that allow servers to perform specific functions for users and other computers on networks. A server can be dedicated to a single role, such as File Services, or a server can have multiple roles.

· Role services Role services are software components that provide the functionality of server roles. While some server roles have a single function and installing the role installs this function, most server roles have multiple, related role services and you are able to choose which role services to install.

· Features Features are software components that provide additional functionality. Features are installed and removed separately from roles and role services. A computer can have multiple features installed or none, depending on its configuration.

You can manage roles, role services, and features by importing the ServerManager module into the PowerShell console.

Server Manager Essentials

You use the ServerManager module to manage server configuration. Not only can you use this module’s cmdlets to add or remove roles, role services, and features, but you can use this module’s cmdlets to view the configuration details and status for these software components.

The ServerManager module as implemented in PowerShell allows concurrent instances to add or remove components at the same time. This allows you to run multiple Server Manager sessions simultaneously. For example, you canadd roles in one Server Manager session while you are removing features in a different Server Manager session.

REAL WORLD Beginning with PowerShell 3.0, installed modules generally are imported automatically the first time you use a command in a module. Because of this, you generally don’t need to explicitly import modules. However, for modules to be imported automatically, they must be stored in the location specified by $env:PSModulePath and delivered as folders. Modules that consist of a file, such as a .DLL or .PSM1 file, are not imported automatically. Additionally, commands that use providers may not cause the related module to be imported. If so, the provider won’t be available.

You can explicitly import an available module into the current session by using the Import-Module cmdlet. Remember, the $PSModuleAutoloadingPreference variable can be used to enable or disable module importing. If automatic importing of modules is disabled, you must always explicitly import modules.

If the server on which you want to install Windows components doesn’t have all the required binary source files, the server gets the files from Windows Update by default or from a location specified in Group Policy. You also can specify an alternate path for the source files. For network shares, enter the Universal Naming Convention (UNC) path to the share, such as \\CorpServer41\WS12\. For mounted Windows images, enter the WIM path prefixed with WIM: and include the index of the image to use, such as WIM:\\CorpServer41\WS12\install.wim:4. For a locally mounted image, enter the alternate path for the mounted WIM file, such as c:\mountdir\windows\winsxs.

Server Manager Commands

You can manage roles, role services, and features using the following cmdlets:

· Get-WindowsFeature Lists the server’s current state with regard to roles, role services, and features.

Get-WindowsFeature [[-Name] ComponentNames]
[-LogPath LogFile.txt]

· Install-WindowsFeature Installs the named role, role service, or feature. The –IncludeAllSubFeature parameter allows you to install all subordinate role services and features of the named component.

Install-WindowsFeature [-Name] ComponentNames
[-IncludeAllSubFeature] [-IncludeManagementTools]
[-LogPath LogFile.txt] [-Restart] [-Source Path]

AddtlParams=
[-Credential Credential] [-ComputerName ComputerName1, ComputerName2, ...]

· Uninstall-WindowsFeature Removes the named role, role service, or feature.

Uninstall-WindowsFeature [-Name] ComponentNames [-LogPath LogFile.txt] [-Restart] [-IncludeManagementTools]

AddtlParams=
[-Credential Credential] [-ComputerName ComputerName1, ComputerName2, ...]

When applicable, you can

· Use the –ComputerName parameter to specify the remote computer or computers on which to perform the modifications.

· Use the –Source parameter to provide the path to required binary source files.

· Use the –LogPath parameter to log error details to a named log file as an alternative to the default logging used.

· Use the –Restart parameter to restart the computer automatically (if restarting is necessary to complete the operation).

· Use the –WhatIf parameter to display the operations that would be performed if the command were executed.

By default, management tools are not installed or uninstalled. Because of this, use the –IncludeManagementTools with Install-WindowsFeature to install related management tools and with Uninstall-WindowsFeature to uninstall related management tools. The parameter values that you can use with these commands include:

· ComponentNames Identifies the roles, role services, or features to work with by their name (not their display name). The –Name parameter matches actual component names and not display names. With Get-WindowsFeature, you can use wildcard characters. With Install-WindowsFeature and Uninstall-WindowsFeature, you can use pipelining to get the required input names from another command, such as Get-WindowsFeature.

· LogFile.txt Sets the path and name of the text file to which log error details should be written.

Most installable roles, role services, and features have a corresponding component name that identifies the component so that you can manipulate it from the PowerShell prompt. This also is true for supplemental components you’ve made available by downloading and installing their installer packages from the Microsoft Web site.

NOTE If you are working with Windows Server 2008 or Windows Server 2008 R2, you’ll need to use Add-WindowsFeature and Remove-WindowsFeature. For Windows Server 2012 and later, Install-WindowsFeature replaces Add-WindowsFeature and Uninstall-WindowsFeature replaces Remove-WindowsFeature.

Checking Installed Roles, Role Services, and Features

Before modifying a server’s configuration, you check its current configuration and carefully plan how adding or removing a role, role service, or feature will affect a server’s overall performance. Although you typically want to combine complementary roles, doing so increases the workload on the server, so you need to optimize the server hardware accordingly.

At a standard or elevated PowerShell prompt, you can determine the roles, role services, and features that are installed on a server by typingGet-WindowsFeature. Get-WindowsFeature then lists the configuration status of each available role, role service, and feature. Installed roles, role services, and features are highlighted and marked as being installed. In the output, roles and role services are listed before features as shown in the following example and sample output:

get-windowsfeature

Display Name Name
------------ ----
[X] Active Directory Domain Services AD-Domain-Services

[ ] Active Directory Federation Services ADFS-Federation

[ ] Active Directory Lightweight Directory Services ADLDS

[ ] Active Directory Rights Management Services ADRMS

[ ] Active Directory Rights Management Server ADRMS-Server

[ ] Identity Federation Support ADRMS-Identity

. . .

[ ] DHCP Server DHCP

[X] DNS Server DNS

[ ] Fax Server Fax

[X] File and Storage Services FileAndStorage-Services

[X] File and iSCSI Services File-Services

You can use wildcard characters to review the status for a subset of components by name. For example, if you want to check the status of only Active Directory–related components, you can enterGet-WindowsFeature -name ad*orGet-WindowsFeature ad*.

In addition to helping you determine at a glance what components are installed, Get-WindowsFeature can help you document a server’s configuration. To do this, you can save the output in a file as standard text using the redirection symbol (>) as shown in this example:

Get-WindowsFeature > ServerConfig03-21-2015.txt

In this example, you save the output to a text file named ServerConfig03-21-2015.txt.

Installing Roles, Role Services, and Features

The ServerManager module is the PowerShell component you use to install roles, role services, and features. Roles, role services, and features can be dependent on other roles, role services, and features. When you install roles, role services, and features, Server Manager prompts you to install any additional roles, role services, or features that are required.

Adding Roles, Role Services, and Features

At an elevated command prompt, you can install roles, role services, and features by typingInstall-WindowsFeature ComponentName, whereComponentName is the name of the component to install. As related management tools aren’t installed by default, you should use the –IncludeManagementTools option when installing features. You can install subordinate components by including the –IncludeAllSubFeature parameter as shown in the following example and sample output:

Install-WindowsFeature adrms -IncludeAllSubFeature


Success Restart Needed Exit Code Feature Result
------- -------------- --------- -------------
True No Success {ADRMS-Server, ADRMS-Identity}

Here, you install the Active Directory Rights Management Services role service as well as the subordinate Server and Support role services. As PowerShell works, you see a Start Installation progress bar. When the installation is complete, you see the result. The output for a successful installation should look similar to the example.

As you can see, the output specifies whether the installation was successful, whether a restart is needed, an exit code, and a list of the exact changes made. The exit code can be different from the Success status. For example, if the components you specify are already installed, the exit code is NoChangeNeeded, as shown in this example and sample output:

Install-WindowsFeature –name net-framework-features
-includemanagementtools


Success Restart Needed Exit Code Feature Result
------- -------------- --------- -------------
True No NoChangeNeeded {}

Here, you see that Install-WindowsFeature was successful but didn’t actually make any changes. The Feature Result shows no changes as well.

Install-WindowsFeature allows you to specify component names by getting the input from the output of another command. Consider the following example and sample output:

get-windowsfeature bits* | Install-WindowsFeature


Success Restart Needed Exit Code Feature Result

------- -------------- --------- --------------

True No Success {Background Intelligent
Transfer Service (...

Here, Install-WindowsFeature gets the list of components to install from the Get-WindowsFeature cmdlet. You install the Background Intelligent Transfer Service (BITS) role service as well as the subordinate Compact Server and IIS Server Extension role services.

If a restart is required to complete an installation, you can have the Install-WindowsFeature cmdlet restart the computer by including the –Restart parameter. Also note that a pending restart could prevent you from adding or removing other components. If so, you’ll see a related error message as well as the standard output:

Install-WindowsFeature: Please restart the computer before trying to install more roles/features.

Success Restart Needed Exit Code Feature Result
------- -------------- --------- -------------
False Yes FailedRestartRequired { }

Handling Configuration Errors and Other Issues

Some components cannot be installed from the command line. If you try to install one of these components, you’ll see a warning such as:

WARNING: Installation of ‘XYZ’ is not supported on the command line. Skipping . . .

Success Restart Needed Exit Code Feature Result
------- -------------- --------- -------------
True No NoChangeNeeded { }

To test the installation prior to performing the actual operation, you can use the –WhatIf parameter. If you are trying to install components that are already installed, you see a message in the output stating no changes were made, such as

get-windowsfeature ad-d* | Install-WindowsFeature –whatif


What if: Checking if running in ‘WhatIf’ Mode.
Success Restart Needed Exit Code Feature Result
------- -------------- --------- -------------
True No NoChangeNeeded {}

If an error occurs and Install-WindowsFeature is not able to perform the operation specified, you see an error. Generally, error text is shown in red and includes an error flag and error text as shown in the following example output:

The term ‘Install-WindowsFeature’ is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:19
+ Install-WindowsFeature <<<< fs-dfs-replication
+ CategoryInfo : ObjectNotFound: (Install-WindowsFeature:String)
[], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

This error indicates that PowerShell doesn’t recognize the Install-WindowsFeature cmdlet. You see this error if automatic importing is disabled and you forget to import the ServerManager module using the command import-module servermanager.

Another common error you’ll see occurs when elevated permissions are required and you don’t use an elevated administrator PowerShell prompt:

Uninstall-WindowsFeature: Because of security restrictions imposed by User Account Control you must run Uninstall-WindowsFeature in a Windows PowerShell session opened with elevated rights.

To resolve this problem, open an administrator PowerShell prompt.

When you install components, you can write the detailed information to a log by including the –LogPath parameter. This logging information details every operation performed by Install-WindowsFeature. In this example, you write the logging information to C:\logs\install.log:

Install-WindowsFeature BITS -IncludeAllSubFeature -LogPath
c:\logs\install.log

Finally, because PowerShell returns the output as an object, you can pass the output object along the pipeline as necessary. You also can apply alternative formatting to the output, such as list formatting, as shown in this example and sample output:

get-windowsfeature net-* | Install-WindowsFeature | format-list *

Success : True
RestartNeeded : No
FeatureResult : { }
ExitCode : NoChangeNeeded

Uninstalling Roles, Role Services, and Features

The ServerManager module is the PowerShell component you use to uninstall roles, role services, and features. Roles, role services, and features can be dependent on other roles, role services, and features. If you try to remove a required component of an installed role, role service, or feature, Server Manager warns that you cannot remove the component unless you also remove the other role, role service, or feature.

Removing Roles, Role Services, and Features

At an elevated command prompt, you can uninstall roles, role services, and features by typingUninstall-WindowsFeature ComponentName, whereComponentName is the name of the component to uninstall. When you uninstall a top-level component, subordinate components are automatically uninstalled as well. Consider the following example and sample output:

Uninstall-WindowsFeature net-framework-features


Success Restart Needed Exit Code Feature Result

------- -------------- --------- --------------

True No Success {.NET Framework 3.5 (includes .NET 2.0 and...

Here, you uninstall the .NET Framework 3.5 Features as well as the subordinate .NET Framework 3.5. As PowerShell works, you see a Start Removal progress bar. When the removal is complete, you see the result. The output for a successful removal should look similar to the example. If you must restart the server to complete a removal you see a warning message as well as results. The warning states a restart is required to complete the removal. You can have the Uninstall-WindowsFeature cmdlet restart the computer by including the –Restart parameter.

As with installation, you can test the removal prior to performing the actual operation using the –WhatIf parameter. If you are trying to remove components that aren’t installed, you see a note stating no changes were made, such as

Uninstall-WindowsFeature net-framework-features -whatif


What if: Checking if running in ‘WhatIf’ Mode.

Success Restart Needed Exit Code Feature Result
------- -------------- --------- -------------
True No NoChangeNeeded {}

Uninstall-WindowsFeature allows you to specify component names by getting the input from the output of another command. Consider the following example and sample output:

get-windowsfeature web-ftp* | Uninstall-WindowsFeature


Success Restart Needed Exit Code Feature Result
------- -------------- --------- -------------
True No Success {FTP Service, FTP Extensibility}

Here, Uninstall-WindowsFeature gets the list of components to remove from the Get-WindowsFeature cmdlet. You uninstall the FTP Service as well as FTP Extensibility.

Handling Removal Errors and Other Issues

If an error occurs and Uninstall-WindowsFeature is not able to perform the operation specified, you see an error. The errors you see are similar to those for adding components.

In some cases, you might not be able to uninstall a component. Typically, this occurs because a component is required by or depended on by another role, role service, or feature. Consider the following example and sample output:

get-windowsfeature FileAndStorage* | Uninstall-WindowsFeature

Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
False Maybe Failed {}

uninstall-windowsfeature : A prerequisite check for the FileAndStorage-Services feature failed.
1. Storage Services cannot be removed.
At line:1 char:38
+ get-windowsfeature fileandstorage* | uninstall-windowsfeature
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (File and
Storage Services:ServerComponentWrapper) [Uninstall-
WindowsFeature], Exception + FullyQualifiedErrorId :
Alteration_PrerequisiteCheck_Failed,Microsoft.Windows.
ServerManager.Commands.RemoveWindowsFeatureCommand

Here, you try to uninstall the File and Storage role service. However, you are unable to remove this role because it is a required role for servers.

When you uninstall components, you can write the detailed information to a log by including the –LogPath parameter. This logging information details every operation performed by Uninstall-WindowsFeature. In this example, you write the logging information to C:\logs\install.log:

As with the installation process, you can write the detailed information to a log by including the –logPath parameter. Here is an example:

Uninstall-WindowsFeature BITS -IncludeAllSubFeature -LogPath
c:\logs\uninstall.log