我在尝试运行以下代码段时遇到错误.
$Users = Import-Csv -Path $Path foreach ($User in $Users) { $Domain = "Domain.lan" $DefaultPassword = 'Password123' $Parms = @{'Name'= "$($User.Lastname),$($User.Firstname) $($User.middleinitial)"; 'DisplayName' = "$($User.Firstname) $($User.Middleinitial) $($User.LastName)"; 'samAccountName' = "$($User.Firstname[0])$($User.middleinitial)$($User.Lastname)".ToLower(); 'UserPrincipalName' = "$($User.Firstname[0])$($User.middleinitial)$($User.Lastname)@domain.lan".ToLower(); 'GivenName' = $User.Firstname; 'Surname' = $User.LastName; 'Initials' = $User.middleinitial; 'EmailAddress' = $User.EmailAddress; 'AccountPassword' = (ConvertTo-SecureString $DefaultPassword -AsPlainText -Force); 'Enabled' = $true; 'Path' = "OU=Users,DC=domain,DC=lan"; 'ChangePasswordAtlogon' = $True; 'PasswordNeverExpires' = $false } if (Get-ADObject -Filter { samAccountName -eq "$($Parms.samAccountName)"}) { Write-Error "The user $($Parms.SAMAccountName) is already a user in $Domain" } else { $Account = New-ADUser $Parms -PassThru }
我得到的错误是:
New-ADUser : The name provided is not a properly formed account name At C:\Tools\Scripts\Add-BatchUsers.ps1:34 char:9 + New-ADUser $Parms + ~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (CN=System.Colle...DC=Domain,DC=lan:String) [New-ADUser],ADException + FullyQualifiedErrorId : ActiveDirectoryServer:1315,Microsoft.ActiveDirectory.Management.Commands.NewADUser
但是,如果我指定每个参数,则使用相同的$Parms命令按预期工作.谁能解释这种行为?
当你splat你必须在呼叫站点表达变量@Params,而不是$Params
原文链接:https://www.f2er.com/windows/367974.html