28

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

Windows功能

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

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

4 回答 4

42

对于较新的 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 版似乎是唯一需要客户端操作系统的版本,但服务器操作系统上还有许多其他版本需要引用安装媒体的源代码。

于 2015-12-30T21:05:41.830 回答
21

如果您在 Windows 2008R2 中,则有一个模块:

Import-Module servermanager

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

因此您可以 get-windowsfeature *frame*列出 .net 功能并通过以下命令安装它 Add-WindowsFeature Net-Framework

于 2013-01-09T13:39:27.610 回答
7

试试这个来获取名称(短)和显示名称(长描述):
get-windowsfeature | 格式表 - 属性名称,显示名称 -AutoSize

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

其中 $Name 是 get 中的 name 属性

PS:Install-WindowsFeature 已经取代了 Add-WindowsFeature

于 2015-07-29T13:46:39.897 回答
0

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

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

导出配置设置

保存 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
于 2018-01-19T16:34:28.900 回答