The following script will backup files from destination location to some other folder and copy files from source location to destination. The script accepts following input parameters
1. Destination location
2. Backup location
Note: I have hard coded source location for convenience, however you can pass as a parameter
Steps involved
1. Save following script as psCopyFiles.ps1
#-----------------------------------------------------------------------------
# Name: psCopyFiles.ps1
# Description: This script will files from source location to destination
# location and backup files to another directory
#
# Usage: Run the script by passing the paramter SourceLocation,
# DestinationLocation & BackupLocation
# Created By: Vamsi Mohan Mitta
# Date: 01/07/2014
#-----------------------------------------------------------------------------
Param([Parameter(Mandatory=$true)]
[String]
$DestinationLocation,
[Parameter(Mandatory=$true)]
[String]
$BackupLocation
)
if ((gsnp Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null){
asnp Microsoft.SharePoint.Powershell
}
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$currentPhysicalPath = Get-ScriptDirectory
$logfile=$currentPhysicalPath + "\log.log"
Start-Transcript $logfile
#$backupLocation = "\\WIN-K6KO9R0T2LC\C$\Backup"
#$backupFolder = $BackupLocation + "Backup"
if ((Test-Path $BackupLocation) -ne $True)
{
New-Item $BackupLocation -type directory
write-host “Successfully created folder!” -ForegroundColor Green
}
else
{
write-host “Folder already exits” -ForegroundColor Blue
}
#Get current date
#$currentDate = (get-date).tostring("mm_dd_yyyy-hh_mm_s")
$currentDate = Get-Date -format MM-dd-yyyy
if($currentDate -ne $null)
{
$newFolder = $BackupLocation + $currentDate
if ((Test-Path $newFolder) -ne $True)
{
New-Item $newFolder -type directory
write-host “Successfully created backup folder-” $newFolder -ForegroundColor Green
}
if($newFolder -ne $null)
{
$removeBackupFolder = $newFolder + "\*"
Remove-Item $removeBackupFolder -Recurse
Copy-Item $DestinationLocation -Destination $newFolder -Recurse
write-host “Successfully copied files to backup folder-” $newFolder -ForegroundColor Green
$removeFolder = $DestinationLocation + "*"
Remove-Item $removeFolder -Recurse
write-host “Successfully removed files from folder--” $DestinationLocation -ForegroundColor Green
#copy files from source location to destination
$sLocation = $currentPhysicalPath + "\webapp.com\*"
Copy-Item $sLocation -Destination $DestinationLocation -Recurse
write-host “Successfully copied files & folders to -” $DestinationLocation -ForegroundColor Green
}
}
Stop-Transcript
Echo Finish
Step2: Create batch file to run the script. Save file in .bat format.
cd /d %~dp0
powershell -file ./psCopyFiles.ps1 -DestinationLocation "\\WIN-K6KO9R0T2LC\C$\ecwebapp.com\" -BackupLocation "\\WIN-K6KO9R0T2LC\C$\Backup\"
pause
Happy powershell coding... please feel free to comment...
1. Destination location
2. Backup location
Note: I have hard coded source location for convenience, however you can pass as a parameter
Steps involved
1. Save following script as psCopyFiles.ps1
#-----------------------------------------------------------------------------
# Name: psCopyFiles.ps1
# Description: This script will files from source location to destination
# location and backup files to another directory
#
# Usage: Run the script by passing the paramter SourceLocation,
# DestinationLocation & BackupLocation
# Created By: Vamsi Mohan Mitta
# Date: 01/07/2014
#-----------------------------------------------------------------------------
Param([Parameter(Mandatory=$true)]
[String]
$DestinationLocation,
[Parameter(Mandatory=$true)]
[String]
$BackupLocation
)
if ((gsnp Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null){
asnp Microsoft.SharePoint.Powershell
}
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$currentPhysicalPath = Get-ScriptDirectory
$logfile=$currentPhysicalPath + "\log.log"
Start-Transcript $logfile
#$backupLocation = "\\WIN-K6KO9R0T2LC\C$\Backup"
#$backupFolder = $BackupLocation + "Backup"
if ((Test-Path $BackupLocation) -ne $True)
{
New-Item $BackupLocation -type directory
write-host “Successfully created folder!” -ForegroundColor Green
}
else
{
write-host “Folder already exits” -ForegroundColor Blue
}
#Get current date
#$currentDate = (get-date).tostring("mm_dd_yyyy-hh_mm_s")
$currentDate = Get-Date -format MM-dd-yyyy
if($currentDate -ne $null)
{
$newFolder = $BackupLocation + $currentDate
if ((Test-Path $newFolder) -ne $True)
{
New-Item $newFolder -type directory
write-host “Successfully created backup folder-” $newFolder -ForegroundColor Green
}
if($newFolder -ne $null)
{
$removeBackupFolder = $newFolder + "\*"
Remove-Item $removeBackupFolder -Recurse
Copy-Item $DestinationLocation -Destination $newFolder -Recurse
write-host “Successfully copied files to backup folder-” $newFolder -ForegroundColor Green
$removeFolder = $DestinationLocation + "*"
Remove-Item $removeFolder -Recurse
write-host “Successfully removed files from folder--” $DestinationLocation -ForegroundColor Green
#copy files from source location to destination
$sLocation = $currentPhysicalPath + "\webapp.com\*"
Copy-Item $sLocation -Destination $DestinationLocation -Recurse
write-host “Successfully copied files & folders to -” $DestinationLocation -ForegroundColor Green
}
}
Stop-Transcript
Echo Finish
Step2: Create batch file to run the script. Save file in .bat format.
cd /d %~dp0
powershell -file ./psCopyFiles.ps1 -DestinationLocation "\\WIN-K6KO9R0T2LC\C$\ecwebapp.com\" -BackupLocation "\\WIN-K6KO9R0T2LC\C$\Backup\"
pause
Happy powershell coding... please feel free to comment...
No comments:
Post a Comment