首页 > 解决方案 > 导入模块 ActiveDirectory powershell 错误

问题描述

我正在创建一个脚本,询问用户的名字和姓氏,并在 Active Directory 的组中实现它。下面显示了脚本如何启动

Import-Module ActiveDirectory

#Get-Command New-ADUser -Syntax

$firstName = Read-Host -Prompt "Please enter the first name"
$lastName = Read-Host -Prompt "Please enter the last name"

下面的文本显示了放置信息的脚本正文

New-ADUser ` 
    -Name "$firstName $lastName" `
    -GivenName $firstName `
    -Surname $lastName `
    -UserPrincipalName = "$firstName.lastname"
    -EmailAddress "$firstName.$lastName@<domain>"
    -ChangePasswordAtLogon 1 `
    -Enabled 1 `
    -StreetAddress "<info>" `
    -Office "<info>" `
    -State "<info>" `
    -PostalCode "<info>" `
    -Country "<info>" `
    -Path "<path>" 

我收到如下所示的错误,表明找不到对象。错误如下所示

-Name$firstName $lastName : The term '-Name$firstName $lastName' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:9 char:5
+     -Name"$firstName $lastName" `
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-Name$firstName $lastName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-EmailAddress : The term '-EmailAddress' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:13 char:5
+     -EmailAddress "$firstName.$lastName@irtc-tx.com"
+     ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-EmailAddress:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
-ChangePasswordAtLogon : The term '-ChangePasswordAtLogon' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:14 char:5
+     -ChangePasswordAtLogon 1 `
+     ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-ChangePasswordAtLogon:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我检查了拼写,这似乎不是问题。我犯了一个明显的错误吗?

标签: powershell

解决方案


这看起来像你在反引号字符后有一个空格。这就是使用它来换行的危险。如果您喜欢更好的组织方式,一个更好的方法是定义一个哈希表,然后像这样将它放到 cmdlet 中:

$UserParams = @{
    Name = "$firstName $lastName"
    GivenName = $firstName
    Surname = $lastName 
    UserPrincipalName = "$firstName.lastname"
    EmailAddress = "$firstName.$lastName@<domain>"
    ChangePasswordAtLogon = 1 
    Enabled = 1 
    StreetAddress = "<info>" 
    Office = "<info>" 
    State = "<info>" 
    PostalCode = "<info>" 
    Country = "<info>" 
    Path = "<path>" 
}
New-ADUser @UserParams

推荐阅读