Examples/xUser_CreateUserDetailedConfig.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<#PSScriptInfo
.VERSION 1.0.1
.GUID 3353a4c7-e6b0-4ca9-852d-86d0c4a3e9a5
.AUTHOR Microsoft Corporation
.COMPANYNAME Microsoft Corporation
.COPYRIGHT
.TAGS DSCConfiguration
.LICENSEURI https://github.com/PowerShell/xPSDesiredStateConfiguration/blob/master/LICENSE
.PROJECTURI https://github.com/PowerShell/xPSDesiredStateConfiguration
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES First version.
.PRIVATEDATA 2016-Datacenter,2016-Datacenter-Server-Core
#>


#Requires -module @{ModuleName = 'xPSDesiredStateConfiguration';ModuleVersion = '8.2.0.0'}

<#
    .SYNOPSIS
        Configuration that creates a local user account using the given credentials.
 
    .DESCRIPTION
        Configuration that creates a local user account using the given credentials.
 
    .PARAMETER Credential
        Credentials to use to create the local user account.
 
    .PARAMETER FullName
        Full name of the local user account. Defaults to the name passed in the credentials.
 
    .PARAMETER Description
        Description of the local user account. Defaults to no description.
 
    .PARAMETER PasswordNeverExpires
        To ensure that the password for this account will never expire, set this
        property to $true, and set it to $false if the password will expire.
        Defaults to $false.
 
    .PARAMETER PasswordChangeRequired
        If the user must change the password at the next sign in. Set this
        property to $true if the user must change the password. Defaults to
        $false.
 
    .PARAMETER PasswordChangeNotAllowed
        If the user can change the password. Set this property to $true to ensure
        that the user cannot change the password, and set it to $false to allow
        the user to change the password. Defaults to $false.
 
    .PARAMETER Disabled
        If the account is enabled. Set this property to $true to ensure that
        this account is disabled, and set it to $false to ensure that it is
        enabled. Defaults to $false.
 
    .NOTES
        If you want to create a user with minimal attributes, every parameter,
        except username and password, can be deleted since they are optional.
 
        If the parameters are present then they will be evaluated to be in
        desired state, meaning if for example Description parameter is left as
        the default value, then the desired state is to have no description on
        the local user account.
 
    .EXAMPLE
        xUser_CreateUserDetailedConfig -Credential = (Get-Credential) -FullName = 'MyUser' -Description = 'My local user account' -PasswordNeverExpires = $true -PasswordChangeRequired = $false -PasswordChangeNotAllowed = $false -Disabled = $false
 
        Compiles a configuration that creates a local user account.
#>

Configuration xUser_CreateUserDetailedConfig
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential,

        [Parameter()]
        [System.String]
        $FullName,

        [Parameter()]
        [System.String]
        $Description,

        [Parameter()]
        [System.Boolean]
        $PasswordNeverExpires,

        [Parameter()]
        [System.Boolean]
        $PasswordChangeRequired,

        [Parameter()]
        [System.Boolean]
        $PasswordChangeNotAllowed,

        [Parameter()]
        [System.Boolean]
        $Disabled
    )

    Import-DSCResource -ModuleName 'xPSDesiredStateConfiguration'

    Node localhost
    {
        if (-not $FullName)
        {
            $FullName = $Credential.UserName
        }

        xUser 'CreateUserAccount'
        {
            Ensure                   = 'Present'
            UserName                 = Split-Path -Path $Credential.UserName -Leaf
            Password                 = $Credential
            FullName                 = $FullName
            Description              = $Description
            PasswordNeverExpires     = $PasswordNeverExpires
            PasswordChangeRequired   = $PasswordChangeRequired
            PasswordChangeNotAllowed = $PasswordChangeNotAllowed
            Disabled                 = $Disabled
        }
    }
}