' script to create aan Input File for Access Manager Batch Maintenance
'usage:  AccMan_Maintenance.vbs users.csv

' this code is for examplary purposes only
' it is provided as is with no guarantee of any kind

Option Explicit
On Error Resume Next

Const AccManFile = "d:\AccMan_automation\accman_input.txt"
Const strAccManPathName = "D:\Program Files\cognos\cer5\bin\AccessAdmMaint.exe"
Const AccManBatchFile = "d:\AccMan_automation\accmanbatch.bat"
Const AccManWorkDir = "d:\AccMan_automation\"

Call Main()

Sub Main()
	Dim objFSO, objFile, strReadLine
    Dim strInputFile, strReadLineSplit
	Dim arUsers()
	Dim intLine
	
	strInputFile = WScript.Arguments.Item(0)
	Set objFSO  = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(strInputFile, 1)
	intLine=0
	While Not objFile.AtEndOfStream
		strReadLine = Trim(objFile.ReadLine)
				If strReadLine <> "" And InStr(strReadLine, ",") > 0 Then
					ReDim Preserve arUsers(3, intLine)
					strReadLineSplit = Split(strReadLine, ",")
					arUsers(0,intLine) = strReadLineSplit(0)
					arUsers(1,intLine) = strReadLineSplit(1)
					arusers(2,intLine) = strReadLineSplit(2)
				End If
			intLine = intLine+1
	WEnd
	WriteAccManFile arUsers
	CreateBatchFile
	RunAccMan
	SendMsg
 End Sub
 
Sub WriteAccManFile (arUsers)
	CreateAccmanFile
	Dim objFSO, objFile
	Dim iArraySize, k
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(AccManFile, 2)
	iArraySize = UBound(arUsers,2)
	k=1
	while k <= iArraySize
		objFile.WriteLine ("AddUser," & arUsers(0,k))
		objFile.WriteLine ("AddUserClass," & arUsers(1,k) & ",Root User Class")
		objFile.WriteLine ("AddUserToUserClass," & arUsers(0,k) & "," & arUsers(1,k))
		objFile.WriteLine ("AddUserBasicSignon," & arUsers(0,k) & "," & arUsers(2,k))
		k=k+1
	wend

End Sub 

Sub CreateAccmanFile
	Dim objFSO, objFile
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.CreateTextFile(AccManFile)
End Sub

Sub CreateBatchFile
	Dim strAccManCommand
	strAccManCommand = Chr(34) & strAccManPathName & CHr(34)
	strAccManCommand = strAccManCommand & " -namespace=default -uid=Administrator -pass="&Chr(34)&Chr(34)&" -filetype=2 -filename="
	strAccManCommand = strAccManCommand & Chr(34) & AccManFile & Chr(34)

	Dim objFSO, objFile
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.CreateTextFile(AccManBatchFile)
	Set objFile = nothing
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(AccManBatchFile, 2)
	objFile.WriteLine (strAccManCommand)
End Sub

Sub RunAccMan
	Dim objShell
    set objShell = WScript.CreateObject("WScript.Shell")
	objShell.Run AccManBatchFile,1,True
End Sub

Sub SendMsg
	Dim objOutlook, objOutlookEmail, objOutlookAttachments
	Dim strToday, strSubject, strFileName1, strFileName2, strTo, strBody
	strToday = date
	strFileName1 = AccManWorkDir & "log.txt"
	strFileName2 = AccManWorkDir & "errorlog.txt"
	strTo = "whoevercares@yourcompany.com"
	strSubject = "Access Manager Batch Maintenance Completed " & strToday
	strBody = "Log File Attached." & Chr(13)
	Outlook strSubject, strTo, strBody, strFileName1, strFileName2
End Sub

Sub Outlook(strSubject, strTo, strBody, strFileName1, strFileName2)
	Dim objOutlook, objOutlookEmail, objOutlookAttachments,OlMailItem,ObjFSO
	Set objOutlook = CreateObject("Outlook.Application")
	Set objOutlookEmail = objOutlook.CreateItem(OlMailItem)
	objOutlookEmail.Subject = strSubject
	objOutlookEmail.Body = strBody
	objOutlookEmail.To = strTo
	Set objOutlookAttachments = objOutlookEmail.Attachments
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	if objFSO.FileExists(strFileName1) then
		objOutlookAttachments.Add strFileName1
	end if
	if objFSO.FileExists(strFileName2) then
	objOutlookAttachments.Add strFileName2
	end if 
	objOutlookEmail.Save
	' the following two lines are useful, but have been commented out for this example
	'objOutlookEmail.Display
	'objOutlookEmail.Send
End Sub