Jump to content

Robocopy simultaneous copy and send mail when ok


Recommended Posts

Hi there,

I want to create a simple script to :

- Copy one file (500Mb) to 25 servers from the same source

- I would to robocopy 5 servers by 5 (to limit the bw)

- I would like to have a reporting for these copy

--> If copy OK for Server1 --> Send mail to me (and same way for the others copy ok)

--> If copy KO for Server1 --> Send mail to me (and same way for the others copy failed)

I started the script below, but for now I have two problems:
1: the email does not work! Error 50 :/
2: I do not know how to segment 5 by 5 copies.
Otherwise, the script seems to work. I know my script is not very clean, I just multiply all actions line by 13 for now. And I would do the same for 25.  I can not do differently!
 
#include <Constants.au3>
#include <File.au3>
#include <Inet.au3>

$robocopy = @ScriptDir & '\robocopy.exe'

$ini = @ScriptDir & '\Robocopy.ini'
$LogFile = @ScriptDir & '\_Robo.log'

If FileExists(@ScriptDir & '\_Robo.log') Then
    FileDelete(@ScriptDir & '\_Robo.log')
EndIf

If FileExists($ini) Then
;Ini file ; credentials settings
$user = IniRead($ini, "cred", "user", "NotFound")
$domain = IniRead($ini, "cred", "domain", "NotFound")
$pwd = IniRead($ini, "cred", "pwd", "NotFound")
;Ini file ; SMTP settings
$smtpsrv = IniRead($ini, "mail", "smtpsrv", "NotFound")
$name = IniRead($ini, "mail", "name", "NotFound")
$mailfrom = IniRead($ini, "mail", "mailfrom", "NotFound")
$mailto = IniRead($ini, "mail", "mailto", "NotFound")
$mailsubject = IniRead($ini, "mail", "mailsubject", "NotFound")
$mailbody = IniRead($ini, "mail", "body", "NotFound")
;Ini file ; Servers settings
$filiale1 = IniRead($ini, "servers", "Filiale1", "NotFound")
$filiale2 = IniRead($ini, "servers", "Filiale2", "NotFound")
$filiale3 = IniRead($ini, "servers", "Filiale2", "NotFound")
$filiale4 = IniRead($ini, "servers", "Filiale2", "NotFound")
$filiale5 = IniRead($ini, "servers", "Filiale2", "NotFound")
$filiale6 = IniRead($ini, "servers", "Filiale2", "NotFound")
$filiale7 = IniRead($ini, "servers", "Filiale2", "NotFound")
$filiale8 = IniRead($ini, "servers", "Filiale2", "NotFound")
$filiale9 = IniRead($ini, "servers", "Filiale2", "NotFound")
$filiale10 = IniRead($ini, "servers", "Filiale2", "NotFound")
$filiale11 = IniRead($ini, "servers", "Filiale2", "NotFound")
$filiale12 = IniRead($ini, "servers", "Filiale2", "NotFound")
$filiale13 = IniRead($ini, "servers", "Filiale2", "NotFound")
;Ini file ; Var for Robocopy
$source = IniRead($ini, "Robocopy", "source", "NotFound")
$dest = IniRead($ini, "Robocopy", "dest", "NotFound")
$options = IniRead($ini, "Robocopy", "options", "NotFound")
$logg = '/log:' & @ScriptDir & '\_Robo.log ' & IniRead($ini, "Robocopy", "logg", "NotFound")
$what = IniRead($ini, "Robocopy", "what", "NotFound")
$dest1 = ($filiale1 & $dest)
$dest2 = ($filiale2 & $dest)
$dest3 = ($filiale3 & $dest)
$dest4 = ($filiale4 & $dest)
$dest5 = ($filiale5 & $dest)
$dest6 = ($filiale6 & $dest)
$dest7 = ($filiale7 & $dest)
$dest8 = ($filiale8 & $dest)
$dest9 = ($filiale9 & $dest)
$dest10 = ($filiale10 & $dest)
$dest11 = ($filiale11 & $dest)
$dest12 = ($filiale12 & $dest)
$dest13 = ($filiale13 & $dest)
Else
    _FileWriteLog($LogFile, $ini & " not found!! ")
    Exit
 EndIf

