powershell - 导入模块 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
我检查了拼写,这似乎不是问题。我犯了一个明显的错误吗?
解决方案
这看起来像你在反引号字符后有一个空格。这就是使用它来换行的危险。如果您喜欢更好的组织方式,一个更好的方法是定义一个哈希表,然后像这样将它放到 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
推荐阅读
- agora.io - WebRTC 与 Agora.io:SwitchCamera?
- java - Firebase 使用 addTextChangedListener 检查用户名是否存在
- php - 如何完全删除订单页面上的订单标题?
- javascript - 在 html5 filereader for javascript 中使用位置 url 而不是输入文件来选择文件
- reactjs - 用户拒绝 React.js 和文件夹访问
- typescript - 使用电子邮件和密码进行 Angular6 Firebase 身份验证
- r - 在 R Shiny 中运行系统命令
- parquet - 是否可以用 pyarrow 编写镶木地板统计信息?
- android - 如何从 string.xml 文件中获取这种类型的数组到 android 活动
- botframework - 无法构建和部署默认 Azure Web Bot