无法找到接受争论“ xxx”的位置参数。 [英] A positional parameter cannot be found that accepts arguement "xxx"

查看:128
本文介绍了无法找到接受争论“ xxx”的位置参数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解此错误的实际含义。到目前为止,针对该错误的类似帮助请求的搜索范围包括缺少参数,缺少管道,使用单行或多行以及级联问题,但似乎没有答案给出确切的原因。因此,我认为问题是代码格式(这使得查找难度大得多)。

I am trying to understand what this error actually means. So far a search of similar help requests for this error range from missing parameters, missing pipes, use of single or multi lines and also concatenation issues but none of the answers seem to give a definitive reason. So I assume the issue is code format (which makes it a lot harder to track down).

这是我编写的脚本,用于为每个目标重命名活动目录用户

This is my script which I am writing to rename active directory users per target OU from whatever format they are now into a firstname.surname format.

我在AD中创建了一个测试OU,其中一些用户会触发错误,而某些用户不会触发错误。但是,不应该给我错误的用户却给我无法找到接受争论 firstname.surname的位置参数

I have created a test OU in AD with some users which will trigger errors and some that will not. However the users that should not give me an error are giving me the "a positional parameter cannot be found that accepts arguement "firstname.surname"

我看不出有什么问题

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
        }
    }
}


推荐答案

powershell中的cmdlet接受一堆参数。定义这些参数后,您可以为每个参数定义位置。

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

这使您可以在不指定参数名称的情况下调用cmdlet,因此对于以下cmdlet,将path属性定义为0位置,从而允许您在调用它时跳过-Path,因此以下两者将

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.

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

但是,如果指定的参数多于定义的位置参数,则您将得到错误。

However if you specify more arguments than there are positional parameters defined then you will get the error.

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

由于此cmdlet不知道如何接受第二个位置参数,因此您会收到错误。

As this cmdlet does not know how to accept the second positional parameter you get the error. You can fix this by telling it what the parameter is meant to be.

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

我认为您在以下代码行中遇到错误,因为它似乎是唯一的此处您未正确指定参数名称,可能是将=视为参数,将$ username视为另一个参数。

I assume you are getting the error on the following line of code as it seems to be the only place you are not specifying the parameter names correctly, and maybe it is treating the = as a parameter and $username as another parameter.

Set-ADUser $user -userPrincipalName = $newname

尝试为$ user指定参数名称并删除=

Try specifying the parameter name for $user and removing the =

这篇关于无法找到接受争论“ xxx”的位置参数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