新增或更新登錄機碼值
描述
此範例示範如何使用 Registry
資源來確保已設定登錄機碼值。
將 [確定] 設定為 Present
、[ValueName] 設定為 MyValue
,而[機碼] 設定 HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
為 時,如果機碼不存在,資源就會 MyValue
在機碼下 Environment
新增登錄機碼值。
當 ValueType 設定為 Binary
、 ValueData 設定為 0x00
和 Force 設定 $true
為 時,資源會將登錄機碼值設定為 0
,即使其存在值不同也一樣。
使用 Invoke-DscResource
此腳本示範如何搭配 Cmdlet 使用 Registry
資源,以確保 Environment
登錄機碼的值 MyValue
設定為 0
。 Invoke-DscResource
[CmdletBinding()]
param()
begin {
$SharedParameters = @{
Name = 'Registry'
ModuleName = 'PSDscResource'
Properties = @{
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
Ensure = 'Present'
ValueName = 'MyValue'
ValueType = 'Binary'
ValueData = '0x00'
Force = $true
}
}
$NonGetProperties = @(
'Ensure'
'ValueType'
'ValueData'
'Force'
)
}
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
}
}
使用組態
此程式碼片段示範如何使用資源區塊來定義 , Registry
Configuration
以確保 Environment
登錄機碼的值 MyValue
設定為 0
。
Configuration AddOrModifyValue {
Import-DscResource -ModuleName 'PSDscResources'
Node localhost {
Registry ExampleRegistry {
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
Ensure = 'Present'
ValueName = 'MyValue'
ValueType = 'Binary'
ValueData = '0x00'
Force = $true
}
}
}
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應