barconr
barconr

Reputation: 197

Feeding variables into PowerShell commands in New-ADUser cmd

I'm trying to feed some variables into the Powershell cmdlet New-ADuser to automate user creation. This works fine if I hard code the values but when I try to pass it variables I get the following errors:

New-ADUser : A positional parameter cannot be found that accepts argument 'System.Object[]'.
At C:\users\test\Documents\test.ps1:103 char:1
+ New-ADUser -SamAccountName $Target -Name $TargetFullname  -UserPrincipalName [st ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.NewADUser

The command is:

New-ADUser -SamAccountName $Target -Name $TargetFullname  -UserPrincipalName [string]::Concat($Target,"@test.local") 
-AccountPassword (ConvertTo-SecureString -AsPlainText $TargetPassword  -Force) -Enabled $true -PasswordNeverExpires $true -Path 'CN=Users,DC=test,DC=local'

I get similar problems trying this with the office365 cmdlets. Am I missing a basic syntax error here?

Edit

Creating new question for office365 issue: Argument errors with office 365 cmdlet

Upvotes: 1

Views: 2832

Answers (1)

Tim Ferrill
Tim Ferrill

Reputation: 1674

I think it has to do with how you're building your UPN. Try wrapping that section in parentheses:

-UserPrincipalName ([string]::Concat($Target,"@test.local"))

Upvotes: 3

Related Questions