在PowerShell中运行以下我得到下面的结果。
Running the following in Powershell I get the result below.
Get-ADUser -Filter * -Properties DistinguishedName | select DistinguishedName | Select-Object -Property @{label='Result';expression={$_.DistinguishedName}}
结果
CN=User Full Name 1,OU=AMS,OU=DS,DC=domain,DC=local
CN=User Full Name 2,OU=TECHM,OU=DS,DC=domain,DC=local
CN=User Full Name 3,OU=Agencies,OU=HK,OU=Developers,DC=domain,DC=local
CN=User Full Name 4,OU=Agencies,OU=CH,OU=Developers,DC=domain,DC=local
CN=User Full Name 5,OU=Agencies,OU=US,OU=Developers,DC=domain,DC=local
CN=User Full Name 6,OU=Market,OU=PP,OU=Developers,DC=domain,DC=local
CN=User Full Name 7,OU=Market,OU=HK,OU=Developers,DC=domain,DC=local
这是我在Active Directory中OU
DS
AMS
TECHM
Developers
CH
Agencies
Market
HK
Agencies
Market
我需要一种方法来适应这个查询(做拆分或其他),所以我能够获得第三个项目(逗号分隔) - 最重要的 - 从终端和第四和第五。这是我想要得到的输出:
I need a way to adapt this query (doing a split or whatever) so I am able to obtain the third item (comma separated) - most important - from the end and 4th and 5th. This is the output I'd like to get:
OU Where Type
--------------------------------
DS AMS N/A
DS TECHM N/A
Developers CH Agencies
Developers CH Market
Developers HK Agencies
Developers HK Market
我发现了一些例子来分割字符串,但没有完成什么我想在这里。
I found some examples to split strings but nothing to accomplish what I am trying here.
任何想法?!
任何帮助是极大的AP preciated。在此先感谢!
Any help is greatly appreciated. Thanks in advance!
鉴于你想提取值,我会从CanonicalName,而不是distinguishedName来:
Given the values you're wanting to extract, I'd start with CanonicalName, rather than DistinguishedName:
Get-ADUser -Filter * -Properties CanonicalName |
select -ExpandProperty CanonicalName |
ForEach-Object {
$Parts = $_.split('/')
$Object =
[PSCustomObject]@{
OU = $Parts[1]
Where = $Parts[2]
Type = 'N/A'
}
if ($Parts.count -ge 5)
{ $Object.Type = $Parts[3] }
$Object
}