Dynamic script required for exmerge

I'd like to manage archiving of users mailboxes automatically at server level. It's March this month and I'd like the server to automatically archive messages that are, let's say, older than February 1st into a PST file called 2010.pst
This pst file would live in the user's personal folder on the server within a folder called Mail Archives.

This procedure would be run once a month on the 1st of the month. Since we can calculate the date range (today - 2 months) - (today - 1 month), the user's name is a variable and so is the year, I thinking it may be possible to write a script to make Exmerge do this automatically.

I'm anticipating that it might involve dynamically creating the exmerge.ini file to contain the correct variables first.

Anyone feel they could make this work?

LVL 1
edz_pgtAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sr75Commented:
I am positive it can be done.  I have done something similar.  Only I don't have time to write it for you and I don't have my script library with me at the moment.  But I am really sure it is possible.
0
Glen KnightCommented:
I have an article here that explains how to configure exmerge: http://www.experts-exchange.com/articles/Software/Server_Software/Email_Servers/Exchange/Export-mailboxes-using-Exmerge-and-Exchange-2003.html
Once you get to the end of the wizard, select "Save Settings" and then click Cancel, otherwise it will perform the task there and then.

Once you are happy, you can setup a scheduled job using the task scheduler to run the following command:

EXMERGE -F C:\PathToExmergeINIFile\EXMERGE.INI -B

As for dynamically changing this you could use a batch file as part of the process so you run a batch file that Edits the INI file using a command line ini file editor like: http://www.softpedia.com/get/System/File-Management/IniFile.shtml and then runs the exmerge command.

You would also need to rename the files after the exmerge process because by default they will export to aliasname.pst
0
edz_pgtAuthor Commented:
Thanks for the tips so far but I feel I'm still drowning. I've had a play with IniFile and I can see how it can create and amend an entry in an ini file. However, I'm now struggling with assembling a date variable and inserting it appropriately in the code.
I suppose the first step is to work out how to edit the start and finish dates in the ini file.
The second step would be to calculate those dates prior to amending the ini file.
Can anyone help me with the syntax for configuring inifile.exe to do this please?
The exmerge.ini file is attached for reference.

; EXEMERGE.INI
; This file is for use with the EXMERGE.EXE program, version 4.00 or higher.

[EXMERGE]
LoggingLevel=0
LogFileName=ExMerge.log
DataDirectoryName=D:\ARCHIVES
MergeAction=0
RestoreDB=0
SourceServerName=ZEBRA01
DomainControllerForSourceServer=zebra01
SrcServerLDAP-Port=
DestServerName=
DomainControllerForDestServer=
DestServerLDAP-Port=
SelectMessageStartDate=01/01/1995   00:00:00
SelectMessageEndDate=12/31/1995   23:59:59
ListOfFolders=
FileContainingListOfFolders=
FoldersProcessed=2
ApplyActionToSubFolders=0
FileContainingListOfMailboxes=
FileContainingListOfDatabases=
RemoveIntermediatePSTFiles=1
DateAttribute=0
DataImportMethod=3
ReplaceDataOnlyIfSourceItemIsMoreRecent=1
CopyUserData=1
CopyAssociatedFolderData=0
CopyFolderPermissions=0
CopyDeletedItemsFromDumpster=0
FileContainingListOfMessageSubjects=
SubjectStringMatchCriteria=0
FileContainingListOfAttachmentNames=
AttachmentNameStringMatchCriteria=0
MapFolderNameToLocalisedName=0
[International]
DefaultLocaleID=2057
UseLastLogonLocaleID=0

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Glen KnightCommented:
So the command you would need would be:

inifile c:\pathtoexmerge\exmerge.ini [EXMERGE] SelectMessageStartDate=01/03/2010   00:00:00
inifile c:\pathtoexmerge\exmerge.ini [EXMERGE] SelectMessageEndDate=01/03/2010   23:59:59
0
edz_pgtAuthor Commented:
Great - thanks for that.
Now how do I calculate (Today - 1 month) and then produce a similar line of code?

