使用PowerShell管理Windows Server角色和功能

时间:2020-01-09 10:46:39  来源:igfitidea点击:

在Windows Server 2012R2/2015/2019中,我们可以使用图形服务器管理器控制台来安装和删除服务器角色和功能。但是,在大多数情况下,我们可以从PowerShell控制台更快地执行相同操作。在本文中,请仔细考虑如何使用PowerShell管理现代Windows Server版本中的角色和功能。

列出所有通过PowerShell安装的Windows Server角色和功能

Get-WindowsFeature

cmdlet显示所有可用的Windows Server角色和功能的列表。如果不带参数运行它,则将看到有关所有Windows Server组件的信息。

显示组件名称(显示名称),系统名称(名称)和状态(安装状态:"已安装,可用或者已删除")。角色和功能的列表看起来像一棵树,具有嵌套的角色,类似于在服务器管理器GUI中安装角色时看到的角色。要使用PowerShell安装和删除任何角色或者功能,必须知道在"名称"列中列出的它们的系统名称。

提示。如果删除了角色或者功能,则意味着其安装文件已从系统组件列表中删除(以减小WinSxS文件夹的大小),并且如果没有直接的Internet访问或者Windows ServerinstallationISO,则将无法安装该角色(请参阅.Net 3.5安装示例)。

我们可以像这样从网上删除图像中的角色或者组件:

Uninstall-WindowsFeature –Name DHCP –Remove

若要安装已删除的角色,请使用此cmdlet:

Install-WindowsFeature DHCP

(我们将需要直接访问互联网)

或者,我们可以从Windows Server ISO镜像还原组件二进制文件:

Install-WindowsFeature DHCP -Source E:\sources\sxs

我们可以列出已安装的服务器功能:

Get-WindowsFeature | Where-Object {$_. installstate -eq "installed"} | ft Name,Installstate

根据以下屏幕截图,此服务器用作文件服务器(已安装FileAndStorage-Services,Storage-Services角色)。大多数其他组件用于管理或者监视服务器。

如果我们不完全知道角色名称,则可以使用通配符。例如,要检查安装了IIS角色的哪些Web组件,请运行以下命令(语法略有缩短):

Get-WindowsFeature -Name web-* | Where installed

我们可以获取远程Windows Server上已安装组件的列表:

Get-WindowsFeature -ComputerName ny-spool1 | Where installed | ft Name,Installstate

根据已安装的"打印服务"和"打印服务器"角色判断,该服务器用作打印服务器。

我们可以使用Get-WindowsFeature cmdlet在域中查找安装了特定角色的服务器。我们可以使用PowerShell ActiveDirectory模块中的Get-ADComputer cmdlet或者提供的服务器列表来搜索特定Active Directory OU中的服务器(

$servers = ('server1', 'server2')

)。

例如,我们想在指定的AD组织单位中找到所有具有FileAndStorage-Services角色的文件服务器(我将Visual Studio Code用作PoweShell编辑器)。使用以下脚本:

import-module activedirectory  
$Servers=get-adcomputer -properties \* -Filter {Operatingsystem -notlike "\*2008 R2\*" -and enabled -eq "true" -and Operatingsystem -like "\*Windows Server\*"} -SearchBase OU=Servers,OU=UK,DC=theitroad,DC=com |select name  
Foreach ($server in $Servers)  {  
Get-WindowsFeature -name FileAndStorage-Services -ComputerName $server.Name | Where installed | ft $server.name, Name, Installstate  
}

在输出中,我们将获得安装了特定角色的服务器列表。

如何使用PowerShell安装Windows Server角色和功能?

为了在Windows Server上安装角色和功能,使用了 Install-WindowsFeature cmdlet。

要在当前服务器上安装DNS服务器角色和管理工具(包括Powershell DNSServer模块),请运行以下命令:

Install-WindowsFeature DNS -IncludeManagementTools

默认情况下,该cmdlet将安装所有从属角色和功能。要在安装之前显示依赖项列表,请使用选项

WhatIf

Install-WindowsFeature -Name UpdateServices -WhatIf

例如,要安装WSUS角色,我们还必须安装一些IIS组件。

What if: Continue with installation?
What if: Performing installation for "[Windows Server Update Services] Windows Server Update
What if: Performing installation for "[Windows Server Update Services] WID Database".
What if: Performing installation for "[Windows Server Update Services] WSUS Services".
What if: Performing installation for "[Web Server (IIS)] Windows Authentication".
What if: Performing installation for "[Web Server (IIS)] Dynamic Content Compression".
What if: Performing installation for "[Web Server (IIS)] Performance".
What if: Performing installation for "[Web Server (IIS)] Static Content".
What if: Performing installation for "[Windows Internal Database] Windows Internal Database".
What if: The target server Jan need to be restarted after the installation completes.

要安装远程桌面会话主机角色,RDS许可角色和RDS远程管理工具,请使用以下命令:

Install-WindowsFeature -ComputerName lon-rds3 RDS-RD-Server, RDS-Licensing –IncludeAllSubFeature –IncludeManagementTools –Restart

如果我们添加

–Restart

参数,服务器将在需要时自动重新启动。

我们也可以使用以下命令安装组件。例如,要安装SMTP服务器角色:

Get-WindowsFeature -Name SMTP-Server | Install-WindowsFeature

如何在多个远程Windows服务器上部署角色?

部署典型服务器时,还有另一个有趣的选项。我们可以在参考Windows Server上安装所需的功能,并将已安装角色的列表导出到CSV文件:

Get-WindowsFeature | where{$_.Installed -eq $True} | select name | Export-Csv C:\PS\InstalledRoles.csv -NoTypeInformation –Verbose

然后,我们将能够使用此CSV文件在其他典型服务器上安装相同的角色集:

Import-Csv C:\PS\Roles.csv | foreach{ Install-WindowsFeature $_.name }

如果已经安装了角色或者功能,该命令将返回NoChangeNeeded并继续安装下一个角色。

或者要在多个远程服务器上安装相同的角色集,可以使用以下命令:

$servers =('ny-rds1','ny-rds2',ny-rds3,ny-rds4)foreach($servers中的$server){Install-WindowsFeature RDS-RD-Server -ComputerName $server}

如何使用PowerShell在Windows Server上卸载角色或者功能?

要删除Windows Server角色或者功能,请使用Remove-WindowsFeaturecmdlet。

例如,要删除打印服务器角色,请运行以下命令:

Remove-WindowsFeature Print-Server -Restart