A simple task - once a week I need to copy certain folder to a NAS device. There are three devices and at any time only one of them is online.

Following Windows PowerShell script will check if host is online and if it is, run the robocopy command. If the host is offline, it will skip it and run the check on the next host.
Robocopy logs will be saved to "D:\My Scripts" folder.

 

 


if (Test-Connection nas1 -quiet){
"NAS1 Online"
robocopy "D:\Archive" "\\nas1\Archive" /MIR /FFT /R:3 /TEE /LOG:"D:\My Scripts\NAS1.log"
}
else{
"NAS1 Offline"
}
if (Test-Connection nas2 -quiet){
"NAS2 Online"
robocopy "D:\Archive" "\\nas2\Archive" /MIR /FFT /R:3 /TEE /LOG:"D:\My Scripts\NAS2.log"
}
else{
"NAS2 Offline"
}
if (Test-Connection nas3 -quiet){
"NAS3 Online"
robocopy "D:\Archive" "\\nas3\Archive" /MIR /FFT /R:3 /TEE /LOG:"D:\My Scripts\NAS3.log"
}
else{
"NAS3 Offline"
}

Command Test-Connection requires PowerShell 2.0. If you still running v1, you download 2.0 from here.

To run above script as a scheduled task, choose following options in Scheduled Task Action tab:

  • Action: Start a program
  • Program/script: PowerShell.exe
  • Add arguments: & 'D:\My Scripts\ScriptFileName.ps1' >> 'D:\My Scripts\Log.txt'
    Where: 
    D:\My Scripts\ScriptFileName.ps1 - path to your PowerShell script
    D:\My Scripts\Log.txt - path to the log file (optional)

Windows SBS Server 2008

No comments

Leave your comment

In reply to Some User
Captcha Image