Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to back up PST files from all computers to Server 2

Status
Not open for further replies.

bence8810

IS-IT--Management
Jul 20, 2005
241
AE
Dear Script Gurus,

Please note that I am a complete beginner with Scripting, I kind of dont even know what scripts are :) Having said that, I am in need to put all PST files from users' computers to a server which will be backed up as one.

I have a "script" (DOS batch file) which copies the PST file to the server upon boot (Using GPO and logon script). The problem is that while the PST file is being copied, the computer still boots up and shows the desktop, so the user can launch Outlook, and the Copy all of a sudden fails.

How would I go about a very simple script that wouldnt allow them to Open Outlook, or just to Note them not to open until it finishes. I basically need to run this script for every user once a week, maybe 7 users a day. We have 35 users.

The exchange server is exhausted with the amount of Email everyone has, so I needed to start archiving to local PST folders, but those are not backed up yet.

Thanks for any pointers, and I am open to new ideas of how to backup as well.

Thanks,

Ben
 
Hi MrMovie,

Thanks a bunch. Script works like a charm with the modifications you indicated.

Where should I plug the Wscript.Echo in, and how exactly?

Now I would like to implement the Group query thing, that both you and Markdmac suggested. I checked his FAQ for some pointers, but I am still in the dark as to how I should go implementing that. Could you give me some pointers?

Thanks a lot for your help,

Ben
 
i take it you will want a different group for each day of the week??? the logic in that case should be left to you i guess.

there are (and not limited to) 2 angles of attack.
grab all the group objects and check membership against burrent user of grab the current user and check all its membership, the later should be quicker as you will only need to bind to one AD object whereas with the group one you might need to bind to lots of groups....thats not strictly the case but now i am confusing myself


strDay = CStr(Weekday(Now()))
Set objUser = GetObject("WinNT://domainanme/" & strUserName & ",user")
For Each aGroup In objUSer.Groups
If LCase(Left(aGroup.Name, 11)) = "olookcopy_" & strDay Then
Wscript.Echo "we are in the group which needs to update today"
End If
Next
Set objUser = Nothing

'this would work if your groupnames in AD naming convention was

olookcopy_2
olookcopy_n

etc etc

you can of course change the naming convention to anything you want, or utilise a different group property other than .Name, .Description etc might be more to your liken,,,i am sure you get the drift
 
Hi

Thanks. So basically I would do something like this?

Code:
Set WshShell = CreateObject("Wscript.Shell")
strUserName = WshShell.ExpandEnvironmentStrings("%username%")

strDay = CStr(Weekday(Now()))
Set objUser = GetObject("WinNT://domainname/" & strUserName & ",user")
For Each aGroup In objUSer.Groups
   If LCase(Left(aGroup.Name, 11)) = "pstbckp_" & strDay Then
      Wscript.Echo "we are in the group which needs to update today"
   Set WshShell = CreateObject("Wscript.Shell")
      On Error Resume Next
      strTemp = WshShell.RegRead("HKLM\Software\Synovate\PSTCopyFlag")
      On Error Goto 0
      If CStr(strTemp) <> CStr(Date) Then
         Set objFSO = CreateObject("Scripting.FileSystemObject")
         strUserName = WshShell.ExpandEnvironmentStrings("%username%")
         objFSO.CopyFile "D:\Mail\*.pst" , "\\bckupsrv1\PST-Backup\" & strUserName & "\"
      On Error Resume Next
     objFSO.CopyFile "C:\Mail\*.pst" , "\\bckupsrv1\PST-Backup\" & strUserName & "\"
      On Error Resume Next
     objFSO.CopyFile "C:\Documents and Settings\" & strUserName & "\Local Settings\Application Data\Microsoft\Outlook\*.pst" , "\\bckupsrv1\PST-Backup\" & strUserName & "\"
      On Error Resume Next
         Set objFSO = Nothing
         'set the FLAG
         WshShell.RegWrite "HKLM\Software\Synovate\PSTCopyFlag", Date
      End If
End If
Next
Set objUser = Nothing

I tried to make some sense out of it but based on past experience, I would rather post it before letting it lose.

Thanks

Ben
 
If LCase(Left(aGroup.Name, 11)) = "pstbckp_" & strDay Then

'wont work as the length of "pstbckp_" & strDay wont be 11 anymore.....

Set objUser = GetObject("WinNT://domainname/" & strUserName & ",user")

