【发布时间】:2015-12-15 13:10:44
【问题描述】:
我正在编写一个 PowerShell 脚本来创建一个新的 Active Directory 组并自动将其放入正确的 OU 中,具体取决于用户所在的部门。脚本从 Active Directory 中的用户那里获取部门,然后需要使用它作为活动目录中 OU 的名称。当我不使用 AD 路径中的变量时,此脚本有效。
[string]$department = Get-ADUser -identity johndoe -properties department | Select department
New-ADGroup -Name NewADGroup -GroupScope Global -path “OU=($department),OU=SubDepartment,OU=MainDepartment,DC=OrgName”
但是,当我尝试使用上述变量 $department 时,出现以下错误:
New-ADGroup : The object name has bad syntax
At C:\Users\JohnDoe\Desktop\CreateNewGroup.ps1:7 char:1
+ New-ADGroup -Name NewADGroup -GroupScope Global -path
"OU=($department ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (CN=NewADGroup,DC=OrgName
:String) [New-ADGroup], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirec
tory.Management.Commands.NewADGroup
如何在 Active Directory 路径中调用该变量?
【问题讨论】:
-
($department)应该是$($department)或只是$department
标签: powershell active-directory