Sign in to follow this  
Followers 0
JLogan3o13

Faster way to trim strings [solved]

15 posts in this topic

#1 ·  Posted (edited)

I am working with a customer to clean up their A.D. infrastructure. They have apparently never deleted a computer object from A.D. when it was retired, and now have on the order of 10,000 extraneous objects. I have a script which polls A.D. and returns the FQDN, as well as the last time the computer's password was changed. The thought is, if the password hasn't changed in 6 months, I might as well delete it. The script returns information in this format:

CN=MyMachine-DS-1,OU=Member Servers,DC=Contoso,DC=org ; True ; 12/21/2011 3:11:56 PM

CN=MyMachine1-FHC,OU=Computers,DC=Contoso,DC=org ; True ; 12/21/2011 3:11:56 PM

CN=12898-rma-1,OU=Computers,DC=Contoso2,DC=org ; True ; 12/21/2011 3:11:56 PM

Most other customers where I have employed this script have some sort of standard naming convention. When this is the case, I can basically parse through the list of machines, and delete everything after a certain number of characters. Unfortunately this company has no consistency at all, making this unviable. The one thing I do have is the way A.D. breaks up the FQDN. My thought is I should be able to parse through each line until I find the first comma, then delete everything after it, but I haven't hit on the correct combo yet. I'm thinking the only way would be with StringRegExp, which I have yet to wrap my brain around.

Can anyone suggest another way of accomplishing this, or point me in the right direction using StringRegExp?

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites



jlogan3013,

If you are trying to return the data/time at the end of each string than one poosible way is

$pwd_chg = StringRegExp($Value, "d{2}/d{2}/d{4}.*PM", 3)

Another way would be to split the string on the semi-colon and use the ubound - 1 entry...

I'm sure that there are many other techniques, these just come to mind immediately.

I'm just learning regex's so I suspect that another, better solution will be along shortly!

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Thanks for the suggestions Klyomas. What I would like to end up with is simply the computer name, nothing beyond it. So the Common Name (CN=) is easy to get rid of. I'd like to then eliminate anything after the first comma, leaving me with only the machine name.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

You want to end up with only the cn of a machine? (ie MyMachine-DS-1 ?) I'm no AD guru, but I'd think you'd want the FQDN, especially when deleting.

Your script spits out everything that is inactive, or do you have to sift through the time/date at the end? What is your next step after that, assuming you have what you want?


While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

jlogan3013,

$mach_name = StringRegExp($Value, "CN=(.*?),", 3)
will do that with regex...

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

jlogan3013,

I am not familiar with AD or WMI but from what I have seen of it in this forum WMI supports an SQL like language. Can you find todays date - 6 months and run a select query by equal to or older than that date? Then perform the delete or archive action.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Hi, someone. You are correct that I use the FQDN for manipulating the data, e.g. deleting any machines that have been inactive for x number of months. But when presenting to the customer, and having them look through a list of 20,000 objects, I invariable get one exec who wants it "neat". I've actually been asked on this job to "pretty it up" for the presentation.

The script already sorts the machines by those that have not checked in in 3 months, 6 months, 1 year, and then a bucket for "longer" (they have machines going back to 2005). I would simply like to strip the additional info and push it into an excel file for presentation to management.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Is this what you're trying to do? I'm weak with RegEx, and I'll try to accomplish something with the String functions first..

$String = "CN=12898-rma-1,OU=Computers,DC=Contoso2,DC=org ; True ; 12/21/2011 3:11:56 PM"
$Result = StringInStr($String, ",")
ConsoleWrite(StringTrimLeft(StringTrimRight($String, StringLen($String) - ($Result - 1)), 3) & @LF)

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

Hi, klyomas. I apologize, I didn't explain very well in my original post. See my answer to someone; the actual work of the script is handled - I am mainly just looking to provide management with a list of machines, showing only the machine name, categorized by how long they've been inactive. The only piece I'm missing is being able to sort the list, and turn this:

CN=MyMachine-DS-1,OU=Member Servers,DC=Contoso,DC=org ; True ; 12/21/2011 3:11:56 PM

CN=MyMachine1-FHC,OU=Computers,DC=Contoso,DC=org ; True ; 12/21/2011 3:11:56 PM

CN=12898-rma-1,OU=Computers,DC=Contoso2,DC=org ; True ; 12/21/2011 3:11:56 PM

Into this:

MyMachine-DS-1

MyMachine1-FHC

12898-rma-1

What has me banging my head against the wall is this company's inconsistent naming convention. Some PC's are 7 characters long, some 10, and everywhere in between.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

jlogan3013,

When I use the above regex on this string

CN=MyMachine-DS-1,OU=Member Servers,DC=Contoso,DC=org ; True ; 12/21/2011 3:11:56 PM
CN=MyMachine-DS-1,OU=Member Servers,DC=Contoso,DC=org ; True ; 12/21/2011 3:11:56 PM
CN=MyMachine1-FHC,OU=Computers,DC=Contoso,DC=org ; True ; 12/21/2011 3:11:56 PM
CN=12898-rma-1,OU=Computers,DC=Contoso2,DC=org ; True ; 12/21/2011 3:11:56 PM
I get an array that looks like this
[0] = MyMachine-DS-1
[1] = MyMachine-DS-1
[2] = MyMachine1-FHC
[3] = 12898-rma-1

kylomas

edit: That would be the regex from post #5

edit2: I repeated line #1 to show that it works with multiple duplicate values

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Ooooohhh ok gotcha. Check out kylomas' stringreg, I've just used stringtrim to do that... basically stringtrimleft to remove the CN=, then use stringinstr to find the ',' position, and keep only up to that. You can do it all in one line if you want.


While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd

Share this post


Link to post
Share on other sites

I ended up using somdcomputerguy's three lines, but also got it working with klyomas' regex. Both work great. Thanks, all.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Looks like a lot of work that could have just been dumped into sqlite database for easy querying.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Hi, SmokeN. I don't disagree, but this is more an exception than the norm, and rather than querying I am attempting to format something for some non-technical folks to look at. This result can easily be pumped into an Excel spreadsheet for them to go over at their leisure.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

May I suggest my AD UDF to query and manage Active Directory? Functions _AD_GetPasswordExpired and _AD_DeleteObject is all you need to do what you want.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0