UserHandling/New-NavContainerWindowsUser.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
<#
 .Synopsis
  Creates a new Winodws User in a NAV/BC Container
 .Description
  Creates a new Windows user in a NAV/BC Container.
 .Parameter containerName
  Name of the container in which you want to create a windows user
 .Parameter Credential
  Credentials of the new Winodws user
 .Parameter group
  Name of the local group to add the user to (default is administrators)
 .Example
  New-NavContainerWindowsUser -containerName test -tenantId mytenant -username freddyk -password $password
#>

function New-NavContainerWindowsUser {
    Param (
        [Parameter(Mandatory=$false)]
        [string] $containerName = "navserver",
        [Parameter(Mandatory=$true)]
        [PSCredential] $Credential,
        [parameter(Mandatory=$false)]        
        [string] $group = "administrators"
    )

    PROCESS
    {
        Invoke-ScriptInNavContainer -containerName $containerName -ScriptBlock { param([System.Management.Automation.PSCredential]$Credential, [string]$group)

            Write-Host "Creating Windows user $($Credential.username)"
            New-LocalUser -AccountNeverExpires -FullName $Credential.username -Name $Credential.username -Password $Credential.Password | Out-Null
            Write-Host "Adding Windows user $($Credential.username) to $group"
            Add-LocalGroupMember -Group $group -Member $Credential.username -ErrorAction Ignore
                        
        } `
        -ArgumentList $Credential, $group
    }
}
Set-Alias -Name New-BCContainerWindowsUser -Value New-NavContainerWindowsUser
Export-ModuleMember -Function New-NavContainerWindowsUser -Alias New-BCContainerWindowsUser