Import NAV Users from CSV with PowerShell

Import NAV Users from CSV with PowerShell

My user import PowerShell script is available on GitHub.

It is very simple, you need to create a CSV file with the following structure:

  • Username
  • Fullname
  • Email

Window Authentication Example

Fill in your CSV file with windows user accounts:

usersincsv

Execute the script as it’s shown below:

. "C:\YourFolder\Import-NavUsersFromCSV.ps1"

$Permissions = ('BASIC', 'TEAM MEMBER')
Import-NavUsersFromCSV -NavVersion 100 `
                       -NavServerInstanceName 'DynamicsNAV100' `
                       -AuthenticationMethod Windows `
                       -ImportFilePath 'C:\YourFolder\NAV Create NAV Users From CSV.csv' `
                       -PermissionSets $Permissions

NAV Username and Password Authentication

Fill in your CSV file with users:

usersincsv2.jpg

. "C:\YourFolder\Import-NavUsersFromCSV.ps1"

$Permissions = ('BASIC', 'TEAM MEMBER')
Import-NavUsersFromCSV -NavVersion 100 `
                       -NavServerInstanceName 'DynamicsNAV100' `
                       -AuthenticationMethod NavUserPassword  `
                       -ImportFilePath "C:\YourFolder\NAV Create NAV Users From CSV.csv" `
                       -PermissionSets $Permissions `
                       -DefaultPassword 'Password001'

The users must change the password when they login for the first time.

Full Script

function Import-NavUsersFromCSV
{

    [CmdletBinding()]
    Param(
        [parameter(Mandatory=$true)]
        [int] $NavVersion,
        [parameter(Mandatory=$true)]
        [string] $NavServerInstanceName,
        [parameter(Mandatory=$true)]
        [string] $ImportFilePath,
        [parameter(Mandatory=$true)]
        [array] $PermissionSets,
        [parameter(Mandatory=$true)]
        [ValidateSet('Windows', 'NavUserPassword')]
        [string] $AuthenticationMethod,
        [parameter(Mandatory=$false)]
        [string] $DefaultPassword
    )

    Process
    {
        Import-Module "C:\Program Files\Microsoft Dynamics NAV\$($NavVersion)\Service\NavAdminTool.ps1"

        $Users = Import-Csv $ImportFilePath -Verbose

        foreach ($User in $Users)
        {

            Write-Host "Creating $($User.UserName)" -ForegroundColor Cyan

            # Create User
            if ($AuthenticationMethod -eq 'Windows')
            {
                New-NAVServerUser -ServerInstance $NavServerInstanceName `
                                  -WindowsAccount $User.UserName `
                                  -FullName $User.FullName `
                                  -ContactEmail $User.Email `
                                  -ErrorAction Inquire `
                                  -Verbose

                foreach ($PermissionSet in $PermissionSets)
                {
                    New-NAVServerUserPermissionSet -ServerInstance $NavServerInstanceName `
                                                   -WindowsAccount $User.UserName `
                                                   -PermissionSetId $PermissionSet `
                                                   -ErrorAction Inquire -Verbose
                }
            }

            if ($AuthenticationMethod -eq 'NavUserPassword')
            {

                $SecureDefaultPassword = ConvertTo-SecureString $DefaultPassword -AsPlainText -Force

                New-NAVServerUser -ServerInstance $NavServerInstanceName `
                                  -UserName $User.UserName `
                                  -FullName $User.FullName `
                                  -ContactEmail $User.Email `
                                  -Password $SecureDefaultPassword `
                                  -ChangePasswordAtNextLogOn  `
                                  -ErrorAction Inquire -Verbose

                foreach ($PermissionSet in $PermissionSets)
                {
                    New-NAVServerUserPermissionSet -ServerInstance $NavServerInstanceName `
                                                   -UserName $User.UserName `
                                                   -PermissionSetId $PermissionSet `
                                                   -ErrorAction Inquire -Verbose
                }
            }
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s