Microsoft Virtualization Discussions

how to error check existing user

stephen2
2,383 Views

Trying to write a script to add local groups, roles, users to multiple filers.

When invoking new-nauser, the script terminates if the user already exists. Is there a way to check if the user exists prior to executing new-nauser?

What would the syntax be for using Get-NaUser to check if the user already exists without the script bombing out?

1 REPLY 1

timothyn
2,383 Views

That's pretty straightforward:

if (!(Get-NaUser eric -ErrorAction SilentlyContinue))

{

    New-NaUser eric -group users -password toolkit1!

}

else

{

    Write-Warning "User eric already exists"

}

or if you are confident your command won't fail for other reasons (e.g. bad group/password):

New-NaUser eric -group users -password toolkit1! -ErrorAction SilentlyContinue

Public