;Job Robocopy
$runner = ' "' & $source & '" "' & $dest1 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner2 = ' "' & $source & '" "' & $dest2 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner3 = ' "' & $source & '" "' & $dest3 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner4 = ' "' & $source & '" "' & $dest4 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner5 = ' "' & $source & '" "' & $dest5 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner6 = ' "' & $source & '" "' & $dest6 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner7 = ' "' & $source & '" "' & $dest7 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner8 = ' "' & $source & '" "' & $dest8 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner9 = ' "' & $source & '" "' & $dest9 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner10 = ' "' & $source & '" "' & $dest10 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner11 = ' "' & $source & '" "' & $dest11 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner12 = ' "' & $source & '" "' & $dest12 & '" ' & $what & ' ' & $options & ' ' & $logg
$runner13 = ' "' & $source & '" "' & $dest13 & '" ' & $what & ' ' & $options & ' ' & $logg

Sleep(50)

RunAs ($user, $domain, $pwd,0, $robocopy & $runner, @ScriptDir, @SW_SHOW)
RunAs ($user, $domain, $pwd,0, $robocopy & $runner2, @ScriptDir, @SW_SHOW)

If @error = 0 Then
 Local $Response =   _INetSmtpMail($smtpsrv, $name, $mailfrom, $mailto, $mailsubject, $mailbody)
Local $err = @error
Else
   Exit
   EndIf
If $Response = 1 Then
    MsgBox(0, "Success!", "Mail sent")
Else
    MsgBox(0, "Error!", "Mail failed with error code " & $err)
EndIf

;RunAs ($user, $domain, $pwd,0, $robocopy & $runner3, @ScriptDir, @SW_SHOW)
;RunAsWait ($user, $domain, $pwd,0, $robocopy & $runner4, @ScriptDir, @SW_SHOW)
;RunAsWait ($user, $domain, $pwd,0, $robocopy & $runner5, @ScriptDir, @SW_SHOW)
;RunAs ($user, $domain, $pwd,0, $robocopy & $runner6, @ScriptDir, @SW_SHOW)
;RunAs ($user, $domain, $pwd,0, $robocopy & $runner7, @ScriptDir, @SW_SHOW)
;RunAs ($user, $domain, $pwd,0, $robocopy & $runner8, @ScriptDir, @SW_SHOW)
;RunAsWait ($user, $domain, $pwd,0, $robocopy & $runner9, @ScriptDir, @SW_SHOW)
;RunAsWait ($user, $domain, $pwd,0, $robocopy & $runner10, @ScriptDir, @SW_SHOW)
;RunAs ($user, $domain, $pwd,0, $robocopy & $runner11, @ScriptDir, @SW_SHOW)
;RunAs ($user, $domain, $pwd,0, $robocopy & $runner12, @ScriptDir, @SW_SHOW)
;RunAs ($user, $domain, $pwd,0, $robocopy & $runner13, @ScriptDir, @SW_SHOW)

NB : The. Ini file works and has no impact for my problems.

 
Do you have an idea for my problems ? :)
Thanks!
Link to comment
Share on other sites

Maybe it's easier to skip the send mail.

So, is it possible to create a log with one line for one copy/one server with the statut ?

##Log Robocopy##

Copy to Server1: OK

Copy to Server2 : OK

...

##End Log Robocopy##

I guess a log file will be especially easy to read than an email !

A way to do that ? :D

Link to comment
Share on other sites

Can you post the ini file? Clean it of anything confidential like usernames/passwords/servers etc., but it would be easier to work with it rather than having to come up with one myself.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Sure, here is the ini file

[mail]
smtpsrv="MySMTP"
name=Me
mailfrom="MyMail"
mailto="MyMail"
mailsubject="[Robocopy] - Copy Server [OK]"
mailbody="Robocopy completed successfully. No error :)"

[servers]
Filiale1=\\Server1
Filiale2=\\Server2
Filiale3=
Filiale4=
Filiale5=
Filiale6=
Filiale7=
Filiale8=
Filiale9=
Filiale10=
Filiale11=
Filiale12=
Filiale13=

