我试图理解这个错误的实际含义。到目前为止,对于此错误的类似帮助请求的搜索范围从缺少参数,缺少管道,使用单行或多行以及串联问题。但是,没有一个答案似乎给出了明确的原因。所以我认为问题在于代码格式(这使得跟踪变得更加困难)。
这是我编写的脚本,用于将活动目录用户从任何格式重命名为名字.姓氏的格式,并针对目标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
}
}
}
Set-ADUser $user -userPrincipalName = $newname
。请删除等号。 - Matt