0
Glen KnightCommented:
Now that I don't know, you will probably need to post a new question in the VBScript zone
0
edz_pgtAuthor Commented:
Ah, ok well thanks for you help so far. I did do some digging on the net about this and I get the impression it's possible to do it within a batch file although the coding was long winded and I couldn't get my head around it quickly.
This question is already tagged to the vbscript zone anyway but does anyone else have anything that might help me here?
0
Glen KnightCommented:
I will ask a few other experts to take a look at the vbscript part of this question for you.
0
Glen KnightCommented:
Basically what we need is to create a VBScript that will take todays date minus a month from it and then append it to the end of the command above.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
so, the question now is about how to read/write ini files from vbscript?
what about checking out these functions from here:
http://www.motobit.com/tips/detpg_asp-vbs-read-write-ini-files/
0
Glen KnightCommented:
I suppose you could do it that way all in the same VBScript or just need to get the current date minus a month from it and then append it to the end of these commands and run the command from a VBScript:

inifile c:\pathtoexmerge\exmerge.ini [EXMERGE] SelectMessageStartDate=01/03/2010   00:00:00
inifile c:\pathtoexmerge\exmerge.ini [EXMERGE] SelectMessageEndDate=01/03/2010   23:59:59
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>Now how do I calculate (Today - 1 month) and then produce a similar line of code?
sorry, that's what you need...

in vbscript, you have the dateserial(), dateadd() and datepart() functions ...
http://www.w3schools.com/VBscript/func_dateserial.asp

for example, get first of this month:
d = DateSearial(year(), month(), 1)

previos month:
d = dateadd("m", -1,DateSearial(year(), month(), 1))

etc.
0
edz_pgtAuthor Commented:

LOL - yeah ok. Now my head is really buzzing. I'm not very familiar with VBS so this is all a massive learning curve for me. Please treat me as a complete newbie in this area but I'll hopefully pick it up fairly quickly.
 
So, assuming that I put this into a vbs file, how do I then get it to alter the ini file accordingly?
0
Glen KnightCommented:
OK, this probably isn't the most pretty way of doing it but I have tested it and it works.

Please consider I am no programmer.

NewDate = DateAdd("d", -9, date()) 
Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run "c:\path_to_inifile_exe\inifile.exe c:\path_to_exmerge_ini\exmerge.ini [EXMERGE] SelectMessageStartDate=" & NewDate & "   00:00:00"

Open in new window

0
Glen KnightCommented:
The line:

NewDate = DateAdd("d", -9, date())

Should read:

NewDate = DateAdd("m", -1, date())

I did warn you I wasn't a programmer :)



0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
considering that you seemed to have wanted the first of previous month, it would actually be:


NewDate = dateadd("m", -1,DateSearial(year(), month(), 1))

Open in new window

0
edz_pgtAuthor Commented:
ok - cool. I'm getting along with this a bit better now. Thanks for the coding, demazter.
I'm interested in a bit of code that angellll posted which referred to the first day of this month. I can't get this to work as it always complains about the number of arguments for the 'year' element.
I'd like to be able to calculate:
1. The first day of this month
2. The first day of last month
3. The year it was last month
I need to use the year to create a directory name and/or filename to put the PST files in.
Any ideas?
0
Glen KnightCommented:
Yes I couldn't get that to work either that's why I posted my code :)
0
edz_pgtAuthor Commented:
Acutally, I've just noticed that I've asked for the wrong thing, too!

I need to be able to calculate:
1. The first day of last month
2. The first day of 2 months ago
3. The year it was 2 months ago
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry, year(), month() function require a date value to be passed, my fault (working with toooooooo many systems)
http://www.w3schools.com/VBScript/func_year.asp
NewDate = dateadd("m", -1,DateSearial(year(date()), month(date()), 1))

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>3. The year it was 2 months ago

Year2MonthAgo = Month( dateadd("m", -2,DateSearial(year(date()), month(date()), 1)))
0
edz_pgtAuthor Commented:
Thanks angellll.
Amending your typo (there's only one 'a' in serial) :oP
... The result of this:
Month( dateadd("m", -2,DateSerial(year(date()), month(date()), 1)))
is displayed as this:
1   00:00:00
Any idea why?
0
edz_pgtAuthor Commented:
OK - figured that one out a bit. It should have been this instead:
Year ( dateadd("m", -2,DateSerial(year(date()), month(date()), 1)))
but it's still putting the timestamp at the end like this:
2010   00:00:00
0
edz_pgtAuthor Commented:
Can't believe I'm so stupid! - Sorry, the timestamp was as a result of copying and pasting a bit of code that added the zeros as text after the field. I just didn't spot it.
Sorry about that - fantastic work - thanks for your help. :o)
 
