Powershell:找不到接受参数“xxx"的位置参数 [英] Powershell: A positional parameter cannot be found that accepts argument "xxx"

查看:24
本文介绍了Powershell:找不到接受参数“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).

这是我编写的脚本,用于将每个目标 OU 的活动目录用户从他们现在的任何格式重命名为 firstname.surname 格式.

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 who 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 argument "firstname.surname"

我看不出脚本有什么问题,但希望有人能给我一些指点.

I cannot see what is wrong with the script but hopefully, someone can give me some pointers.

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,路径属性定义为位置 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:	emp	hing.txt
Get-Item C:	emp	hing.txt

但是,如果您指定的参数多于定义的位置参数,则会出现错误.

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

Get-Item C:	emp	hing.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:	emp	hing.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 =

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

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