[cred]
user=MyUser
pwd=MyPassword
domain=MyDomain

[robocopy]
source="C:\Temp\SourceTestRobocopy"
;dest must start with \
dest=\c$\Temp\DestTestRobocopy
options = /R:15 /W:53 /TBD
logg = /NJH /TS /TEE
what = /Z /SEC /MIR
Link to comment
Share on other sites

Try this as a start, to see if it's working the way you want it to. If you still want the email sent, use >this script instead of _InetSMTPMail, this one allows you to use gmail, yahoo etc.

#include <Constants.au3>
#include <File.au3>
#include <Inet.au3>

$robocopy = @ScriptDir & '\robocopy.exe'

$ini = @ScriptDir & '\Robocopy.ini'
$LogFile = @ScriptDir & '\_Robo.log'

If FileExists(@ScriptDir & '\_Robo.log') Then
    FileDelete(@ScriptDir & '\_Robo.log')
EndIf

If FileExists($ini) Then
    ;Ini file ; credentials settings
    $user = IniRead($ini, "cred", "user", "NotFound")
    $domain = IniRead($ini, "cred", "domain", "NotFound")
    $pwd = IniRead($ini, "cred", "pwd", "NotFound")
    ;Ini file ; SMTP settings
    $smtpsrv = IniRead($ini, "mail", "smtpsrv", "NotFound")
    $name = IniRead($ini, "mail", "name", "NotFound")
    $mailfrom = IniRead($ini, "mail", "mailfrom", "NotFound")
    $mailto = IniRead($ini, "mail", "mailto", "NotFound")
    $mailsubject = IniRead($ini, "mail", "mailsubject", "NotFound")
    $mailbody = IniRead($ini, "mail", "body", "NotFound")
    ;Ini file ; Servers settings
    Global $filiale = IniReadSection($ini, "servers")
    ;Ini file ; Var for Robocopy
    $source = IniRead($ini, "Robocopy", "source", "NotFound")
    $dest = IniRead($ini, "Robocopy", "dest", "NotFound")
    $options = IniRead($ini, "Robocopy", "options", "NotFound")
    $logg = '/log:' & @ScriptDir & '\_Robo.log ' & IniRead($ini, "Robocopy", "logg", "NotFound")
    $what = IniRead($ini, "Robocopy", "what", "NotFound")
Else
    _FileWriteLog($LogFile, $ini & " not found!! ")
    Exit
EndIf
$logg = '/log:' & @ScriptDir & '\_Robo.log ' & IniRead($ini, "Robocopy", "logg", "NotFound")

;Job Robocopy
Global $aRunner[14] = ["13"]
For $I = 1 To $aRunner[0]
    $aRunner[$I] = '"' & $robocopy & '" "' & $source & '" "' & $filiale[$I][1] & $dest & '" ' & $what & ' ' & $options & ' ' & $logg
Next

For $I = 1 To $aRunner[0]
    RunAs($user, $domain, $pwd, 0, $aRunner, @ScriptDir, @SW_SHOW)
Next
;~ If @error = 0 Then
;~  Local $Response = _INetSmtpMail($smtpsrv, $name, $mailfrom, $mailto, $mailsubject, $mailbody)
;~  Local $err = @error
;~ Else
;~  Exit
;~ EndIf
;~ If $Response = 1 Then
;~  MsgBox(0, "Success!", "Mail sent")
;~ Else
;~  MsgBox(0, "Error!", "Mail failed with error code " & $err)
;~ EndIf

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I just tested, it does not work: even copying "locally" does not work.
I look! There is no visible message or log created ...
 
EDIT : An idea to "split" the copy 5 servers by 5 servers ?
Edited by DraGula
Link to comment
Share on other sites

Where does it fail? What values do your variables hold when you run the script and what is the error message if any?

 

EDIT: Just realized I made a mistake in the RunAs line, change $aRunner to $aRunner[$i]

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Oh yes, you rocks !

This is almost perfect. For simplicity, I will make runaswait as runas...
 
However, you think it is possible to log a "success" for each copy on each server?
Log like :
Copy to Server1: OK
Copy to Server2: OK
...
?
 
