Solved

Create ScheduleTasks for SystemStateData backup on remote servers

Posted on 2011-09-22
9
391 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now