如何通过Powershell启用Windows功能

时间:2013-01-09 13:33:36

标签: powershell

我需要使用Powershell启用两个Windows功能。但我不知道他们的名字或如何找到他们。

Windows Features

到目前为止,我已设法安装IIS并使用找到的here脚本停止默认应用程序池。

function InstallFeature($name) {
    cmd /c "ocsetup $name /passive"
}
InstallFeature IIS-WebServerRole
    InstallFeature IIS-WebServer
        InstallFeature IIS-CommonHttpFeatures
            InstallFeature IIS-DefaultDocument
            InstallFeature IIS-DirectoryBrowsing
            InstallFeature IIS-HttpErrors
            InstallFeature IIS-HttpRedirect
            InstallFeature IIS-StaticContent
        InstallFeature IIS-HealthAndDiagnostics
            InstallFeature IIS-CustomLogging
            InstallFeature IIS-HttpLogging
            InstallFeature IIS-HttpTracing
            InstallFeature IIS-LoggingLibraries
        InstallFeature IIS-Security
            InstallFeature IIS-RequestFiltering
            InstallFeature IIS-WindowsAuthentication
        InstallFeature IIS-ApplicationDevelopment
            InstallFeature IIS-NetFxExtensibility
            InstallFeature IIS-ISAPIExtensions
            InstallFeature IIS-ISAPIFilter
            InstallFeature IIS-ASPNET
    InstallFeature IIS-WebServerManagementTools 
        InstallFeature IIS-ManagementConsole 
        InstallFeature IIS-ManagementScriptingTools

import-module WebAdministration

Stop-WebAppPool DefaultAppPool

解决方案

要搜索:

Get-WindowsFeature *ASP*
Get-WindowsFeature *activation*

安装:

Add-WindowsFeature NET-Framework-45-ASPNET
Add-WindowsFeature NET-HTTP-Activation

4 个答案:

答案 0 :(得分:30)

对于较新的Windows客户端操作系统(Windows 10 / 8.1 / 8),您无法使用 Install-WindowsFeature ,因为这仅用于管理服务器上的功能。尝试使用它会导致错误消息:

  

Get-WindowsFeature:指定cmdlet的目标不能是基于Windows客户端的操作系统。

您可以使用DISM Powershell模块查找和安装可选功能:

gcm -module DISM #List available commands
Get-WindowsOptionalFeature -online | ft #List all features and status
Enable-WindowsOptionalFeature -online -FeatureName NetFx3 -Source e:\Sources\sxs

在最后一个命令中-Source e:\Sources\sxs仅在功能需要引用源文件的安装介质时才需要(通常要修复错误:0x800f081f无法找到源文件)。 .NET Framework 3.5版似乎是唯一一个要求客户端操作系统的版本,但是服务器操作系统上还有许多其他需要引用安装介质的源代码。

答案 1 :(得分:21)

如果你在Windows 2008R2中有一个模块:

Import-Module servermanager

此模块导出3个cmdlet:Get-WindowsFeatureAdd-WindowsFeatureremove-WindowsFeature

所以你可以做些像 get-windowsfeature *frame*列出.net功能并通过命令安装它  Add-WindowsFeature Net-Framework

答案 2 :(得分:6)

尝试此操作以获取名称(简短)和显示名称(长描述):
get-windowsfeature | format-table -property name,displayname -AutoSize

用它来安装它们:
Install-WindowsFeature -Name $ Name

其中$ Name是get

中的name属性

PS:Install-WindowsFeature已取代Add-WindowsFeature

答案 3 :(得分:0)

最简单的方法(对我有用)是:

  • 带一台未安装必要角色和功能的计算机
  • 转到添加服务器角色和功能(在服务器管理器中)
  • 使用向导添加必要的角色和功能
  • 在最后一个屏幕上,在按下“安装”按钮之前,您会注意到“导出配置设置”链接

Export configuration settings

保存XML文件。

获得XML文件后,可以使用下面列出的PowerShell脚本调用“Install-WindowsFeature”cmdlet(Server 2012,2016)。

注意1:PowerShell脚本旨在用于Windows Server 2008,2012和2016。 对于Windows Server 2008,cmdlet是Add-WindowsFeature。

注意2:PowerShell脚本假定XML文件位于同一文件夹中,并且其名称列在脚本中 - 请参阅第3行和第4行。

Import-Module ServerManager -ErrorAction Stop

$win2k8xml = '.\features-w2k8.xml'
$win2k12xml = '.\RolesAndFeatures.xml'


$minOSVersion = [version] "6.1.7600" # Windows Server 2008 R2 RTM version
$os = Get-WmiObject Win32_OperatingSystem
$currentVersion = [version]$os.Version

if($currentVersion -lt $minOSVersion)
{
    throw "OS version equal or greater than ${minOSVersion} is required to run this script"
}
elseif($currentVersion.ToString().substring(0,3) -eq $minOSVersion.ToString().substring(0,3))
{
If (!(Test-Path $win2k8xml)) {Write-Host "For Windows Server 2008 R2 server make sure that you have Features-W2K8.xml in the current folder" -ForegroundColor Yellow; Pause}
}
elseif($currentVersion -gt $minOSVersion){                                                            

If (!(Test-Path $win2k12xml)) {Write-Host "For Windows Server 2012/2016 make sure that you have RolesAndFeatures.xml in the current folder" -ForegroundColor Yellow; Pause}
}

$OSVersionName = (get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
Write-Host "Your OS version is:$OSVersionName" -ForegroundColor Green

$defaultComputerName = $env:computername
$ComputerName = Read-Host "Computername (Press Enter for current computer - $defaultComputerName)"


if ([string]::IsNullOrEmpty($ComputerName))
{
    $ComputerName = $defaultComputerName;
}

Write-host "Installation will take place on the following computers: $ComputerName"



function Invoke-WindowsFeatureBatchDeployment {
    param (
        [parameter(mandatory)]
        [string] $ComputerName,
        [parameter(mandatory)]
        [string] $ConfigurationFilePath
    )

    # Deploy the features on multiple computers simultaneously.
    $jobs = @()
    if(Test-Connection -ComputerName $ComputerName -Quiet){
        Write-Host "Connection succeeded to: " $ComputerName

        if ($currentVersion.ToString().substring(0,3) -eq $minOSVersion.ToString().substring(0,3)) {
        $jobs += Start-Job -Command {
        #Add-WindowsFeature -ConfigurationFilePath $using:ConfigurationFilePath -ComputerName $using:ComputerName -Restart
        $import = Import-Clixml $using:ConfigurationFilePath
        $import | Add-WindowsFeature
        } 
        } 
        elseif ($currentVersion -gt $minOSVersion) {
        $jobs += Start-Job -Command {
        Install-WindowsFeature -ConfigurationFilePath $using:ConfigurationFilePath -ComputerName $using:ComputerName -Restart
        } 
        }    

    }
    else{
        Write-Host "Configuration failed for: "+ $ComputerName + "! Check computer name and execute again"
    }


    Receive-Job -Job $jobs -Wait | Select-Object Success, RestartNeeded, ExitCode, FeatureResult
}
if ($currentVersion.ToString().substring(0,3) -eq $minOSVersion.ToString().substring(0,3)) {$FilePath = Resolve-Path $win2k8xml}
elseif ($currentVersion -gt $minOSVersion) {$FilePath = Resolve-Path $win2k12xml}

Invoke-WindowsFeatureBatchDeployment $ComputerName $FilePath