Solved

Create ScheduleTasks for SystemStateData backup on remote servers

Posted on 2011-09-22
9
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert from shell script to powershell 15 41
WPF and Powershell button hover change text 5 57
Powercli validate module 6 39
78 files, need to delete row 2 in every file 3 27
Utilizing an array to gracefully append to a list of EmailAddresses
This article will help you understand what HashTables are and how to use them in PowerShell.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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