domainname will need to changed, you might get away with
WshShell.ExpandEnvironmentStrings("%userdomain%") but you might want consider getting it from somewhere esle, or you might consider %logonserver%, or WshNetwork etc etc,,,or just hardcode to your netbios domainname
 
Hi

Thanks, at least last time I got some of it right already, except the Doman name and the number part. Now, the script runs like a champ. I changed the Pop up window so that it will only show up if the backup has not yet been completed, so I moved it down a few lines. I think at this point I am done. I am posting the final script below, please check if there are nothing left which is not necessary. I am extremely happy now :)

Thanks

Ben

Code:
Set WshShell = CreateObject("Wscript.Shell")
strUserName = WshShell.ExpandEnvironmentStrings("%username%")

strDay = CStr(Weekday(Now()))
Set objUser = GetObject("WinNT://synovate/" & strUserName & ",user")
For Each aGroup In objUSer.Groups
   If LCase(Left(aGroup.Name, 9)) = "pstbckp_" & strDay Then
         Set WshShell = CreateObject("Wscript.Shell")
      On Error Resume Next
      strTemp = WshShell.RegRead("HKLM\Software\Synovate\PSTCopyFlag")
      On Error Goto 0
      If CStr(strTemp) <> CStr(Date) Then
	Wscript.Echo "we are in the group which needs to update today"
         Set objFSO = CreateObject("Scripting.FileSystemObject")
         strUserName = WshShell.ExpandEnvironmentStrings("%username%")
         objFSO.CopyFile "D:\Mail\*.pst" , "\\bckupsrv1\PST-Backup\" & strUserName & "\"
      On Error Resume Next
     objFSO.CopyFile "C:\Mail\*.pst" , "\\bckupsrv1\PST-Backup\" & strUserName & "\"
      On Error Resume Next
     objFSO.CopyFile "C:\Documents and Settings\" & strUserName & "\Local Settings\Application Data\Microsoft\Outlook\*.pst" , "\\bckupsrv1\PST-Backup\" & strUserName & "\"
      On Error Resume Next
         Set objFSO = Nothing
         'set the FLAG
         WshShell.RegWrite "HKLM\Software\Synovate\PSTCopyFlag", Date
      End If
End If
Next
Set objUser = Nothing
 
Oh, one thing still remains. That popup window is really neat, however it wont do the script until OK is pressed. Is there a way I can change the script that it will press OK automatically, say within 10 seconds?

Thanks

Ben
 
you can use cscript.exe to run the script or you can use a WshShell.Popup which has a timeout parameter for it.

you still need to tidy up your On Error Resume Next's, you dont need to use it more than once to encapsulate your attempted copy jobs...i think
 
Hi

I am not sure I follow you, so for now I commented the nice popup out, as I am in a hurry to make it work by the time tonight people leave, which is right about now. The script works, so I am looking forward seeing a bunch of PST files later.

Next week tuesday I will try to clean things up, the On Resume Next things, and the Popup as well. We have dayoffs til then. We are so spoiled :)

Thanks for helping,

Ben
 
You're probably past this point, but if you wanted to use usernames, you could base it on a character in the name rather than specific users. If your usernames start with numerics, you'd want to add those to the cases.

Code:
Select Case Left(varUserName,1)
Case "A","B","C","D","E"
    If WeekDay(Now()) = 2 Then MsgBox "Move PST Files"
Case "F","G","H","I","J"
    If WeekDay(Now()) = 3 Then MsgBox "Move PST Files"
Case "K","L","M","N","O"
    If WeekDay(Now()) = 4 Then MsgBox "Move PST Files"
Case "P","Q","R","S","T"
    If WeekDay(Now()) = 5 Then MsgBox "Move PST Files"
Case "U","V","W","X","Y","Z"
    If WeekDay(Now()) = 6 Then MsgBox "Move PST Files"
End Select
 
Hi Skie,

Thanks, your code looks interesting. I think for now the Group AD lookup will do a good enough job, however I liked the MsgBox command. I gave it a go, and I sadly noted that the Window has to be OK'ed here too, to proceed. I am looking for a solution where the Note would only pop up to give information, and the process would autostart. I dont want Users ignore the message, leave, and let the PC run all weekend for no good reason.

Is there an easy way of implementing this?

Thanks and happy easter everyone..

Ben
 
You can use a for/next to act as a timer then use sendkeys to press the OK

I hope you find this post helpful.

Regards,

Mark
 
Hi

