Powershell:无法找到接受争论"xxx"的位置参数

Dav*_*rst 21 powershell active-directory

我试图了解这个错误实际意味着什么.到目前为止,对此错误的类似帮助请求的搜索范围包括缺少参数,缺少管道,使用单行或多行以及连接问题,但没有一个答案似乎给出明确的理由.所以我认为问题是代码格式(这使得追踪更加困难).

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

我在AD中创建了一个测试OU,其中一些用户将触发错误,而另一些用户则不会.但是,不应该给我错误的用户给我"无法找到接受争论的位置参数"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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

Ale*_*les 15

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

这允许您在不指定参数名称的情况下调用cmdlet.因此,对于以下cmdlet,路径属性的位置为0,允许您在调用时跳过键入-Path,因此以下两者都可以.

Get-Item -Path C:\temp\thing.txt
Get-Item C:\temp\thing.txt
Run Code Online (Sandbox Code Playgroud)

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

Get-Item C:\temp\thing.txt "*"
Run Code Online (Sandbox Code Playgroud)

由于此cmdlet不知道如何接受第二个位置参数,因此会出现错误.您可以通过告诉它参数是什么来解决这个问题.

Get-Item C:\temp\thing.txt -Filter "*"
Run Code Online (Sandbox Code Playgroud)

我假设您在下面的代码行中收到错误,因为它似乎是您没有正确指定参数名称的唯一地方,也许它将=作为参数和$ username作为另一个参数.

Set-ADUser $user -userPrincipalName = $newname
Run Code Online (Sandbox Code Playgroud)

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


Nic*_*ais 13

我在尝试使用字符更改目录时遇到了这个问题_。解决方案是使用字符串来更改目录。

C:\> cd "my_new_dir"
Run Code Online (Sandbox Code Playgroud)


Sli*_*SFT 10

将我的Write-Hostcmdlet 转换为我后,出现了这个问题,Write-Information并且缺少参数的引号和括号。cmdlet签名显然不相同。

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

这是在花了20到30分钟挖掘功能堆栈后纠正的cmdlet签名...

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

  • 这超级有帮助。我一直收到此错误,但不知道它是从哪里抛出的。谢谢! (3认同)

Nae*_*Nae 5

就我而言,这是(En dash) 和(Hyphen)之间的区别,如下所示:-

Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Run Code Online (Sandbox Code Playgroud)

和:

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Run Code Online (Sandbox Code Playgroud)

破折号连字符减号哦,天哪!