gpt4 book ai didi

Powershell - 将特定单词保留在字符串中

转载 作者:行者123 更新时间:2023-12-02 23:47:06 27 4
gpt4 key购买 nike

我正在做 AD 提取并对字段“distinguishedname”进行排序,并且我只想保留代表用户本身的“父 OU”的值的特定部分。

我正在运行此命令来添加所有用户的提取:

import-module activedirectory
get-aduser -filter * -properties *| Select-Object -Property SamAccountName,CN,co,ExtensionAttribute10,extensionAttribute11,extensionAttribute12,EmailAddress,whenCreated,Enabled,LastLogonDate,accountexpirationdate,distinguishedname |Sort-Object -Property Name | Export-Csv -Delimiter ";" -path "u:\theOutFile_NOFILTER_July.txt"

专栏 "distinguishedname"看起来像这样:
distinguishedname
CN=familly\, user,OU=Remote Users,OU=New York,OU=My,DC=Company,DC=Local
CN=nameless\, cat,OU=Remote Users,OU=Ottawa,OU=My,DC=Company,DC=Local
CN=Cameron\, James,OU=Regular Users,OU=Hollywood,OU=My,DC=Company,DC=Local
CN=Bon\, Jean,OU=regular Users,OU=Springfield,OU=My,DC=Company,DC=Local

注意 7 月 10 日
有一段时间我会打那些线:
CN=Dog\, Cesar,OU=Special Accounts,OU=Regular Users,OU=Alma,OU=My,DC=Company,DC=Local
CN=keys\, Alicia,OU=Special Accounts,OU=Regular Users,OU=Paris,OU=My,DC=Company,DC=Local
CN=Clansy\, Door,OU=Map Drives,OU=Remote Users,OU=Rome,OU=My,DC=Company,DC=Local

在这种情况下,我会得到这样的结果 Remote Users一个 Regular Users而不是城市。我已经尝试对您给出的命令进行一些修改,但徒劳无功。

但我希望第一个命令返回这个结果:
distinguishedname
New York
Ottawa
Hollywood
Springfield

我无法努力找到方法。

提前致谢

最佳答案

Select-Object 具有一个非常通用的功能,可以使用散列代替属性名称来创建计算属性,其中键“名称”设置为计算属性的名称(实际上是列标题),“表达式”设置为一个代码块,用于确定管道中每个对象的属性值。这将做你想要的:

Get-Aduser -Filter * -Properties * | Select-Object -Property SamAccountName,CN,co,ExtensionAttribute10,extensionAttribute11,extensionAttribute12,EmailAddress,whenCreated,Enabled,LastLogonDate,accountexpirationdate,@{Name='distinguishedname'; Expression={[regex]::match($_.distinguishedname,'OU=.+?OU=(.+?),(OU|DC)=').Groups[1].Value}} | Sort-Object -Property Name | Export-Csv -Delimiter ";" -Path "u:\theOutFile_NOFILTER_July.txt"

以下是正在发生的事情的分割:
  • Name='distinguishedname'告诉它创建一个名为“distinguishedname”的新列。我使用该名称来匹配您正在查找的输出示例,但它不必是现有属性的名称。将名称更改为更能描述您正在计算的值的名称可能更有意义,例如Name="parentOU" .
  • [regex]::match用于从 中提取所需部分$_.distinguishedname 使用正则表达式 OU=.+?OU=(.+?),(OU|DC)= ,它使用匹配组隔离列表中第二个 OU 的名称。
  • .Groups[1].Value返回第一个匹配组的值(与第一组括号的内容匹配的部分)。 .值 没有 .Groups[1] 将返回整个匹配的字符串,从第一个 欧= = 以下父 OU 的名称。以下也可以使用,使用零宽度断言而不是匹配组:[regex]::match($_.distinguishedname,'(?<=OU=.+?OU=).+?(?=,(OU|DC)=)').Value
  • 关于Powershell - 将特定单词保留在字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17555642/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com