Tuesday, 24 January 2012

a little problem with DSquery & DSget

Just came across a knotty little issue with one of our domains, and it went something like this:

2nd line support engineer: Can you write me a script to give me a list of all the users whose accounts have expiry dates and whether those accounts are disabled or not

Me: no need. Just use

dsquery user -limit 0 | dsget user -samid -acctexpires -disabled

2nd line support engineer: that's only showing me users that don't have expiry dates - and i know for a fact that some of them do.

Me: (Assuming that the items wanted were simply being swamped by the vast number of accounts that didn't have an expiry date)

ok, then try this:
dsquery user -limit 0 | dsget user -samid -acctexpires -disabled | grep -v "never"


I supplied a copy of grep.exe from the gnu unix utils ports, as i couldn't make find work after two pipes for some reason
http://sourceforge.net/projects/unxutils/

2nd line support engineer: now i'm getting nothing at all

Me:
It's at this point I decide it's probably worth shadowing the engineer's TS session and having a look for myself what is going on

After deconstructing the pipe and using text files:

dsquery user -limit 0 > users.txt (Worked fine)
type users.txt | dsget user -samid (produced some results and then failed with the error:

dsget failed: Directory object not found.
type dsget /? for help.

followed by a PARTIAL list of user samid's (causing the error to scroll off-screen and making it difficult to find))

Looking at the last samid on the list, i searched for that user in users.txt - then looked at the next user in the list. Gotcha!!!!

The next user in the list had an apostrophe in his name! - very strange.... I'd have expected microsoft to sort that one out fairly quickly.

did a google - found http://www.rlmueller.net/CharactersEscaped.htm which (although suggesting that ' was a valid character) also suggested deconstructing the command using text files and manually editing the user DSNs. - not my favorite solution

With a little but of poking around in the help for the ds command, i found a couple of highly interesting switches

-uco output unicode
-uci input unicode
-uc both of the above

and so, with a few judicious switches on the initial syntax, the command ran through without errors:

Dsquery user –uc –limit 0 | dsget user –uci –samid –acctexpires –disabled | grep –v “never”

Problem solved.

Hope this can help someone else out there with a similar problem!

11 comments:

  1. you are a genius. Really helped me out.

    thanks man.

    alberthoward@live.com

    ReplyDelete
  2. Thanks very much. The command was very helpful.

    ReplyDelete
  3. Thanks Ignatius,
    That tip really help me!

    Joao from Brazil

    ReplyDelete
  4. I tried to run the command in Windows PowerShell it would fail with the following message:

    dsget failed:The value for parameter 'Target object for this command' does not appear to be in Unicode, but the -uc and/or -uci switches were specified.
    type dsget /? for help.

    Running the same command in cmd.exe worked fine, though.

    ReplyDelete
  5. You sir are a legend! Thank you so much.

    ReplyDelete
  6. arrived here after googling for the exact same issue. thanks much!!

    ReplyDelete
  7. Yep, it worked. :)

    ReplyDelete
  8. Thanks very much, you realy help me. it worked :)

    ReplyDelete
  9. Thanks for this research, you guided me to the right way... I still can't pipe to dsget cause -uc doesn't replace the ñ character, but now I know where is the problem ;)

    ReplyDelete
  10. Brilliant, thanks very much for that useful tip. This problem was getting very tiresome.

    Tim

    ReplyDelete
  11. Most excellent tip, thank you for sharing!

    Brad

    ReplyDelete