Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Exclude lines that include string

Reply
 
Old 01-11-2007   #1 (permalink)
Brian Hoort


 
 

Exclude lines that include string

I'm trying to get a list of all users in AD except ones in OUs "foo"
and "fum", while also excluding disabled accounts. I'm having two
problems with this. I'll try and explain:

I'm getting the full list of users with:

dsquery user -limit 0

This produces output like the following but includes disabled accounts
and some oddball OUs and CNs that shouldn't really be included for
processing which need to be excluded from the output.

"CN=billgates,OU=Users,Disabled Accounts,DC=microsoft,DC=com"
"CN=melinda,OU=Users,OU=Disabled Accounts,DC=microsoft,DC=com"
"CN=billskid,OU=Users,OU=Development,DC=microsoft,DC=com"

I tried separating the disabled accounts out first (actual disabled
accounts, don't confuse with the OU=Disabled Accounts) with a:

compare-object $(dsquery user -limit 0) $(dsquery user -disabled
-limit 0) -passThru | where-object {$_.SideIndicator -eq "<="}

but that doesn't seem to work -- I've done this with a diff converted
from unix and that works properly, but where-object doesn't seem to
compare the lines as well. I end up with lots of disabled users in
there anyhow.

That's the first problem. Also, I need to exclude a few OUs from this
list. So, how can I take the above output and filter out lines that
include strings like "OU=Disabled" and "CN=Microsoft Exchange System
Objects"? This can't be done in the object model, right? Doesn't it
have to be done with strings? I can't figure out how to do that in a
PSH way (I could do it with grep, sed, etc.).

If I should be querying AD differently, great, but my understanding is
that wasn't included in PSH yet.

Thank You

bh


My System SpecsSystem Spec
Old 01-11-2007   #2 (permalink)
Gaurhoth


 
 

Re: Exclude lines that include string

Powershell actually has relatively good AD support built in through ADSI. As with all things Powershell, you can use the built in ways or any .NET ways available.

If you want a list of all objects in a given OU:

$g = [adsi]LDAP://OU=SpecificOU,DC=domain,DC=com
$g.psbase.get_children()

This returns groups, users, computers... anything under that OU. Personally I perfer to start with a .NET directorysearcher object and work from that...


$ldapQuery = "(&(objectCategory=person)(objectClass=user))"
$de = new-object system.directoryservices.directoryentry -argumentlist "LDAP://OU=SpecificOU,DC=domainDC=com"
$ads = new-object system.directoryservices.directorysearcher -argumentlist $de,$ldapQuery
$g = $ads.findall()

This will return only objects with ObjectCategory = person and ObjectClass = user. If you want to further filter that to exclude disabled users, change $ldapQuery to:

$ldapQuery = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
$de = new-object system.directoryservices.directoryentry -argumentlist "LDAP://OU=SpecificOU,DC=domainDC=com"
$ads = new-object system.directoryservices.directorysearcher -argumentlist $de,$ldapQuery
$g = $ads.findall()

Now you have just a list of non-disabled users. The one thing to keep in mind is that this is simply a SearchResult. If you want access to the *actual* directory entry you can do the following:

$direntry = [adsi]$g[0].path

You can see what methods and properties are available using

$direntry | gm



--

gaurhoth
http://gaurhothw.spaces.live.com/


"Brian Hoort" <brian.hoort@gmail.com> wrote in message news:1168536313.635590.217720@i56g2000hsf.googlegroups.com...
> I'm trying to get a list of all users in AD except ones in OUs "foo"
> and "fum", while also excluding disabled accounts. I'm having two
> problems with this. I'll try and explain:
>
> I'm getting the full list of users with:
>
> dsquery user -limit 0
>
> This produces output like the following but includes disabled accounts
> and some oddball OUs and CNs that shouldn't really be included for
> processing which need to be excluded from the output.
>
> "CN=billgates,OU=Users,Disabled Accounts,DC=microsoft,DC=com"
> "CN=melinda,OU=Users,OU=Disabled Accounts,DC=microsoft,DC=com"
> "CN=billskid,OU=Users,OU=Development,DC=microsoft,DC=com"
>
> I tried separating the disabled accounts out first (actual disabled
> accounts, don't confuse with the OU=Disabled Accounts) with a:
>
> compare-object $(dsquery user -limit 0) $(dsquery user -disabled
> -limit 0) -passThru | where-object {$_.SideIndicator -eq "<="}
>
> but that doesn't seem to work -- I've done this with a diff converted
> from unix and that works properly, but where-object doesn't seem to
> compare the lines as well. I end up with lots of disabled users in
> there anyhow.
>
> That's the first problem. Also, I need to exclude a few OUs from this
> list. So, how can I take the above output and filter out lines that
> include strings like "OU=Disabled" and "CN=Microsoft Exchange System
> Objects"? This can't be done in the object model, right? Doesn't it
> have to be done with strings? I can't figure out how to do that in a
> PSH way (I could do it with grep, sed, etc.).
>
> If I should be querying AD differently, great, but my understanding is
> that wasn't included in PSH yet.
>
> Thank You
>
> bh
>

My System SpecsSystem Spec
Old 01-12-2007   #3 (permalink)
Marcel J. Ortiz [MSFT]


 
 

Re: Exclude lines that include string

If each entry is in a separate line something like this should work...

dsquery user -limit 0 | where { $_ -notmatch 'Disabled Accounts' }

or:

@(dsquery user -limit 0) -notmatch 'Disabled Accounts' -notmatch
'CN=Microsoft Exchange System Objects'


"Brian Hoort" <brian.hoort@gmail.com> wrote in message
news:1168536313.635590.217720@i56g2000hsf.googlegroups.com...
> I'm trying to get a list of all users in AD except ones in OUs "foo"
> and "fum", while also excluding disabled accounts. I'm having two
> problems with this. I'll try and explain:
>
> I'm getting the full list of users with:
>
> dsquery user -limit 0
>
> This produces output like the following but includes disabled accounts
> and some oddball OUs and CNs that shouldn't really be included for
> processing which need to be excluded from the output.
>
> "CN=billgates,OU=Users,Disabled Accounts,DC=microsoft,DC=com"
> "CN=melinda,OU=Users,OU=Disabled Accounts,DC=microsoft,DC=com"
> "CN=billskid,OU=Users,OU=Development,DC=microsoft,DC=com"
>
> I tried separating the disabled accounts out first (actual disabled
> accounts, don't confuse with the OU=Disabled Accounts) with a:
>
> compare-object $(dsquery user -limit 0) $(dsquery user -disabled
> -limit 0) -passThru | where-object {$_.SideIndicator -eq "<="}
>
> but that doesn't seem to work -- I've done this with a diff converted
> from unix and that works properly, but where-object doesn't seem to
> compare the lines as well. I end up with lots of disabled users in
> there anyhow.
>
> That's the first problem. Also, I need to exclude a few OUs from this
> list. So, how can I take the above output and filter out lines that
> include strings like "OU=Disabled" and "CN=Microsoft Exchange System
> Objects"? This can't be done in the object model, right? Doesn't it
> have to be done with strings? I can't figure out how to do that in a
> PSH way (I could do it with grep, sed, etc.).
>
> If I should be querying AD differently, great, but my understanding is
> that wasn't included in PSH yet.
>
> Thank You
>
> bh
>



My System SpecsSystem Spec
Old 01-15-2007   #4 (permalink)
Brian Hoort


 
 

Re: Exclude lines that include string

Both of these solutions are very educational and helpful, Thank You
gentlemen.

In the second solution, the @ doesn't seem necessary. For the benefit
of everyone here, is there a reason to do it? What benefits are there
to using it?

bh

On Jan 12, 11:58 pm, "Marcel J. Ortiz [MSFT]"
<mos...@online.microsoft.com> wrote:
> If each entry is in a separate line something like this should work...
>
> dsquery user -limit 0 | where { $_ -notmatch 'Disabled Accounts' }
>
> or:
>
> @(dsquery user -limit 0) -notmatch 'Disabled Accounts' -notmatch
> 'CN=Microsoft Exchange System Objects'
>
> "Brian Hoort" <brian.ho...@gmail.com> wrote in messagenews:1168536313.635590.217720@i56g2000hsf.googlegroups.com...
>
>
>
> > I'm trying to get a list of all users in AD except ones in OUs "foo"
> > and "fum", while also excluding disabled accounts. I'm having two
> > problems with this. I'll try and explain:

>
> > I'm getting the full list of users with:

>
> > dsquery user -limit 0

>
> > This produces output like the following but includes disabled accounts
> > and some oddball OUs and CNs that shouldn't really be included for
> > processing which need to be excluded from the output.

>
> > "CN=billgates,OU=Users,Disabled Accounts,DC=microsoft,DC=com"
> > "CN=melinda,OU=Users,OU=Disabled Accounts,DC=microsoft,DC=com"
> > "CN=billskid,OU=Users,OU=Development,DC=microsoft,DC=com"

>
> > I tried separating the disabled accounts out first (actual disabled
> > accounts, don't confuse with the OU=Disabled Accounts) with a:

>
> > compare-object $(dsquery user -limit 0) $(dsquery user -disabled
> > -limit 0) -passThru | where-object {$_.SideIndicator -eq "<="}

>
> > but that doesn't seem to work -- I've done this with a diff converted
> > from unix and that works properly, but where-object doesn't seem to
> > compare the lines as well. I end up with lots of disabled users in
> > there anyhow.

>
> > That's the first problem. Also, I need to exclude a few OUs from this
> > list. So, how can I take the above output and filter out lines that
> > include strings like "OU=Disabled" and "CN=Microsoft Exchange System
> > Objects"? This can't be done in the object model, right? Doesn't it
> > have to be done with strings? I can't figure out how to do that in a
> > PSH way (I could do it with grep, sed, etc.).

>
> > If I should be querying AD differently, great, but my understanding is
> > that wasn't included in PSH yet.

>
> > Thank You

>
> > bh- Hide quoted text -- Show quoted text -


My System SpecsSystem Spec
Old 01-15-2007   #5 (permalink)
Marcel J. Ortiz [MSFT]


 
 

Re: Exclude lines that include string

@(statements) will execute the statementsand put the results in an array.
Using @() is important in the second example is important because we want
the results to be placed in an array regardless if its one item, zero or
many. If you use () and the result is one item then it won't be placed in
an array in which case -notmatch will return True or False rather than all
the elements that result in true.

PS>('foo') -notmatch 'bar'
True
PS>@('foo') -notmatch 'bar'
foo


"Brian Hoort" <brian.hoort@gmail.com> wrote in message
news:1168874478.218396.261050@51g2000cwl.googlegroups.com...
> Both of these solutions are very educational and helpful, Thank You
> gentlemen.
>
> In the second solution, the @ doesn't seem necessary. For the benefit
> of everyone here, is there a reason to do it? What benefits are there
> to using it?
>
> bh
>
> On Jan 12, 11:58 pm, "Marcel J. Ortiz [MSFT]"
> <mos...@online.microsoft.com> wrote:
>> If each entry is in a separate line something like this should work...
>>
>> dsquery user -limit 0 | where { $_ -notmatch 'Disabled Accounts' }
>>
>> or:
>>
>> @(dsquery user -limit 0) -notmatch 'Disabled Accounts' -notmatch
>> 'CN=Microsoft Exchange System Objects'
>>
>> "Brian Hoort" <brian.ho...@gmail.com> wrote in
>> messagenews:1168536313.635590.217720@i56g2000hsf.googlegroups.com...
>>
>>
>>
>> > I'm trying to get a list of all users in AD except ones in OUs "foo"
>> > and "fum", while also excluding disabled accounts. I'm having two
>> > problems with this. I'll try and explain:

>>
>> > I'm getting the full list of users with:

>>
>> > dsquery user -limit 0

>>
>> > This produces output like the following but includes disabled accounts
>> > and some oddball OUs and CNs that shouldn't really be included for
>> > processing which need to be excluded from the output.

>>
>> > "CN=billgates,OU=Users,Disabled Accounts,DC=microsoft,DC=com"
>> > "CN=melinda,OU=Users,OU=Disabled Accounts,DC=microsoft,DC=com"
>> > "CN=billskid,OU=Users,OU=Development,DC=microsoft,DC=com"

>>
>> > I tried separating the disabled accounts out first (actual disabled
>> > accounts, don't confuse with the OU=Disabled Accounts) with a:

>>
>> > compare-object $(dsquery user -limit 0) $(dsquery user -disabled
>> > -limit 0) -passThru | where-object {$_.SideIndicator -eq "<="}

>>
>> > but that doesn't seem to work -- I've done this with a diff converted
>> > from unix and that works properly, but where-object doesn't seem to
>> > compare the lines as well. I end up with lots of disabled users in
>> > there anyhow.

>>
>> > That's the first problem. Also, I need to exclude a few OUs from this
>> > list. So, how can I take the above output and filter out lines that
>> > include strings like "OU=Disabled" and "CN=Microsoft Exchange System
>> > Objects"? This can't be done in the object model, right? Doesn't it
>> > have to be done with strings? I can't figure out how to do that in a
>> > PSH way (I could do it with grep, sed, etc.).

>>
>> > If I should be querying AD differently, great, but my understanding is
>> > that wasn't included in PSH yet.

>>
>> > Thank You

>>
>> > bh- Hide quoted text -- Show quoted text -

>



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Select-String -exclude PowerShell
Add include/exclude property support to Export-Csv PowerShell
Re: gci internals: which happens first, include or exclude? PowerShell
gci internals: which happens first, include or exclude? PowerShell
One more question on -LiteralPath and -Include/-Exclude for GetChi PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46