Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Split escaped characters Expand / Collapse
Author
Message
Posted Monday, July 9, 2012 12:52 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 8:10 AM
Points: 428, Visits: 939
My network admins, in their wisdom, created admin accounts for us developers for those time when we simply must have some privileges.

As you can see, my CN appears to be based on my DisplayName (Admin-LastName, Paul) rather than a concatenation of my FirstName and sn (Paul and Admin-LastName, respectively).

CN=Admin-Lastame\, Paul,OU=Users,OU=Data_Admins,OU=Admins,DC=....

My problem is that I need to split the CN based on comma, and don't know how to get -split to ignore the escaped comma after Admin-LastName.

I was not able to find, among the myriad examples in the split docs on Technet, this kind of case.

How do I do this? Do I need to replace the escape?

For that matter, is there a better way of getting to the endpoint, which is, I need a list of all AD groups and their members in a FirstName.LastName format, not using QAD cmdlets?


TIA.



Post #1327076
Posted Monday, July 9, 2012 2:03 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:38 PM
Points: 7,097, Visits: 12,598
I do not do much AD work these days, let alone from PowerShell, but I do know of the AD cmdlets MS published. http://technet.microsoft.com/en-us/library/ee617195

Here is what I got (using replacement) with some basic PS in case you proceed down the current path. I could not find a slick split option or similar built into PS:

$str = 'CN=Admin-Lastame\, Paul,OU=Users,OU=Data_Admins,OU=Admins'
#$str
[string[]]$a = $str.Replace('\,', '~~~').Split(',')
#$a
#$a.Count

for ($i=0; $i -lt $a.Count; $i++)
{
$a[$i] = $a[$i].Replace('~~~',',')
}

$a



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1327122
Posted Monday, July 9, 2012 2:43 PM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 6:19 AM
Points: 60, Visits: 889
Here's a regex that might work for you.
$cn = 'CN=Admin-Lastame\, Paul,OU=Users,OU=Data_Admins,OU=Admins,DC=....'
$pat = 'CN=\w+\-(\w+)\\,\s+(\w+),.*$'
[regex]::Replace($cn, $pat, '$2 $1')

It returns
Paul Lastame

Edit: just reread your op. rewritten for just firstname lastname

Edit2: I knew that original regex wouldn't handle two word last names like St. James or hyphenated names which was why I said "might work". Anyway while playing golf with the regex I modified it to handle hyphens and two word last names. If you don't have those kind of names the original works. Replace with the pattern below if you do.
$pat = 'CN=Admin-(\w.+)\\, (\w+),.*$'

Post #1327156
Posted Monday, July 9, 2012 5:17 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 6:46 PM
Points: 36,944, Visits: 31,446
Never mind...

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1327214
Posted Tuesday, July 10, 2012 5:27 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 8:10 AM
Points: 428, Visits: 939
@opc.three: thanks. I can't wait for the time when we move on from XP , and I can begin using those AD cmdlets.

@Bruce: thanks for that. I can see I''m finally going to have to learn regex.




Post #1327441
Posted Tuesday, July 10, 2012 8:36 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 6:46 PM
Points: 36,944, Visits: 31,446
With that thought in mind, let me ask... do you really need to do this in PowerShell or is the ultimate target of your efforts to have something stored in T-SQL???

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1327613
Posted Tuesday, July 10, 2012 9:39 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 8:10 AM
Points: 428, Visits: 939
We grant SQL Server / DB access to Windows groups.

We determine which version of the app (read "Server"), which projects (read "databases") users can see, as well as what functionality is enabled, based on group membership.

Ultimate goal is to have a table containing AD Group Name and samaccountname.

Currently doing this in PS because the custom system stored proc I'd been using in SQL2K is not allowed in SQL 2K8.

The table needs to be refreshed every 15 minutes or so, as users are added/shuffled around.
We have ~800 groups.

Takes 19 seconds in PS + 3 to import to SQL Server, 50 seconds in T-SQL using xp_logininfo and xp_enumgroups and a cursor.

Also, xp_logininfo only returns results for groups granted server access.





Post #1327700
Posted Tuesday, July 10, 2012 9:47 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:38 PM
Points: 7,097, Visits: 12,598
schleep (7/10/2012)
@opc.three: thanks. I can't wait for the time when we move on from XP , and I can begin using those AD cmdlets.

Bummer. I am in the same boat at the current shop on the desktop (I wanted to give them a quick spin). Seeing your setup however I am surprised you do not have an app server you could use for this. Granted, you would need one to develop on before releasing it into the wild, but sometimes with these types of system admin tasks that's a good thing.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1327712
Posted Tuesday, July 10, 2012 9:54 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 8:10 AM
Points: 428, Visits: 939
The edict came from on high a couple of years ago: NO NEW SERVERS! (except when a prod box dies).



Post #1327714
Posted Tuesday, July 10, 2012 10:00 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:38 PM
Points: 7,097, Visits: 12,598
Booo

You could explore the possibility of using the command line tool dsquery which is available on XP. I have used it before, but never for automation. However with PS it may be a cinch to use stdout coming from it.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1327718
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse