No positional parameter was found that takes the argument "xxx"

I am trying to understand what this error actually means. Until now, the search for similar requests for help for this error has varied from missing parameters, missing channels, using single or multi-line lines, and also from problems of concatenation, but none of the answers seem to give a definite reason. Therefore, I assume the problem is in the code format (which makes it much harder to track).

This is my script that I am writing to rename active users of the directory for the target unit from any format in which they are currently in to the format firstname.surname.

I created a test unit in AD with some users who will cause errors, and some not. However, users who should not give me an error tell me that "a positional parameter was not found that takes the argument" firstname.surname "

I don’t see what is wrong with the script, but I hope someone will tell me.

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 } } } 
+18
powershell active-directory
source share
6 answers

Cmdlets in powershell accept a bunch of arguments. When these arguments are defined, you can determine the position for each of them.

This allows you to invoke the cmdlet without specifying a parameter name. Thus, for the next cmdlet, the path attribute defines at position 0, allowing you to skip the -Path input when you invoke it, and as such the following will work.

 Get-Item -Path C:\temp\thing.txt Get-Item C:\temp\thing.txt 

However, if you specify more arguments than the positional parameters are defined, you will receive an error message.

 Get-Item C:\temp\thing.txt "*" 

Since this cmdlet does not know how to accept the second positional parameter, you get an error. You can fix this by telling him what this parameter should mean.

 Get-Item C:\temp\thing.txt -Filter "*" 

I assume that you are getting an error in the next line of code, as this is the only place where you did not specify the parameter names correctly, and perhaps it treats the parameter = as a parameter and $ username as another parameter.

 Set-ADUser $user -userPrincipalName = $newname 

Try specifying the parameter name for $ user and remove =

+13
source share

I had this problem after converting my Write-Host cmdlets to Write-Information , and I lacked the quotes and parens around the parameters. The cmdlet signatures obviously do not match.

Write-Host this is a good idea $here
Write-Information this is a good idea $here <= BAD

This is the cmdlet signature, which is fixed after spending 20-30 minutes copying the function stack ...

Write-Information ("this is a good idea $here") <= GOOD

+7
source share

In my case, the symbol in one of the named parameters ("-StorageAccountName" for cmdlet "Get-AzureStorageKey") was damaged, which in the editor (SublimeText) looked completely normal, but Windows Powershell could not parse it.

To understand this, I moved the violation lines from the error message to another .ps1 file, doing this, and the error now showed an unsuccessful character at the beginning of my "-StorageAccountName" parameter.

Deleting a character (again looking normal in the actual editor) and re-entering it fixes this problem.

0
source share

In my case, it was the difference between – and - as in:

 Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" 

and:

 Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" 
0
source share

I had to use

 powershell.AddCommand("Get-ADPermission"); powershell.AddParameter("Identity", "complete id path with OU in it"); 

to get past this error

0
source share

In my case, I tried to make the code more readable by setting:

 "LONGTEXTSTRING " + "LONGTEXTSTRING" + "LONGTEXTSTRING" 

As soon as I changed it to

 LONGTEXTSTRING LONGTEXTSTRING LONGTEXTSTRING 

Then it worked

0
source share

All Articles