【问题标题】:How to enable a Windows feature via Powershell如何通过 Powershell 启用 Windows 功能
【发布时间】:2013-01-09 13:33:36
【问题描述】:

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

到目前为止,我已经成功地安装了 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

【问题讨论】:

    标签: powershell


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      如果您使用的是 Windows 2008R2,则有一个模块:

      Import-Module servermanager

      此模块导出 3 个 cmdlet:Get-WindowsFeature、Add-WindowsFeature 和 remove-WindowsFeature

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

      【讨论】:

      • 我在 Windows Server 2012 中,但我仍然需要功能的确切名称,或者至少需要如何获取列表。你知道这是怎么做到的吗?
      • 你看我的回答了吗??使用 get-WindowsFeature PS 它将显示名称: Display Name Name ------------ ---- [ ] Hyper-V Hyper-V [ ] Serveur d'applications Application-Server
      【解决方案3】:

      试试这个来获取名称(短)和显示名称(长描述):
      获取窗口功能 | format-table -property name,displayname -AutoSize

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

      其中 $Name 是 get 中的 name 属性

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

      【讨论】:

        【解决方案4】:

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

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

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-04
          相关资源
          最近更新 更多