简体   繁体   中英

Combine PSObject regex filtered results with initial value names

Need to search some string data in USERS properties inside my AD. I belive that I've made the first step and found them, but I've some troubles with combining the results in one table - I need to determine the CN or Name of the user (something to distinguish the object). Here's the code to search from 12 to 16 characters in any string properties:

$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

This gives me the table:

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

I understand that the pipe doesn't hold CN by its end. I've tried to use foreach-object but failed to rewrite correctly all functions (eg psobject.properties) with maintaining the pipe. I need something like:

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

Thanks to LotPings, this is the working result, that searches from 12 to 16 characters (regex means: at least - one uppercase letter, one lowercase letter, one number and one special character):

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM