2013-01-09 52 views
18

Tôi cần bật hai tính năng Windows bằng Powershell. Nhưng tôi không biết tên của họ hoặc cách tìm chúng.Cách bật tính năng Windows qua Powershell

Windows Features

Cho đến nay tôi đã quản lý để cài đặt IIS và ngăn chặn các ứng dụng mặc định Pool sử dụng một kịch bản tìm thấy 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 

Giải pháp

Để tìm kiếm:

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

Để cài đặt:

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

Trả lời

18

nếu bạn đang ở trong cửa sổ 2008R2 có một mô-đun cho việc này:

01.

Import-Module servermanager

module này xuất khẩu 3 cmdlets: Get-WindowsFeature, Add-WindowsFeatureremove-WindowsFeature

vì vậy bạn có thể làm cho someting như get-windowsfeature *frame* vào danh sách các tính năng .net và cài đặt nó thông qua lệnh như Add-WindowsFeature Net-Framework

+1

Tôi đang ở Windows Server 2012, nhưng tôi vẫn cần tên chính xác của các tính năng hoặc ít nhất là làm thế nào để có được một danh sách. Bạn có biết làm thế nào điều này được thực hiện? –

+0

bạn đã đọc câu trả lời của tôi chưa ?? Sử dụng get-WindowsFeature PS nó sẽ hiển thị tên: Tên hiển thị Tên ------------ ---- [] Hyper-V Hyper-V [] Ứng dụng phân phối ứng dụng- Máy chủ –

+1

Tôi đã đọc câu trả lời của bạn và tôi mới sử dụng Powershell. –

4

Hãy thử điều này để có được tên (viết tắt) và tên hiển thị (mô tả dài):
get-windowsfeature | định dạng bảng -property tên, DisplayName -AutoSize

này sử dụng để cài đặt chúng:
Install-WindowsFeature -Name $ Tên

nơi $ Tên là tài sản tên từ nhận

PS: Cài đặt -WindowsFeature đã thay thế Add-WindowsFeature

18

Đối với hệ điều hành máy khách Windows mới hơn (Windows 10/8.1/8), bạn không thể sử dụng Cài đặt-WindowsFeature vì đó chỉ là để quản lý các tính năng trên máy chủ. Cố gắng sử dụng nó sẽ gây ra một thông báo lỗi:

Get-WindowsFeature : The target of the specified cmdlet cannot be a Windows client-based operating system.

Có một mô-đun DISM Powershell mà bạn có thể sử dụng để tìm và cài đặt tính năng tùy chọn:

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

Trong câu lệnh cuối cùng -Source e:\Sources\sxs chỉ cần thiết nếu tính năng này cần tham khảo phương tiện cài đặt cho các tệp nguồn (thường là để sửa lỗi: 0x800f081f Không thể tìm thấy tệp nguồn). .NET Framework phiên bản 3.5 dường như là cái duy nhất yêu cầu hệ điều hành máy khách, nhưng có nhiều thứ khác trên hệ điều hành máy chủ yêu cầu tham chiếu đến phương tiện cài đặt cho các nguồn.

0

Cách đơn giản nhất (nó làm việc cho tôi) là:

  • mất một máy tính mà vai trò và tính năng cần thiết không được cài đặt
  • hãy vào Add Server Roles and Features (trong Server Manager)
  • sử dụng thuật sĩ để thêm vai trò cần thiết và các tính năng
  • trên màn hình cuối cùng, trước khi nhấn nút "cài đặt", bạn sẽ thấy liên kết "cài đặt cấu hình xuất khẩu"

Export configuration settings

Lưu tệp XML.

Sau khi bạn có tệp XML, bạn có thể sử dụng tập lệnh PowerShell được liệt kê dưới đây để gọi lệnh "Install-WindowsFeature" (Server 2012, 2016).

NOTE1: tập lệnh PowerShell được thiết kế để hoạt động trên Windows Server 2008, 2012 và 2016. Đối với Windows Server 2008, lệnh ghép ngắn là Add-WindowsFeature.

NOTE2: Các kịch bản PowerShell giả định các tập tin XML là trong cùng một thư mục và tên của nó được liệt kê bên trong kịch bản - xin vui lòng xem dòng 3 và 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 
Các vấn đề liên quan