Thanks !
Link to comment
Share on other sites

Change the second for loop to this to have it write to your _Robo.log file.

For $I = 1 To $aRunner[0]
    $ErrorCode = RunAsWait($user, $domain, $pwd, 0, $aRunner[$I], @ScriptDir, @SW_SHOW)
    If $ErrorCode = 0 Then
        _FileWriteLog(@ScriptDir & '\_Robo.log', "Copy to " & $filiale[$I] & ": OK")
    Else
        _FileWriteLog(@ScriptDir & '\_Robo.log', "Copy to " & $filiale[$I] & ": FAILED")
    EndIf
Next

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You are my hero !

I share the new script for information :

#include <Constants.au3>
#include <File.au3>
#include <Inet.au3>

$robocopy = @ScriptDir & '\robocopy.exe'

$ini = @ScriptDir & '\Robocopy.ini'
$LogFile = @ScriptDir & '\_Robo.log'
$Pinglog= @ScriptDir & '\_Ping.log'

If FileExists(@ScriptDir & '\_Robo.log') Then
    FileDelete(@ScriptDir & '\_Robo.log')
 EndIf
 If FileExists(@ScriptDir & '\_Ping.log') Then
    FileDelete(@ScriptDir & '\_Ping.log')
EndIf

If FileExists($ini) Then
    ;Ini file ; credentials settings
    $user = IniRead($ini, "cred", "user", "NotFound")
    $domain = IniRead($ini, "cred", "domain", "NotFound")
    $pwd = IniRead($ini, "cred", "pwd", "NotFound")
    ;Ini file ; Servers settings
    Global $filiale = IniReadSection($ini, "servers")
    ;Ini file ; Var for Robocopy
    $source = IniRead($ini, "Robocopy", "source", "NotFound")
    $dest = IniRead($ini, "Robocopy", "dest", "NotFound")
    $options = IniRead($ini, "Robocopy", "options", "NotFound")
    $logg = '/log+:' & @ScriptDir & '\_Robo.log ' & IniRead($ini, "Robocopy", "logg", "NotFound")
    $what = IniRead($ini, "Robocopy", "what", "NotFound")
Else
    _FileWriteLog($LogFile, $ini & " not found!! ")
    Exit
 EndIf

 ;Check if Computer is online
 Global $computer[14] = ["13"]
For $I = 1 To $computer[0]
    $computer[$I] = $filiale[$I][1]
 Next
For $I = 1 To $Computer[0]
    $aPing = Ping ($Computer[$i],40)
   If $aPing Then
   FileWrite($Pinglog,"OnLine : " & $Computer[$i] &@CRLF)
Else
    FileWrite($Pinglog,"Offline : " & $Computer[$i] &@CRLF)
EndIf
Next

;Job Robocopy
Global $aRunner[14] = ["13"]
For $I = 1 To $aRunner[0]
    $aRunner[$I] = '"' & $robocopy & '" "' & $source & '" "' & "\\" & $filiale[$I][1] & $dest & '" ' & $what & ' ' & $options ;& ' ' & $logg
Next

 For $I = 1 To $aRunner[0]
    $ErrorCode = RunAsWait($user, $domain, $pwd, 0, $aRunner[$i], @ScriptDir, @SW_HIDE)
    If $ErrorCode = 0 Then
        _FileWriteLog(@ScriptDir & '\_Robo.log', "Copy to " & $filiale[$I][1] & ": OK")
    Else
        _FileWriteLog(@ScriptDir & '\_Robo.log', "Copy to " & $filiale[$I][1] & ": FAILED")
    EndIf
Next

For now, I think it's perfect for my need :)

Thank you again !

Link to comment
Share on other sites

My bad, I assumed (and we all knows what happens when we do that) that an exitcode of 0 indicated success for RoboCopy. Unfortunately an exitcode of 0 means nothing was copied, but there were no errors. An exitcode of 1 means it copied files and it did it successfully. So make this change.
 

;change this
 If $ErrorCode = 0 Then
; to this
 If $ErrorCode >= 0 And $ErrorCode < 2 Then

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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...