共用方式為


新增或更新登錄機碼值

描述

此範例示範如何使用 Registry 資源來確保已設定登錄機碼值。

將 [確定] 設定為 Present[ValueName] 設定為 MyValue ,而[機碼] 設定 HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment 為 時,如果機碼不存在,資源就會 MyValue 在機碼下 Environment 新增登錄機碼值。

ValueType 設定為 BinaryValueData 設定為 0x00Force 設定 $true 為 時,資源會將登錄機碼值設定為 0 ,即使其存在值不同也一樣。

使用 Invoke-DscResource

此腳本示範如何搭配 Cmdlet 使用 Registry 資源,以確保 Environment 登錄機碼的值 MyValue 設定為 0Invoke-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
    }
}

使用組態

此程式碼片段示範如何使用資源區塊來定義 , RegistryConfiguration 以確保 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
        }
    }
}