移除登錄機碼
描述
此範例示範如何使用 Registry
資源來確保登錄機碼不存在。
當 [確定] 設為 Absent
時,ValueName會設定為空字串,而Key設定 HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\MyNewKey
為 ,則資源會在存在時移除 MyNewKey
登錄機碼。
使用 Invoke-DscResource
此腳本示範如何搭配 Invoke-DscResource
Cmdlet 使用 Registry
資源,以確保 MyNewKey
登錄機碼不存在。
[CmdletBinding()]
param()
begin {
$SharedParameters = @{
Name = 'Registry'
ModuleName = 'PSDscResource'
Properties = @{
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\MyNewKey'
Ensure = 'Absent'
ValueName = ''
}
}
$NonGetProperties = @(
'Ensure'
)
}
process {
$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
Registry
以確保 MyNewKey
登錄機碼不存在。
Configuration RemoveKey {
Import-DscResource -ModuleName 'PSDscResources'
Node localhost {
Registry ExampleRegistry {
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\MyNewKey'
Ensure = 'Absent'
ValueName = ''
}
}
}
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應