The last script is a powershell one and imports the PST in exchange mailbox based if the PST has the name of SamACCountName.
PS with exchange
you cannot have an import PST folder with multiple PST of the same user, so you have to dispatch them !!
on error resume next
Set WShell = CreateObject("WScript.Shell")
Set WSHnetwork = CreateObject("WScript.Network")
Set oFSO = CreateObject("scripting.filesystemobject")
Set oLogFile = OFSO.CreateTextFile("c:\IMPORT3\log.txt")
Dim strcomputer
strcomputer = lcase(WshNetwork.computername)
'############# RECUP PST #############
ologfile.writeline "***SCRIPT START***"
ologfile.writeline date & " " & time
Set Conn = Wscript.CreateObject("ADODB.Connection")
Conn.ConnectionTimeout = 10
Conn.CommandTimeout = 10
err.Clear
Conn.Open "Provider=SQLOLEDB;Data Source=SRVINFO\SQLEXPRESS,1433;Trusted_Connection=yes;Initial Catalog=PST_stats;APP=PST Stats"
ologfile.writeline "Connection opened, error=" & err.number
Set Command = Wscript.CreateObject("ADODB.Command")
Set Command.ActiveConnection = Conn
Command.commandtext = "SELECT * FROM TablePST WHERE skip IS NULL ORDER BY username,path"
'Command.commandtext = "SELECT * FROM TablePST WHERE username = 'user_to_skip' AND skip IS NULL ORDER BY username,path"
err.clear
Set rst = Command.execute
ologfile.writeline "Command executed, error=" & err.number
Dim previoususer
previoususer = ""
Dim i
i = 0
While Not(rst.EOF)
user = rst("username")
computer = rst("computer")
path = rst("path")
size = rst("size")
ologfile.Writeline vbcrlf
ologfile.writeline "**found record:**"
ologfile.writeline date & " " & time
ologfile.Writeline "User=" & user
ologfile.Writeline "computer=" & computer
ologfile.Writeline "path=" & path
ologfile.Writeline "size=" & size
ologfile.Writeline "Comparing user and previoususer user=" & user & " previoususer=" & previoususer
If user = previoususer then
i = i + 1
Else
i = 0
End if
uncpath = "\\" & computer & "\" & Replace(path,":","$")
pos = Instrrev(uncpath,"\")
filename = mid(uncpath, pos + 1, len(uncpath))
uncpath = left(uncpath, pos - 1)
destpath = "C:\IMPORT3\PST" & cstr(i) & "\" & user
ologfile.Writeline "uncpath=" & uncpath
ologfile.writeline "destpath=" & destpath
ologfile.Writeline "filename=" & filename
err.clear
if not OFSO.FolderExists("C:\IMPORT3\PST" & cstr(i) & "\" & user) then
OFSO.CreateFolder "C:\IMPORT3\PST" & cstr(i)
OFSO.CreateFolder "C:\IMPORT3\PST" & cstr(i) & "\" & user
End if
ologfile.writeline "created folder c:\IMPORT3\PST" & cstr(i) & "\" & user & " with error:" & err.number
Wshell.run "robocopy.exe " & chr(34) & uncpath & chr(34) & " " & chr(34) & destpath & chr(34) & " " & filename & " /NP /R:0 /W:0 /LOG+:C:\IMPORT3\" & user & cstr(i) & ".txt", 0, false
ologfile.writeline "ran command ROBOCOPY"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
wscript.sleep 2000
err.clear
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process where Name='robocopy.exe'")
numofprocesses = colProcess.count
ologfile.writeline "Number of ROBOCOPY processes=" & numofprocesses & " with error:" & err.number & " " & err.description
do while numofprocesses > 9
wscript.sleep 2000
err.clear
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process where Name='robocopy.exe'")
numofprocesses = colProcess.count
ologfile.writeline "Too many ROBOCOPY processes, sleeping 2000 msec=" & numofprocesses & " with error:" & err.number
loop
previoususer = user
rst.MoveNext
ologfile.Writeline "Moving to next record"
Wend
ologfile.writeline "***SCRIPT STOP***"
ologfile.writeline date & " " & time
rst.close
Conn.close
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process where Name='robocopy.exe'")
numofprocesses = colProcess.count
do while numofprocesses > 0
wscript.sleep 2000
err.clear
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process where Name='robocopy.exe'")
numofprocesses = colProcess.count
ologfile.writeline "Waiting for ROBOCOPY processes to finish, sleeping 2000 msec=" & numofprocesses & " with error:" & err.number
loop
'#### COPIE FICHIERS ####
Set oFSO = CreateObject("scripting.filesystemobject")
set oRootFolder = OFSO.Getfolder("C:\IMPORT3")
For each oPSTFolder in oRootFolder.subfolders
For each oUserFolder in oPSTFolder.subfolders
UserFolderName = oUserFolder.name
For each oPSTFile in oUserFolder.files
oPSTFile.move oPSTFolder.path & "\" & UserFolderName & ".pst"
Next
Next
For each oUserFolder in oPSTFolder.subfolders
oUserFolder.delete
Next
Next
set oRootFolder = OFSO.Getfolder("C:\IMPORT3")
For each oPSTFolder in oRootFolder.subfolders
ologofile "Running import on:" & oPSTFolder.name
wshell.run "C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe c:\temp\importmbx.ps1 " & oPSTFolder.path, 3 , true
Next
ologfile.close
Anf finally the powershell script to import in the exchange mailboxes !
The scripts are not perfect, so you have suggestions, feel free to comment
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
Dir $args[0] | Import-Mailbox -Confirm:$false -BadItemLimit 10000 -debug -verbose
No comments:
Post a Comment
Fell free to comment : share your opinion and experienced, provide feedback, help me to correct mistakes, and ask for help if you need any !