安裝或卸載 Windows 功能
描述
此範例示範如何使用 WindowsFeature
資源來確保 Windows 功能如下:
- 已安裝或卸載
- 它是否與其子功能一起安裝
- 是否安裝為特定帳戶
所有資源的值都是使用者提供,而不是硬式編碼。 參數會對應至資源的屬性,變更其行為。
名稱
如果您未指定 Name 參數,資源的 Name 屬性會設定為 Telnet-Client
。 這是資源安裝或卸載的 Windows 功能。
Ensure
如果您未指定 Ensure 參數,則資源的 Ensure 屬性會設定 Present
為 ,而且資源如果未安裝,則會安裝 Windows 功能。
如果您將 [確定 ] 指定為 Absent
,則資源會在安裝時卸載 Windows 功能。
IncludeAllSubFeature
如果您未指定IncludeAllSubFeature參數,則資源的IncludeAllSubFeature屬性會設定為 ,而且如果[確定Present
] 設定 $false
為 ,則資源不會安裝 Windows 功能的子功能。
如果 [確定 ] 設定 Absent
為 ,資源一律會卸載它移除之任何 Windows 功能的子功能。
認證
如果您未指定 Credential 參數,則資源不會設定 Credential 屬性,並在預設帳戶下安裝或卸載 Windows 功能。
LogPath
如果您未指定 LogPath 參數,則資源不會設定 LogPath 屬性,也不會將用於安裝或卸載 Windows 功能的記錄寫入檔案。
使用 Invoke-DscResource
此腳本示範如何搭配 Invoke-DscResource
Cmdlet 使用 WindowsFeature
資源,以確保已安裝或安裝 Windows 功能與使用者提供的設定。
根據預設,它會確保 Telnet-Client
Windows 功能安裝時不會有子功能,也不會將安裝記錄寫入檔案。
[CmdletBinding()]
param(
[System.String]
$Name = 'Telnet-Client',
[ValidateSet('Present', 'Absent')]
[System.String]
$Ensure = 'Present',
[System.Boolean]
$IncludeAllSubFeature = $false,
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,
[ValidateNotNullOrEmpty()]
[System.String]
$LogPath
)
begin {
$SharedParameters = @{
Name = 'WindowsFeature'
ModuleName = 'PSDscResource'
Properties = @{
Name = $Name
Ensure = $Ensure
IncludeAllSubFeature = $IncludeAllSubFeature
}
}
$NonGetProperties = @(
'Ensure'
'IncludeAllSubFeature'
)
}
process {
if ($PSBoundParameters.ContainsKey('Credential')) {
$SharedParameters.Properties.Credential = $Credential
$NonGetProperties += 'Credential'
}
if ($PSBoundParameters.ContainsKey('LogPath')) {
$SharedParameters.Properties.LogPath = $LogPath
$NonGetProperties += 'LogPath'
}
$TestResult = Invoke-DscResource -Method Test @SharedParameters
if ($TestResult.InDesiredState) {
$QueryParameters = $SharedParameters.Clone()
foreach ($Property in $NonGetProperties) {
$QueryParameters.Properties.Remove($Property)
}
Invoke-DscResource -Method Get @QueryParameters
} else {
Invoke-DscResource -Method Set @SharedParameters
}
}
使用組態
此程式碼片段示範如何使用資源區塊來定義 , Configuration
WindowsFeature
以確保已安裝或安裝 Windows 功能與使用者提供的設定。
根據預設,它會確保 Telnet-Client
Windows 功能安裝時不會有子功能,也不會將安裝記錄寫入檔案。
重要
電腦設定有一項限制,可防止 DSC 資源使用 PowerShell 本身或PowerShell 資源庫模組中未包含的任何 PowerShell Cmdlet。 此範例是為了示範用途而提供,但因為 DSC 資源使用 DISM 模組中的 Cmdlet,而該模組隨附為其中一個 Windows 模組,因此無法在電腦設定中運作。
Configuration Example {
param(
[System.String]
$Name = 'Telnet-Client',
[ValidateSet('Present', 'Absent')]
[System.String]
$Ensure = 'Present',
[System.Boolean]
$IncludeAllSubFeature = $false,
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,
[ValidateNotNullOrEmpty()]
[System.String]
$LogPath
)
Import-DscResource -ModuleName 'PSDscResources'
$HasCredential = $null -ne $Credential
$HasLogPath = ![string]::IsNullOrEmpty($LogPath)
Node Localhost {
if ($HasCredential -and $HasLogPath) {
WindowsFeature ExampleWindowsFeature {
Name = $Name
Ensure = $Ensure
IncludeAllSubFeature = $IncludeAllSubFeature
Credential = $Credential
LogPath = $LogPath
}
} elseif ($HasCredential) {
WindowsFeature ExampleWindowsFeature {
Name = $Name
Ensure = $Ensure
IncludeAllSubFeature = $IncludeAllSubFeature
Credential = $Credential
}
} elseif ($HasLogPath) {
WindowsFeature ExampleWindowsFeature {
Name = $Name
Ensure = $Ensure
IncludeAllSubFeature = $IncludeAllSubFeature
LogPath = $LogPath
}
} else {
WindowsFeature ExampleWindowsFeature {
Name = $Name
Ensure = $Ensure
IncludeAllSubFeature = $IncludeAllSubFeature
}
}
}
}
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應