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

查看:214
本文介绍了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,定义的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 =

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

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