0
Glen KnightCommented:
Make sure you add the 00:00:00 on the end of the execute string otherwise exmerge will not read the value
0
edz_pgtAuthor Commented:
Yes, I realise that. I need the year so that I can create a directory to put the PST files in. I'm using this code:
PeriodYear = Year(dateadd("m", -2,DateSerial(year(date()), month(date()), 1)))

wshshell.run "inifile.exe exmerge.ini [EXMERGE] DataDirectoryName=D:\ARCHIVES\" & PeriodYear
The system should now produce a folder full of PST files, one for each user. For security purposes, I need to move each one of those files to the relevant user directory.
Since the file name prior to the '.pst' is the same as the username, and therefore the name of the user's folder, do you know of a way to pick up the name of each pst file and use it's name to move it to the relevant folder?
eg, user: Joe Bloggs.
File name: D:\archives\2010\joebloggs.pst
Should be moved to D:\usershares\joebloggs\archives\2010.pst
I gues it would be easiest to do this whilst the current directory is D:\archives\2010 so that the prefixed path doesn't complicate things.
Do you know how to do this? If not, I'll close this thread and open a new one.
0
Glen KnightCommented:
I'm sorry I have already exhausted my programming knowledge.
I was impressed I was able to get that far :)
0
edz_pgtAuthor Commented:
Well, I was impressed, too :)
0
Glen KnightCommented:
Thanks :)
0
Glen KnightCommented:
Although to be fair a3 might have helped a little but but I couldn't get it to work :)
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
well, about the programming part:

>eg, user: Joe Bloggs.
>File name: D:\archives\2010\joebloggs.pst
>Should be moved to D:\usershares\joebloggs\archives\2010.pst

so:
strusername = "Joe Bloggs"
strSourceFileName = "D:\archives\" & year( date()) & "\" lcase(replace(strUsername, " ", "")) & ".pst"
strDestFileName = "D:\usershares\" lcase(replace(strUsername, " ", "")) & "\archives\" & year( date()) & ".pst"

and then you have just to "move" the file, which is peanuts again, once you know how:
http://www.devguru.com/technologies/vbscript/quickref/filesystemobject_movefile.html
0
edz_pgtAuthor Commented:
Thanks angellll but is it also possible to do this if I don't know the names of the users beforehand?
The reason being, if a new user is added to Exchange then a new PST file will appear as a result of this Exmerge. Without altering the script, the pst file won't get moved.
So, is it possible to do a directory listing for, let's say, *.pst and then strip the '.pst' extension to build an array of usernames?
If so, could we then build a subroutine to move each mailbox in turn?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sure, that is possible.
something like this ...
On Error Resume Next
  Dim fso, folder, files, NewsFile,sFolder
 
  Set fso = CreateObject("Scripting.FileSystemObject")

  sFolder = "D:\archives\" & year( date())
  Set folder = fso.GetFolder(sFolder)

  Set files = folder.Files
 
  For each folderIdx In files
    strSourceFileName = folderIdx.Name
    strUserName = left(strSourceFileName, len(strSourceFileName) -4)
   
    strDestFileName = "D:\usershares\" strUsername & "\archives\" & year( date()) & ".pst"
     fso.MoveFile sFolder & "\" & strSourceFileName, strDestFileName
  Next

Open in new window

0
edz_pgtAuthor Commented:
thanks Angellll - I now have this working although it does rely on the archives folder existing. I've tried to add a bit of code to create the folder if it doesn't already exist but although I don't get an error, this doesn't seem to create the folder, either. Could you tell me what's not right please?...
Much appreciated :)

If not exists ("C:\Test\UserShares\" & strusername & "\archives") Then
 CreateFolder("C:\Test\UserShares\" & strusername & "\archives")
    end if

Open in new window

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should do:
if not fso.FolderExists("C:\Test\UserShares\" & strusername & "\archives") Then
   fso.CreateFolder("C:\Test\UserShares\" & strusername & "\archives")
end if

Open in new window

0
edz_pgtAuthor Commented:
What a star! - Thankyou so much for your help!
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Glad I could help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.