. Advertisement .
..3..
. Advertisement .
..4..
I am trying to write a programs that returns an invalid answer. I don’t know where the incorrect command is in the “a positional parameter cannot be found that accepts argument”. My detail is:
Import-Module ActiveDirectory
$users = $null
$users = Get-ADUser -SearchBase "ou=Testing,ou=Users,dc=my,dc=domain" -Filter * -Properties *
foreach ($user in $users) {
Write-Host "Processing... $($user)"
$newname = $null
# Check first/last name is set
if (!$user.givenName -or !$user.Surname) {
Write-Host "$($user) does not have first name or last name set. Please correct, skipping user."
continue
} else {
$newname = ("$($user.givenName).$($user.Surname)")
#Check if new username already exists
if (dsquery user -samid $newname) {
Write-Host "$($user) requires altered username with initial."
if (!$user.Initials) {
Write-Host "$($user) does not have any initials set. Please correct, skipping user."
continue
}
$newname = ("$($user.givenName)$($user.Initials).$($user.Surname)")
#Check if altered new username already exists
if (dsquery user -samid $newname) {
Write-Host "$($user) requires manual change. Please correct, skipping user."
continue
}
}
try {
#Change UPN
Set-ADUser $user -userPrincipalName = $newname
#Change DN
Rename-ADObject -identity $user -Newname $newname
} catch {
Write-Host "Error when renaming $($user). Error is: $($_.Exception.Message). User requires manual change. Please correct, skipping user."
continue
}
}
}
And I end up with the warning message:
a positional parameter cannot be found that accepts argument "firstname.surname"
Pls, suggest the best answer to fix it.
The cause:
Cmdlets in powershell accept a bunch of arguments. When these arguments are defined you can define a position for each of them. This allows you to call a cmdlet without specifying the parameter name. So for the following cmdlet the path attribute is define with a position of 0 allowing you to skip typing -Path when invoking it and as such both the following will work.
However if you specify more arguments than there are positional parameters defined, you will get the following error.
Because this cmdlet does not know the way to accept the second positional parameter and you get the error. This can be fixed by telling it what the parameter is meant to be.
You are getting the error on the following line of code because it seems to be the only place you don’t specify the parameter names correctly.
Solution:
Maybe it is treating the = as a parameter and $username as another parameter, so let’s try to specify the parameter name for $user and removing the =.
This problem occurred after I converted my
Write-Host
cmdlets intoWrite-Information
. I had missed quotes and parens around parameters. Evidently, the signatures of cmdlets are not identical.After spending 20-30 minutes looking through the function stack, this cmdlet signature was corrected.