PowerShell的&放大器;获取-ADUser便有 - 斯普利特OU获得第3,第4和第5元素的结束 [英] Powershell & Get-ADUser - Split OU getting the 3rd, 4th and 5th elements from the end

查看:256
本文介绍了PowerShell的&放大器;获取-ADUser便有 - 斯普利特OU获得第3,第4和第5元素的结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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
}

这篇关于PowerShell的&放大器;获取-ADUser便有 - 斯普利特OU获得第3,第4和第5元素的结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