【问题标题】:Extract Domain from DistiniguishedName using PowerShell使用 PowerShell 从 DistiniguishedName 中提取域
【发布时间】:2019-03-01 13:10:31
【问题描述】:

我尝试了以下代码来提取域,并且在定义变量时效果很好

$ADS = 'CN=Lamda,OU=OU_Bloquage,DC=Adminstrateur,DC=6NLG-AD'

但是当我把$ADS改成

时事情并不顺利
$ADS = Get-ADUser -Identity 'Lamda' -Properties DistinguishedName |
       select DistinguishedName`

我想要的结果是:

DC=管理员,DC=6NLG-AD`

下面是我写的代码

$ADS = Get-ADUser -Identity 'Lamda' -Properties DistinguishedName |
       select DistinguishedName
$pattern = '(?i)DC=\w{1,}?\b'
([RegEx]::Matches($ADS, $pattern) | ForEach-Object { $_.Value }) -join ','

【问题讨论】:

  • select DistinguishedName -> select -Expand DistinguishedName
  • 正如 AnsgarWiechers 指出的那样,您的问题是您的数据位于对象的属性中,而不是它自己的对象中。 [grin] 您可以使用Select-Object 扩展该属性,或者在您的代码中寻址该属性。使用$ADS.DistinguishedName 而不是$ADS
  • 我修改了select DistinguishedName -> select -Expand DistinguishedName并对其进行了测试,但出现以下错误:Move-ADObject:Operation could not be affected because object parent was not created or was deleted
  • 你没有向我们展示你做Move-ADObject的部分。问题只是如何拆分专有名称..

标签: powershell ou distinguishedname


【解决方案1】:

正如 Ansgar Wiechers 和 Lee_Daily 已经指出的那样,您真正想要的只是用户的 DistinghuishedName 属性。 Get-ADUser cmdlet 默认返回此属性,因此只需将其作为字符串获取:

$dn = Get-ADUser -Identity 'Lamda' | Select-Object -ExpandProperty DistinguishedName

$dn 现在将是一个字符串 CN=Lamda,OU=OU_Bloquage,DC=Adminstrateur,DC=6NLG-AD

要从该字符串中仅获取以DC= 开头的部分,有很多选项。
例如:

$DN.Substring($dn.IndexOf("DC="))

另一种方式可能是:

'DC=' + ($DN -split 'DC=', 2)[-1]

甚至像这样的东西也会这样做:

($DN -split '(?<![\\]),' | Where-Object { $_ -match '^DC=' }) -join ','

.. 可能还有更多方法可以得到想要的结果

【讨论】:

    猜你喜欢
    • 2022-10-21
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多