windows – 具有Splatting错误的新ADUser

前端之家收集整理的这篇文章主要介绍了windows – 具有Splatting错误的新ADUser前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在尝试运行以下代码段时遇到错误.
$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

猜你在找的Windows相关文章