I came in today, and found that almost no PST files were saved on last thursday. I wondered why, and I went to a machine that should have backed up some PSTs. I ran the Script from the machine, and it came up with an error, that PATH not found. I looked at it closer, and it was referring to the line where it wanted to copy the PST files from C:\mail, and that directory was not existant on the machine.

I guess even the messy ON ERROR RESUME codes didnt work. How can I eliminate this? Kind of when the folder or file doesnt exist, it would just skip it, and go to the next entry.

Thanks

Ben
 
If objFSO.FolderExists("C:\Mail") Then objFSO.CopyFile "C:\Mail\" ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PH, that really helped. The script is up and running, tonight is the second test-run. Lets see how it works out for all people.

The last thing I guess would be to get the pop up window working without a need of pressing the OK button. i will work on it next time.

By the way, I used the script from Markdmac's FAQ, I tailored it to my needs, and it works like a dream. I love scripts...

Thanks to all of you,

Ben
 
Syntax
intButton = object.Popup(strText, [natSecondsToWait], [strTitle], [natType])
Parameters
Part Description
object WshShell object.
strText The text to appear in the pop-up window.
natSecondsToWait Optional. The number of seconds to wait before closing the pop-up window.
strTitle Optional. The text to appear in the title of the pop-up window.
natType Optional. The type of buttons and icons to display in the pop-up window.



'something like

WshShell.Popup "hello world", 5

'that should stay resident for 5 seconds then naff off
 
Hi

So here we are, at the end. My script is fully functional. I really appreciate all inputs. Now, i Have the nice popup, I have the selected users by groups, and the feature that the backup only takes place once a day, etc. This is wonderful.

Thanks to all of you who contributed. Fianlly, here is my fully functional script, that takes care of all this.

Ben

Code:
Set WshShell = CreateObject("Wscript.Shell")
strUserName = WshShell.ExpandEnvironmentStrings("%username%")

strDay = CStr(Weekday(Now()))
Set objUser = GetObject("WinNT://synovate/" & strUserName & ",user")
For Each aGroup In objUSer.Groups
   If LCase(Left(aGroup.Name, 9)) = "pstbckp_" & strDay Then
         Set WshShell = CreateObject("Wscript.Shell")
      On Error Resume Next
      strTemp = WshShell.RegRead("HKLM\Software\Synovate\PSTCopyFlag")
      On Error Goto 0
      If CStr(strTemp) <> CStr(Date) Then
	WshShell.Popup "Your emails are being backed up now. You may safely leave the machine, it will proceed 

automatically", 5
         Set objFSO = CreateObject("Scripting.FileSystemObject")
         strUserName = WshShell.ExpandEnvironmentStrings("%username%")
          If objFSO.FolderExists("D:\Mail") Then objFSO.CopyFile "D:\Mail\*.pst" , "\\bckupsrv1\PST-Backup\" & strUserName & "\"
     If objFSO.FolderExists("C:\Mail") Then objFSO.CopyFile "C:\Mail\*.pst" , "\\bckupsrv1\PST-Backup\" & strUserName & "\"
     If objFSO.FolderExists("C:\Documents and Settings\" & strUserName & "\Local Settings\Application 

Data\Microsoft\Outlook") Then objFSO.CopyFile "C:\Documents and Settings\" & strUserName & "\Local Settings\Application 

Data\Microsoft\Outlook\*.pst" , "\\bckupsrv1\PST-Backup\" & strUserName & "\"
         Set objFSO = Nothing
         'set the FLAG
         WshShell.RegWrite "HKLM\Software\Synovate\PSTCopyFlag", Date
      End If
End If
Next
Set objUser = Nothing
 
Hi

Just another problem appeared now. The thing is that now I have the IfFolderExists thing, but that does not seem good enough. At one case, I realized that the Folder was existing, but no PST files under it, so it crashed.

How do I make this criteria not looking for the folder, but actually for the PST file?

Now I have

Code:
If objFSO.FolderExists("D:\Mail") Then

How do I change this to look for a file rather than a folder? This is coming purely out of logic, no background behind it. Would this work?

Code:
If objFSO.FileExists("D:\Mail\*.pst") Then

Thanks

Ben
 
Yes, that is the correct syntax however you can't use a wildcard.

I hope you find this post helpful.

Regards,

Mark
 
Hi

Yes, that is exactly what I just found out. That is bad. I mean I dont know what would be the file's name at all times, so this wont work for me. How can I make this work somehow?

Thanks

Ben
 
Could you set up a scheduled task in Windows to run the .bat file nightly (at 2:00 am for example)? Then the user would not be interupted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top