Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Help modifying a powershell script

$
0
0

Hello,

I have recently been given a task to write/find a script that is capable of performing Full and Incremental backups. I found a script that does exactly what I need, however, it requires user input. I need this to be a scheduled task and therefore I need the input to be a static path. Here is the script I am talking about:

#region Params
param(
[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$true)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
[System.String]
$SourceDir,
[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
[System.String]
$DestDir,
[Parameter(Position=2, Mandatory=$false,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
[System.String]
$HashPath,
[Parameter(Position=3, Mandatory=$false,ValueFromPipeline=$false)]
[ValidateSet("Full","Incremental","Differential")] 
[System.String]
$BackupType="Full",
[Parameter(Position=4, Mandatory=$false,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()]  
[System.String]
$LogFile=".\Backup-Files.log",
[Parameter(Position=5, Mandatory=$false,ValueFromPipeline=$false)]
[System.Management.Automation.SwitchParameter]
$SwitchToFull
)
#endregion 

begin{
function Write-Log
{
#region Params
[CmdletBinding()]
[OutputType([System.String])]
param(
[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Message,
[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$LogFile
)
#endregion
try{
Write-Host $Message
Out-File -InputObject $Message -Append $LogFile
}
catch {throw $_}
}

function Get-Hash 
{
#region Params
[CmdletBinding()]
[OutputType([System.String])]
param(
[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$HashTarget,
[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateSet("File","String")]
[System.String]
$HashType
)
#endregion
begin{
try{ $objGetHashMD5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider } 
catch {throw $_ }
}
process{
try {
#Checking hash target is file or just string
switch($HashType){
"String" {
$objGetHashUtf8 = New-Object -TypeName System.Text.UTF8Encoding
$arrayGetHashHash = $objGetHashMD5.ComputeHash($objGetHashUtf8.GetBytes($HashTarget.ToUpper()))
break
}

"File" {
$arrayGetHashHash = $objGetHashMD5.ComputeHash([System.IO.File]::ReadAllBytes($HashTarget))
break
}
}
#Return hash
Write-Output $([System.Convert]::ToBase64String($arrayGetHashHash))
}
catch { throw $_ }
}
}

function Copy-File
{
#region Params
[CmdletBinding()]
[OutputType([System.String])]
param(
[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Any'})] 
[System.String]
$SourceFile,
[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()] 
[System.String]
$DestFile
)
#endregion
try{
#The script fails when folder being copied to file. So the item will be removed to avoid the error.
if(Test-Path -LiteralPath $DestFile -PathType Any){
Remove-Item -LiteralPath $DestFile -Force -Recurse
}

#Creating destination if doesn't exist. It's required because Copy-Item doesn't create destination folder
if(Test-Path -LiteralPath $SourceFile -PathType Leaf){
New-Item -ItemType "File" -Path $DestFile -Force
}

#Copying file to destination directory
Copy-Item -LiteralPath $SourceFile -Destination $DestFile -Force
}
catch{ throw $_ }
}

function Backup-Files
{
#region Params
[CmdletBinding()]
[OutputType([System.String])]
param(
[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
[System.String]
$SourceDir,
[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()] 
[System.String]
$DestDir,
[Parameter(Position=2, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateNotNull()] 
[System.Collections.Hashtable]
$HashTable
)
#endregion
try{
$xmlBackupFilesHashFile = $HashTable

Write-Host "Backup started" 

Get-ChildItem -Recurse -Path $SourceDir|ForEach-Object{
$currentBackupFilesItem = $_

#Full path to source and destination item
$strBackupFilesSourceFullPath = $currentBackupFilesItem.FullName
$strBackupFilesDestFullPath = $currentBackupFilesItem.FullName.Replace($SourceDir,$DestDir)

#Checking that the current item is file and not directory. True - the item is file. 
$bBackupFilesFile = $($($currentBackupFilesItem.Attributes -band [System.IO.FileAttributes]::Directory) -ne [System.IO.FileAttributes]::Directory)

Write-Host -NoNewline ">>>Processing item $strBackupFilesSourceFullPath..."

#Generating path hash
$hashBackupFilesPath = $(Get-Hash -HashTarget $strBackupFilesSourceFullPath -HashType "String")
$hashBackupFilesFile = "d"

#If the item is file then generate hash for file content
if($bBackupFilesFile){
$hashBackupFilesFile = $(Get-Hash -HashTarget $strBackupFilesSourceFullPath -HashType "File")
}

#Checking that the file has been copied
if($xmlBackupFilesHashFile[$hashBackupFilesPath] -ne $hashBackupFilesFile){

Write-Host -NoNewline $("hash changed=>$hashBackupFilesFile...")

Copy-File -SourceFile $strBackupFilesSourceFullPath $strBackupFilesDestFullPath|Out-Null

#Returning result
Write-Output @{$hashBackupFilesPath=$hashBackupFilesFile}
}
else{
Write-Host -NoNewline "not changed..."
}

Write-Host "done"
}
Write-Host "Backup completed"
}
catch { throw $_ }
}

function Backup-Full
{
[CmdletBinding()]
[OutputType([System.String])]
#region Params
param(
[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
[System.String]
$SourceDir,
[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()] 
[System.String]
$DestDir,
[Parameter(Position=2, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()] 
[System.String]
$HashFile,
[Parameter(Position=3, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()]  
[System.String]
$ChainKey
)
#endregion

try{
#Creating an empty hash table
$xmlBackupFullHashFile = @{}

#Starting directory lookup 
$uintBackupFullCount = 0
Backup-Files -SourceDir $SourceDir -DestDir $("$DestDir\$ChainKey\Full_" + $(Get-Date -Format "ddMMyyyy")) -HashTable $xmlBackupFullHashFile|`
ForEach-Object{ 
$xmlBackupFullHashFile.Add([string]$_.Keys,[string]$_.Values) 
$uintBackupFullCount++
}

#Saving chain key.
$xmlBackupFullHashFile.Add("ChainKey",$ChainKey)

Write-Host -NoNewline "Saving XML file to $HashFile..."

Export-Clixml -Path $HashFile -InputObject $xmlBackupFullHashFile -Force
Write-Host "done"

Write-Output $uintBackupFullCount
}
catch { throw $_ }
}

function Backup-Diff
{
#region Params
[CmdletBinding()]
[OutputType([System.String])]
param(
[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
[System.String]
$SourceDir,
[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()] 
[System.String]
$DestDir,
[Parameter(Position=2, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'leaf'})]
[System.String]
$HashFile
)
#endregion
try{
#Loading hash table
$xmlBackupDiffHashFile = Import-Clixml $HashFile
$chainKeyBackupDiffDifferential = $xmlBackupDiffHashFile["ChainKey"]

$uintBackupDiffCount = 0

#Starting directory lookup 
Backup-Files -SourceDir $SourceDir -DestDir $("$DestDir\$chainKeyBackupDiffDifferential\Differential_" + $(Get-Date -Format "ddMMyyyy.HHmm")) -HashTable $xmlBackupDiffHashFile|`
ForEach-Object{ $uintBackupDiffCount++ }

Write-Output $uintBackupDiffCount
}
catch { throw $_ }
}

function Backup-Inc
{
#region Params
[CmdletBinding()]
[OutputType([System.String])]
param(
[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
[System.String]
$SourceDir,
[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()] 
[System.String]
$DestDir,
[Parameter(Position=2, Mandatory=$true,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'leaf'})]
[System.String]
$HashFile
)
#endregion
try{
#Loading hash table
$xmlBackupIncHashFile = Import-Clixml $HashFile
$chainKeyBackupIncIncremental = $xmlBackupIncHashFile["ChainKey"]

$uintBackupIncCount = 0

#Starting directory lookup 
Backup-Files -SourceDir $SourceDir -DestDir $("$DestDir\$chainKeyBackupIncIncremental\Incremental_" + $(Get-Date -Format "ddMMyyyy.HHmm")) -HashTable $xmlBackupIncHashFile|`
ForEach-Object{ 
$xmlBackupIncHashFile[[string]$_.Keys]=[string]$_.Values
$uintBackupIncCount++
}

Write-Host -NoNewline "Saving XML file to $HashFile..."
Export-Clixml -Path $HashFile -InputObject $xmlBackupIncHashFile -Force
Write-Host "Done"

Write-Output $uintBackupIncCount
}
catch { throw $_ }
}

#0 - is OK. 1 - some error
$exitValue=0
}

process{
try{
$filesCopied=0
$strSourceFolderName = $(Get-Item $SourceDir).Name
$strHasFile = $("$HashPath\Hash_$strSourceFolderName.xml")
$strMessage = $($(Get-Date -Format "HH:mm_dd.MM.yyyy;") + "$BackupType backup of $SourceDir started")

#Automatically switch to full backup
$bSwitch = $(!$(Test-Path -LiteralPath $strHasFile -PathType "Leaf") -and $SwitchToFull)

Write-Log -Message $strMessage -LogFile $LogFile

switch($true){
$($BackupType -eq "Full" -or $bSwitch) {
$filesCopied = Backup-Full -SourceDir $SourceDir -DestDir $DestDir -HashFile $strHasFile -ChainKey $("Backup_$strSourceFolderName" + "_" + $(Get-Date -Format "ddMMyyyy"))
break
}
$($BackupType -eq "Incremental") {
$filesCopied = Backup-Inc -SourceDir $SourceDir -DestDir $DestDir -HashFile $strHasFile 
break
}
$($BackupType -eq "Differential") {
$filesCopied = Backup-Diff -SourceDir $SourceDir -DestDir $DestDir -HashFile $strHasFile 
break
}
}

$strMessage = $($(Get-Date -Format "HH:mm_dd.MM.yyyy;") + "$BackupType backup of $SourceDir completed successfully. $filesCopied items were copied.")

Write-Log -Message $strMessage -LogFile $LogFile

Write-Output $filesCopied
}
catch { 

$strMessage = $($(Get-Date -Format "HH:mm_dd.MM.yyyy;") + "$BackupType backup of $SourceDir failed:" + $_)
Write-Log -Message $strMessage -LogFile $LogFile
$exitValue = 1
}
}
end{exit $exitValue}

I have some experience writing Powershell scripts,but I am lost at how this script prompts for Source and Destination paths. I tried modifying the Param section, but this didnt work and up until now I thought the only way you could get a prompt was with"read-host". Any and all education on this matter would be greatly appreciated. (Side note: I have posted this question  on the forum in which I found it and have not got an answer yet).

param(
		[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$true)]
		[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
		[System.String]
		$SourceDir,
		[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
		[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
		[System.String]
		$DestDir,
		[Parameter(Position=2, Mandatory=$false,ValueFromPipeline=$false)]
		[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
		[System.String]
		$HashPath,
		[Parameter(Position=3, Mandatory=$false,ValueFromPipeline=$false)]
		[ValidateSet("Full","Incremental","Differential")] 
		[System.String]
		$BackupType="Full",
		[Parameter(Position=4, Mandatory=$false,ValueFromPipeline=$false)]
		[ValidateNotNullOrEmpty()]  
		[System.String]
		$LogFile=".\Backup-Files.log",
		[Parameter(Position=5, Mandatory=$false,ValueFromPipeline=$false)]
		[System.Management.Automation.SwitchParameter]
		$SwitchToFull
)
#endregion 

begin{
	function Write-Log
	{
		#region Params
		[CmdletBinding()]
		[OutputType([System.String])]
		param(
			[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$true)]
			[ValidateNotNullOrEmpty()]
			[System.String]
			$Message,
			[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$true)]
			[ValidateNotNullOrEmpty()]
			[System.String]
			$LogFile
		)
		#endregion
		try{
			Write-Host $Message
			Out-File -InputObject $Message -Append $LogFile
		}
		catch {throw $_}
	}

	function Get-Hash 
	{	
		#region Params
		[CmdletBinding()]
		[OutputType([System.String])]
		param(
			[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$true)]
			[ValidateNotNullOrEmpty()]
			[System.String]
			$HashTarget,
			[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateSet("File","String")]
			[System.String]
			$HashType
		)
		#endregion
		begin{
			try{ $objGetHashMD5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider } 
			catch {throw $_ }
		}
		process{
			try {
				#Checking hash target is file or just string
				switch($HashType){	"String" {	
						$objGetHashUtf8 = New-Object -TypeName System.Text.UTF8Encoding
						$arrayGetHashHash = $objGetHashMD5.ComputeHash($objGetHashUtf8.GetBytes($HashTarget.ToUpper()))
						break
					}"File" {	
						$arrayGetHashHash = $objGetHashMD5.ComputeHash([System.IO.File]::ReadAllBytes($HashTarget))
						break
					}
				}
				#Return hash
				Write-Output $([System.Convert]::ToBase64String($arrayGetHashHash))
			}
			catch { throw $_ }
		}
	}

	function Copy-File
	{
		#region Params
		[CmdletBinding()]
		[OutputType([System.String])]
		param(
			[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Any'})] 
			[System.String]
			$SourceFile,
			[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateNotNullOrEmpty()] 
			[System.String]
			$DestFile
		)
		#endregion
		try{
			#The script fails when folder being copied to file. So the item will be removed to avoid the error.
			if(Test-Path -LiteralPath $DestFile -PathType Any){
				Remove-Item -LiteralPath $DestFile -Force -Recurse
			}
			
			#Creating destination if doesn't exist. It's required because Copy-Item doesn't create destination folder
			if(Test-Path -LiteralPath $SourceFile -PathType Leaf){
				New-Item -ItemType "File" -Path $DestFile -Force
			}
			#Copying file to destination directory
			Copy-Item -LiteralPath $SourceFile -Destination $DestFile -Force
		}
		catch{ throw $_ }
	}

	function Backup-Files
	{
		#region Params
		[CmdletBinding()]
		[OutputType([System.String])]
		param(
			[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
			[System.String]
			$SourceDir,
			[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateNotNullOrEmpty()] 
			[System.String]
			$DestDir,
			[Parameter(Position=2, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateNotNull()] 
			[System.Collections.Hashtable]
			$HashTable
		)
		#endregion
		try{
			$xmlBackupFilesHashFile = $HashTable
			
			Write-Host "Backup started" 
			Get-ChildItem -Recurse -Path $SourceDir|ForEach-Object{
				$currentBackupFilesItem = $_
				#Full path to source and destination item
				$strBackupFilesSourceFullPath = $currentBackupFilesItem.FullName
				$strBackupFilesDestFullPath = $currentBackupFilesItem.FullName.Replace($SourceDir,$DestDir)
				#Checking that the current item is file and not directory. True - the item is file. 
				$bBackupFilesFile = $($($currentBackupFilesItem.Attributes -band [System.IO.FileAttributes]::Directory) -ne [System.IO.FileAttributes]::Directory)
				Write-Host -NoNewline ">>>Processing item $strBackupFilesSourceFullPath..."
				#Generating path hash
				$hashBackupFilesPath = $(Get-Hash -HashTarget $strBackupFilesSourceFullPath -HashType "String")
				$hashBackupFilesFile = "d"
				#If the item is file then generate hash for file content
				if($bBackupFilesFile){
					$hashBackupFilesFile = $(Get-Hash -HashTarget $strBackupFilesSourceFullPath -HashType "File")
				}

				#Checking that the file has been copied
				if($xmlBackupFilesHashFile[$hashBackupFilesPath] -ne $hashBackupFilesFile){
			
					Write-Host -NoNewline $("hash changed=>$hashBackupFilesFile...")
					Copy-File -SourceFile $strBackupFilesSourceFullPath $strBackupFilesDestFullPath|Out-Null
					#Returning result
					Write-Output @{$hashBackupFilesPath=$hashBackupFilesFile}
				}
				else{
					Write-Host -NoNewline "not changed..."
				}
				Write-Host "done"
			}
			Write-Host "Backup completed"
		}
		catch { throw $_ }
	}

	function Backup-Full
	{	
		[CmdletBinding()]
		[OutputType([System.String])]
		#region Params
		param(
			[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
			[System.String]
			$SourceDir,
			[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateNotNullOrEmpty()] 
			[System.String]
			$DestDir,
			[Parameter(Position=2, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateNotNullOrEmpty()] 
			[System.String]
			$HashFile,
			[Parameter(Position=3, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateNotNullOrEmpty()]  
			[System.String]
			$ChainKey
		)
		#endregion
		
		try{
			#Creating an empty hash table
			$xmlBackupFullHashFile = @{}
			
			#Starting directory lookup 
			$uintBackupFullCount = 0
			Backup-Files -SourceDir $SourceDir -DestDir $("$DestDir\$ChainKey\Full_" + $(Get-Date -Format "ddMMyyyy")) -HashTable $xmlBackupFullHashFile|`
			ForEach-Object{ 
				$xmlBackupFullHashFile.Add([string]$_.Keys,[string]$_.Values) 
				$uintBackupFullCount++
			}
			#Saving chain key.
			$xmlBackupFullHashFile.Add("ChainKey",$ChainKey)

			Write-Host -NoNewline "Saving XML file to $HashFile..."
			Export-Clixml -Path $HashFile -InputObject $xmlBackupFullHashFile -Force
			Write-Host "done"
			Write-Output $uintBackupFullCount
		}
		catch { throw $_ }
	}

	function Backup-Diff
	{	
		#region Params
		[CmdletBinding()]
		[OutputType([System.String])]
		param(
			[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
			[System.String]
			$SourceDir,
			[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateNotNullOrEmpty()] 
			[System.String]
			$DestDir,
			[Parameter(Position=2, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateScript({Test-Path -LiteralPath $_ -PathType 'leaf'})]
			[System.String]
			$HashFile
		)
		#endregion
		try{
			#Loading hash table
			$xmlBackupDiffHashFile = Import-Clixml $HashFile
			$chainKeyBackupDiffDifferential = $xmlBackupDiffHashFile["ChainKey"]
			$uintBackupDiffCount = 0
			#Starting directory lookup 
			Backup-Files -SourceDir $SourceDir -DestDir $("$DestDir\$chainKeyBackupDiffDifferential\Differential_" + $(Get-Date -Format "ddMMyyyy.HHmm")) -HashTable $xmlBackupDiffHashFile|`
			ForEach-Object{ $uintBackupDiffCount++ }
			Write-Output $uintBackupDiffCount
		}
		catch { throw $_ }
	}

	function Backup-Inc
	{	
		#region Params
		[CmdletBinding()]
		[OutputType([System.String])]
		param(
			[Parameter(Position=0, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})] 
			[System.String]
			$SourceDir,
			[Parameter(Position=1, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateNotNullOrEmpty()] 
			[System.String]
			$DestDir,
			[Parameter(Position=2, Mandatory=$true,ValueFromPipeline=$false)]
			[ValidateScript({Test-Path -LiteralPath $_ -PathType 'leaf'})]
			[System.String]
			$HashFile
		)
		#endregion
		try{
			#Loading hash table
			$xmlBackupIncHashFile = Import-Clixml $HashFile
			$chainKeyBackupIncIncremental = $xmlBackupIncHashFile["ChainKey"]
			$uintBackupIncCount = 0
			#Starting directory lookup 
			Backup-Files -SourceDir $SourceDir -DestDir $("$DestDir\$chainKeyBackupIncIncremental\Incremental_" + $(Get-Date -Format "ddMMyyyy.HHmm")) -HashTable $xmlBackupIncHashFile|`
			ForEach-Object{ 
				$xmlBackupIncHashFile[[string]$_.Keys]=[string]$_.Values
				$uintBackupIncCount++
			}
			Write-Host -NoNewline "Saving XML file to $HashFile..."
			Export-Clixml -Path $HashFile -InputObject $xmlBackupIncHashFile -Force
			Write-Host "Done"
			Write-Output $uintBackupIncCount
		}
		catch { throw $_ }
	}
	#0 - is OK. 1 - some error
	$exitValue=0
}

process{
	try{
		$filesCopied=0
		$strSourceFolderName = $(Get-Item $SourceDir).Name
		$strHasFile = $("$HashPath\Hash_$strSourceFolderName.xml")
		$strMessage = $($(Get-Date -Format "HH:mm_dd.MM.yyyy;") + "$BackupType backup of $SourceDir started")
		#Automatically switch to full backup
		$bSwitch = $(!$(Test-Path -LiteralPath $strHasFile -PathType "Leaf") -and $SwitchToFull)
		Write-Log -Message $strMessage -LogFile $LogFile
		switch($true){
			$($BackupType -eq "Full" -or $bSwitch) {
				$filesCopied = Backup-Full -SourceDir $SourceDir -DestDir $DestDir -HashFile $strHasFile -ChainKey $("Backup_$strSourceFolderName" + "_" + $(Get-Date -Format "ddMMyyyy"))
				break
			}
			$($BackupType -eq "Incremental") {
				$filesCopied = Backup-Inc -SourceDir $SourceDir -DestDir $DestDir -HashFile $strHasFile 
				break
			}
			$($BackupType -eq "Differential") {
				$filesCopied = Backup-Diff -SourceDir $SourceDir -DestDir $DestDir -HashFile $strHasFile 
				break
			}
		}
		$strMessage = $($(Get-Date -Format "HH:mm_dd.MM.yyyy;") + "$BackupType backup of $SourceDir completed successfully. $filesCopied items were copied.")
		Write-Log -Message $strMessage -LogFile $LogFile
		Write-Output $filesCopied
	}
	catch { 

		$strMessage = $($(Get-Date -Format "HH:mm_dd.MM.yyyy;") + "$BackupType backup of $SourceDir failed:" + $_)
		Write-Log -Message $strMessage -LogFile $LogFile
		$exitValue = 1
	}
}
end{exit $exitValue}



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>