我试图理解这个错误的实际含义。到目前为止,对该错误的类似帮助请求的搜索范围包括缺少参数、缺少管道、使用单行或多行以及串联问题,但似乎没有一个答案给出明确的原因。所以我认为问题是代码格式(这使得追踪变得更加困难)。
这是我正在编写的脚本,用于将每个目标 OU 的活动目录用户从现在的任何格式重命名为名字.姓氏格式。
我在 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
}
}
}
powershell 中的 Cmdlet 接受一堆参数。定义这些参数后,您可以为每个参数定义一个位置。
这允许您在不指定参数名称的情况下调用 cmdlet。因此,对于以下 cmdlet,路径属性的位置定义为 0,允许您在调用它时跳过键入 -Path,因此以下两者都将起作用。
Get-Item -Path C:\temp\thing.txt
Get-Item C:\temp\thing.txt
但是,如果您指定的参数多于定义的位置参数,那么您将收到错误。
Get-Item C:\temp\thing.txt "*"
由于此 cmdlet 不知道如何接受第二个位置参数,因此会出现错误。您可以通过告诉它参数的含义来解决此问题。
Get-Item C:\temp\thing.txt -Filter "*"
我假设您在以下代码行中收到错误,因为它似乎是您没有正确指定参数名称的唯一地方,并且可能它将 = 视为参数,而将 $username 视为另一个参数。
Set-ADUser $user -userPrincipalName = $newname
尝试为 $user 指定参数名称并删除=
我在将
Write-Host
cmdlet 转换为 Write-Information
后遇到了这个问题,并且参数周围缺少引号和括号。 cmdlet 签名显然不一样。
Write-Host this is a good idea $here
<=BADWrite-Information this is a good idea $here
这是花费 20-30 分钟挖掘函数堆栈后更正的 cmdlet 签名...
<=GOODWrite-Information ("this is a good idea $here")
我在尝试使用字符
_
更改目录时遇到了这个问题。解决方案是使用字符串来更改目录。
C:\> cd "my_new_dir"
就我而言,这是–
(破折号)和
-
(连字符)之间的区别,如下所示:
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
和:
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
就我而言,其中一个命名参数(cmdlet“Get-AzureStorageKey”的“-StorageAccountName”)中有一个损坏的字符,在我的编辑器(SublimeText)中显示为完全正常,但 Windows Powershell 无法解析它。
为了深入了解它,我将错误消息中的违规行移至另一个 .ps1 文件中,运行该文件,现在错误在“-StorageAccountName”参数的开头显示了一个错误的字符。
删除该字符(在实际编辑器中看起来又正常)并重新输入它可以解决此问题。
我必须使用
powershell.AddCommand("Get-ADPermission");
powershell.AddParameter("Identity", "complete id path with OU in it");
克服这个错误
在编写 Powershell 脚本以使用 AWS Powershell 工具 与 AWS CLI
交互时,我遇到了类似的挑战我运行了命令:
Get-S3Bucket // List AWS S3 buckets
然后我得到了错误:
Get-S3Bucket:找不到接受参数列表的位置参数
这是我修复它的方法:
Get-S3Bucket
不接受 // List AWS S3 buckets
作为属性。
我已将其作为评论放在那里,但 AWS CLI 不接受它作为评论。 AWS CLI 将其视为参数。
我必须这样做:
#List AWS S3 buckets
Get-S3Bucket
仅此而已。
我希望这有帮助
就我而言,我尝试通过放置以下内容来使代码更具可读性:
"LONGTEXTSTRING " +
"LONGTEXTSTRING" +
"LONGTEXTSTRING"
一旦我将其更改为
LONGTEXTSTRING LONGTEXTSTRING LONGTEXTSTRING
然后就成功了