共用方式為


建立新的使用者

此範例示範如何使用 User 資源來確保使用者存在。

[確定 ] 設定為 Present ,且 [ UserName ] 設定為 SomeUserName 時,資源會在帳戶不存在時建立 SomeUserName 帳戶。

Password 設定為 PasswordCredential 參數的使用者提供值時,如果資源建立 SomeUserName 帳戶,則會建立密碼設定為 PasswordCredential值的帳戶。 第一次有人以 身分 SomeUserName 登入時,系統會提示他們變更密碼。

如果 SomeUserName 存在,資源就不會設定該帳戶的密碼。

使用 Invoke-DscResource

此腳本示範如何搭配 Invoke-DscResource Cmdlet 使用 User 資源,以確保 SomeUserName 帳戶存在,並視需要使用預設密碼加以建立。

[CmdletBinding()]
param(
    [Parameter(Mandatory)]
    [System.Management.Automation.PSCredential]
    [System.Management.Automation.Credential()]
    $PasswordCredential
)

begin {
    $SharedParameters = @{
        Name       = 'User'
        ModuleName = 'PSDscResource'
        Properties = @{
            Ensure   = 'Present'
            UserName = 'SomeUserName'
            Password = $PasswordCredential
        }
    }

    $NonGetProperties = @(
        'Ensure'
        'Password'
    )
}

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
    }
}

使用組態

此程式碼片段示範如何使用資源區塊來定義 ConfigurationService ,以確保 SomeUserName 帳戶存在,並視需要使用預設密碼加以建立。

Configuration Create {
    param (
        [Parameter(Mandatory)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $PasswordCredential
    )

    Import-DscResource -ModuleName PSDscResources

    Node localhost {
        User ExampleUser {
            Ensure   = 'Present'
            UserName = 'SomeUserName'
            Password = $PasswordCredential
        }
    }
}