Jump to content

Faster way to trim strings [solved]


Recommended Posts

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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

Link to comment
Share on other sites

  • Moderators

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.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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
Link to comment
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

Link to comment
Share on other sites

  • Moderators

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.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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.

Link to comment
Share on other sites

  • Moderators

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.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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

Link to comment
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
Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

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.

Link to comment
Share on other sites

  • Moderators

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.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...