【问题标题】:Combine PSObject regex filtered results with initial value names将 PSObject 正则表达式过滤结果与初始值名称相结合
【发布时间】:2018-07-03 12:28:39
【问题描述】:

需要在我的广告中的用户属性中搜索一些字符串数据。我相信我已经迈出了第一步并找到了它们,但是将结果组合在一个表中时遇到了一些麻烦-我需要确定用户的 CN 或名称(用于区分对象的东西)。 这是在任何字符串属性中搜索 12 到 16 个字符的代码:

$search = 'OU=root,DC=contoso,DC=com'
$props =    @(
            'CN',
            'City',
            'Company',
            'Department',
            'Description',
            'Division',
            'Fax',
            'HomeDirectory',
            'Homepage',
            'HomePhone',
            'Initials',
            'MobilePhone',
            'Office',
            'OfficePhone',
            'Organization',
            'OtherName',
            'POBox',
            'PostalCode',
            'State',
            'StreetAddress',
            'Title'
            )
Get-ADUser -Filter * -Properties * -SearchBase $search | Select $props |
%{$_.psobject.properties} | 
?{$_.Value -match "(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{12,16}"} | 
Format-Table @{N='CN';E={$_.CN}},Name,Value -AutoSize

这给了我桌子:

CN      Name        Value
____    ____        _____
        Description 2f565#124s$Dsa

我知道管道的末端没有容纳 CN。 我尝试使用 foreach-object,但未能正确重写所有函数(例如 psobject.properties)来维护管道。 我需要类似的东西:

(User)CN                                        PropName        Value
CN=bradpitt,OU=Users,OU=root,DC=contoso,DC=com  Description     2f565#124s$Dsa

【问题讨论】:

  • {$CN=$_.CN;...}Format-Table @{N='CN';E={$CN}} 的开头插入 ForEach
  • 喜欢这个? Get-ADUser -Filter * -Properties * -SearchBase $search | Select $props | ForEach-Object { $CN=$_.CN | %{$_.psobject.properties} | ?{$_.Value -match "(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{6,20}"} | Format-Table @{N='CN';E={$CN}},Name,Value -AutoSize } - 什么都不给(
  • 不,那是分号 ; 试试 ForEach{$CN=$_.CN;$_.psobject.properties} |
  • 成功了!谢谢!很抱歉之前可能会提出一些愚蠢的问题,但我不是编码员 - 只是抓取了一些脚本并将其合并。总的来说,我们会有这个:Get-ADUser -Filter * -Properties * -SearchBase $search | Select $props | ForEach-Object { $CN=$_.CN ; $_.psobject.properties} | ?{$_.Value -match "(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{12,16}"} | Format-Table @{N='CN';E={$CN}},Name,Value -AutoSize

标签: powershell properties pipe psobject


【解决方案1】:

感谢 LotPings,这是搜索 12 到 16 个字符的工作结果(正则表达式意味着:至少 - 一个大写字母、一个小写字母、一个数字和一个特殊字符):

$search = 'OU=root,DC=contoso,DC=com'
$props =    @(
            'City',
            'Company',
            'Department',
            'Description',
            'Division',
            'Fax',
            'HomeDirectory',
            'Homepage',
            'HomePhone',
            'Initials',
            'MobilePhone',
            'Office',
            'OfficePhone',
            'Organization',
            'OtherName',
            'POBox',
            'PostalCode',
            'State',
            'StreetAddress',
            'Title'
            )
Get-ADUser -Filter * -Properties * -SearchBase $search | Select $props |
            ForEach-Object { $CN=$_.CN ; $_.psobject.properties} |
?{$_.Value -match "(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{12,16}"} | Format-Table @{N='CN';E={$CN}},Name,Value -AutoSize

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多