Monday, March 17, 2014

GroupWise Object API vbscript to archive all GW email in account

'----------------------------------------------------------------------------------------------------------
'GWMailArchive.vbs
'Version 1.2
'Created by: Corey Sines
'Date: 2/19/2014
'Description: Archives all GroupWise Mail Messages to the desired Archive Folder, using the GroupWise
'  Object API
'----------------------------------------------------------------------------------------------------------
'------------Begin Section to Changing to 32bit wscript/cscript if launched on 64bit windows---------------
dim oSH,oFS,strOldWSH
Set oSH = CreateObject("WScript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")
If (InStr(WScript.FullName, "cscript")) Then
strOldWSH = oSH.ExpandEnvironmentStrings("%SYSTEMROOT%\SysWOW64\cscript.exe")
Else
strOldWSH = oSH.ExpandEnvironmentStrings("%SYSTEMROOT%\SysWOW64\wscript.exe")
End If
If oFS.FileExists(strOldWSH) Then
    If Not UCase(WScript.FullName) = UCase(strOldWSH) Then
        oSH.Run """" & strOldWSH & """ """ & WScript.ScriptFullName & """", 1, False
        WScript.Quit
    End If
End If
' ------------------------------------------------End Section----------------------------------------------
'----------------------------------------------------------------------------------------------------------
'------------- Begin Section to Check Arguments and set Variables, or Return Proper Usage------------------
dim Args, sGWArchivePath, sGWUserName, sGWPassword
If wscript.arguments.count = 3 then  ' needs all 3 arguments
Set Args = Wscript.Arguments
Else
Wscript.echo "Usage: GWMailArchive.vbs PathtoArchive GWUsername GWPassword"
wscript.quit(1)
End If
' Setting variables to Arguments supplied
sGWArchivePath = Args(0)
sGWUserName = Args(1)
sGWPassword = Args(2)
' ------------------------------------------------End Section----------------------------------------------
'Script starts here...
On Error resume next
 dim wshShell, wshSystemEnv, fso
 dim GWApp, GWAccount, oMsg
 dim iTotalMsgCount, iCount, iPercentComplete
 dim objExplorer

 Set wshShell = CreateObject("WScript.Shell")
 Set wshSystemEnv = wshShell.Environment( "USER" )
 Set GWApp = CreateObject("NovellGroupWareSession") ' instantiating the COM Object for "GW Object API"
 Set GWAccount = GWApp.Login(sGWUserName,SGWPassword) ' Creating and logging into a GroupWise Session
 Set fso = CreateObject ("Scripting.FileSystemObject")
 
 'wscript.echo "GroupWise Archive Path: " & GWAccount.PathToArchive & " : GroupWise FID for User: " & GWAccount.AccountProperty(9) & vbCrLf

 If (IsNull(GWAccount.PathToArchive)) Then ' Checking if no Archive Location was set
 wscript.echo "No Archive Path was set for the user account:" & sGWUserName & ", likely the user never has had an Archive..."
 wshSystemEnv( "TEMPDCP" ) = "NO GROUPWISE ARCHIVE FOUND" 'setting environment variable
 Else
 'Backing up the Old GroupWise Archive Value to an USER Environment Variable "TempVBSValue"
 Wscript.echo "GroupWise Archive Path was set to: " & GWAccount.PathToArchive
 wshSystemEnv( "TEMPDCP" ) = GWAccount.PathToArchive
 End If

 If (InStr(GWAccount.PathToArchive, "C:\")) and NOT(InStr(GWAccount.PathToArchive, "C:\TEMP\ARCHIVER")) Then ' checking if the previous Archive Location was on C:\{Path}
 msgbox "The Previous GroupWise Archive Path was:" & chr(34) & GWAccount.PathToArchive & chr(34) & _
 " which appears to be on the local Workstation. You will need to Archive the Workstation to get any previous GroupWise archive files if there. "
 End If

 wscript.echo "Setting GroupWise ArchivePath to desired location Path: " & sGWArchivePath
  GWAccount.SetArchiveTo(sGWArchivePath)

 iTotalMsgCount = 0 ' variable to hold Total number of messsages to be archived
  wscript.echo "Counting Mail Messages to be Archived, this might take a minute..."
 'Counting the number of messages to be archived, [.]Allmessages property doesn't support the Count Property
 For each oMsg in GWAccount.AllMessages
  'wscript.echo oMsg.ClassName
  If (InStr(oMsg.ClassName,"GW.MESSAGE.MAIL"))  And (oMsg.Deleted <> True) Then
  iTotalMsgCount = iTotalMsgCount + 1
  End If
  Next
 
  wscript.echo "Total Mail Count (Not Deleted, or Archived) is:" & iTotalMsgCount
  iCount = 0
If Not(iTotalMsgCount = 0) Then ' There are actually some messages to Archive
  '------------Begin section to create HTML progress window for Archive % completed----------------------
  iPercentComplete = 0
   Set objExplorer = CreateObject("InternetExplorer.Application") 'instancing COM object to launch IE
 
  'setting up IE properties
  objExplorer.Navigate "about:blank"  
  objExplorer.ToolBar = 0
  objExplorer.StatusBar = 0
  objExplorer.Left = 150
  objExplorer.Top = 200
  objExplorer.Width = 355
  objExplorer.Height = 150
  objExplorer.Visible = 1            
  objExplorer.Document.Body.Style.Cursor = "wait"
  objExplorer.Document.Title = "GroupWise Email Archive progress"
  objExplorer.Document.Body.InnerHTML = "Archive progress: <a id='txtProgress'>0</a> % complete" _
   & "<br/><div style='background-color:blue;width:5px;height:10px;' id='progbar'></div>"
 ' ------------------------------------------------End Section----------------------------------------------
 '----------------------------------------------------------------------------------------------------------
 '-Begin Section to Iterate through each Mail message that isn't Deleted or already Archived and Archive it-
  For each oMsg in GWAccount.AllMessages ' checking each message in allmessages collection
  'Updating the IE Progress Bar
  objExplorer.document.getElementById("txtProgress").innerText=iPercentComplete
  objExplorer.document.getElementById("progbar").style.width=iPercentComplete*3
 
 
  ' Checking if the object is 'Mail', not deleted (trash)
  If (InStr(oMsg.ClassName,"GW.MESSAGE.MAIL"))  And (oMsg.Deleted <> True) Then
   wscript.echo "Message Subject: " & oMsg.Subject
   wscript.echo "Message Type: " & oMsg.ClassName
   wscript.echo "Message ID: " & oMsg.MessageID
   
   If oMsg.Archived = False Then ' Not already Archived
   wscript.echo "Archiving Message..."
   oMsg.Archived = True ' Sending the Message to the Archive
   End If
  
   'wscript.echo "Is the messaged archived?:" & oMsg.Archived
   'wscript.echo "Is the messaged deleted?:" & oMsg.Deleted
   iCount = iCount + 1 ' Counting how many done so far
   iPercentComplete =  Round((iCount * 100) / iTotalMsgCount) ' updating %
  End If
 
  If iCount = iTotalMsgCount Then Exit For 'Needed, without this, Wscript throws an error for some reason
  Next
' ------------------------------------------------End Section----------------------------------------------
'----------------------------------------------------------------------------------------------------------
Else
wscript.echo "There are no GroupWise Messages to archive for user:" & sGWUserName
Dim gwFolders
Set gWFolders = GWAccount.AllFolders
For each Folder in gwFolders
'Folder.name
Next
Wscript.quit(80)  'Exit Code 80 means, no messages available to Archive
End If

' Cleaning up resources and exiting script
 objExplorer.Quit ' closing IE

 set fso = Nothing
 set wshSystemEnv = Nothing
 set wshShell = Nothing
 set objExplorer = Nothing
 set GwApp = Nothing
 set GWAccount = Nothing
 WScript.Sleep 1000
 wscript.quit(0)  ' Exiting with no Error Code, all was successful
 '----------------------------------------------------------------------------------------------------------

No comments:

Post a Comment