使用powershell,如何提取当前位于AD中的计算机的OU路径?

问题描述 投票:0回答:1

我已成功隔离可分辨名称,但我想删除通用名称部分,只保留 OU 路径。计算机名称的长度会根据电脑而变化,所以我希望能够只保留第一个逗号之后列出的所有内容(如果有意义的话)。无论 PC 名称是 8 个字符还是 12 个字符,它都应该有效。这是我目前所拥有的。

$testingThis = Get-ADComputer -Identity "ComputerExample" -Properties DistinguishedName | Select-Object -ExpandProperty DistinguishedName

Write-Output $testingThis

这是输出:

CN=example,OU=example,OU=example,OU=example,DC=madeup,DC=madeup,DC=madeup,DC=madeup
powershell scripting active-directory
1个回答
0
投票

您可以将

-replace
运算符 与正则表达式模式
^CN=.+?(?<!\\),
一起使用。另外,
DistinguishedName
是默认属性,不需要
-Properties DistinguishedName

$testingThis = Get-ADComputer -Identity 'ComputerExample'
$testingThis.DistinguishedName -replace '^CN=.+?(?<!\\),'
© www.soinside.com 2019 - 2024. All rights reserved.