[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create ScheduleTasks for SystemStateData backup on remote servers

Posted on 2011-09-22
9
Medium Priority
?
403 Views
Last Modified: 2012-08-13
I have a simple batch file [named SSDBackup.bat which will execute System State Data backup of the server and overwrite it. Now I need a Script or Batch file which I can use to copy this batch file to C:\AdminTasks of remote servers and create a schedule task Called "SystemStateData_Backup" to run at 03:30 AM Daily.
0
Comment
Question by:getazhar
  • 5
  • 4
9 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 36581197
Create a list of those servers, called Computers.txt, then run the following
@echo off
for /f %%a in (C:\Computers.txt) do call :process %%a
goto :eof

:process
set srv=%1
if not exist \\%srv%\c$\AdminTasks md \\%srv%\c$\AdminTasks
copy C:\MyBatFile.bat  \\%srv%\c$\AdminTasks\ /y
schtasks /create /ru system /sc daily /st 03:30:00 /tn SystemStateData_Backup /tr "cmd /c C:\AdminTasks\MyBatFile.bat"

Open in new window

0
 

Author Comment

by:getazhar
ID: 36581392
Can you add error handling & output redirecting to check whether script has executed successfully or not ?
0
 

Author Comment

by:getazhar
ID: 36715558
copying part is working fine.. I think there is something needs to be corrected on below line.

schtasks /create /ru system /sc daily /st 03:30:00 /tn SystemStateData_Backup /tr "cmd /c C:\AdminTasks\MyBatFile.bat"

it's creating the task local server from where i am executing this batch file.. I have modified it as below but still not creating the schedule task on remote servers.

schtasks /s %srv% /create /ru system /sc daily /st 03:30:00 /tn SystemStateData_Backup /tr "cmd /c C:\AdminTasks\MyBatFile.bat"
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 67

Expert Comment

by:sirbounty
ID: 36717002
getazhar, try preceeding the %srv% with \\

schtasks /s \\%srv% /create /ru system /sc daily /st 03:30:00 /tn SystemStateData_Backup /tr "cmd /c C:\AdminTasks\MyBatFile.bat"
0
 

Author Comment

by:getazhar
ID: 36717034
I tried.. but failed.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 36717722
Try this version - let me know the output.  If the script pauses, there should be at least some sort of determination presented as to why...
Are you a domain admin on the target devices?
@echo off
for /f %%a in (C:\Computers.txt) do call :process %%a
goto :eof

:process
set srv=%1
echo.  Processing %srv%
if not exist \\%srv%\c$\AdminTasks md \\%srv%\c$\AdminTasks
if exist \\%srv%\c$\AdminTasks (
  echo. AdminTasks exists on %srv%
) else (
  echo. AdminTasks not present on %srv%
  pause
)
copy C:\MyBatFile.bat  \\%srv%\c$\AdminTasks\ /y
if exist \\%srv%\c$\AdminTasks\MyBatFile.Bat (
  echo. MyBatFile.bat created on %srv%
) else (
  echo.  MyBatFile.bat not found on %srv%
  pause
)

schtasks /create /s \\%srv% /ru system /sc daily /st 03:30:00 /tn SystemState_Backup /tr "cmd /c C:\AdminTasks\MyBatFile.bat"
if exist \\%srv%\Admin$\Tasks\SystemState_Backup.job (
  echo.  SystemState Backup scheduled on %srv%
) else (
  echo.  SystemState Backup not scheduled on %srv%
  pause
)

Open in new window

0
 

Author Comment

by:getazhar
ID: 36814960
Works great but only thing pending is Error handling... if you could redirect the Successfully copied file & Created the task to an output.txt would be great.. and of course it should echo the server name where it has failed or successful to copy and to create the schedule task.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 36815403
This should do it.
@echo off
for /f %%a in (C:\Computers.txt) do call :process %%a
goto :eof

:process
set srv=%1
echo.  Processing %srv% >> output.txt
if not exist \\%srv%\c$\AdminTasks md \\%srv%\c$\AdminTasks
if exist \\%srv%\c$\AdminTasks (
  echo. AdminTasks exists on %srv% >> output.txt
) else (
  echo. AdminTasks not present on %srv% >> output.txt
)
copy C:\MyBatFile.bat  \\%srv%\c$\AdminTasks\ /y
if exist \\%srv%\c$\AdminTasks\MyBatFile.Bat (
  echo. MyBatFile.bat created on %srv% >> output.txt
) else (
  echo.  MyBatFile.bat not found on %srv% >> output.txt
)

schtasks /create /s \\%srv% /ru system /sc daily /st 03:30:00 /tn SystemState_Backup /tr "cmd /c C:\AdminTasks\MyBatFile.bat"
if exist \\%srv%\Admin$\Tasks\SystemState_Backup.job (
  echo.  SystemState Backup scheduled on %srv% >>output.txt
) else (
  echo.  SystemState Backup not scheduled on %srv% >> output.txt
)
echo. >> output.txt

Open in new window

0
 

Author Closing Comment

by:getazhar
ID: 37005230
Good Solution..
0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Loops Section Overview

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question