8
0
Skriptentwickung/current/Modules/MoveOrCopy-Item-withLogging.psm1

292 lines
10 KiB
PowerShell

Function MoveOrCopy-Item-withLogging {
<#
.SYNOPSIS
Function will copy or move File(s).
.DESCRIPTION
If File already exists in target Path new File will get a Version ~2.
.REQUIREMENT General
PowerShell V2
.REQUIREMENT Assembly
<NONE>
.REQUIREMENT Variables
Datei, Datei1, DateiName, DateiEndung, DateiTest, DateiVersion,ZielPath, MoveorCopy, SourcePath
.REQUIREMENT Variables preSet
VersionSeperator
.REQUIREMENT Functions
Write-Logfile
.VERSION
1.2.1.2 / 23.02.2024
.PARAMETER SourceFile
Give the full Path and Filename to one File.
.PARAMETER DestinationPath
Give the Target Path you want to move or copy the specified File.
.PARAMETER Action
Determine which Action you want to perform <Copy|Move>. Default Value is "Copy".
.PARAMETER NewFileName
Give a String, which get the new Name of the File you are moving.
.PARAMETER NewFileName_prefix
Give a String, which get the new prefix Name of the File you are moving.
.PARAMETER NewFileName_suffix
Give a String, which get the new suffix Name of the File you are moving.
.EXAMPLE
MoveOrCopy-Item-withLogging -SourceFile "E:\Quellpfad\Test.txt" -DestinationPath "E:\Zielpfad" -move_or_copy move
.EXAMPLE
MoveOrCopy-Item-withLogging -SourceFile "E:\Quellpfad\Test.txt" -DestinationPath "E:\Zielpfad" -move_or_copy copy
#>
Param (
[Parameter(Position=0,Mandatory=$True,HelpMessage='Give the full Path and Filename to one File.')]
[ValidateNotNullOrEmpty()]
[String]$SourceFile,
[Parameter(Mandatory=$True,HelpMessage='Give the Target Path you want to move or copy the specified File.')]
[ValidateNotNullOrEmpty()]
[String]$DestinationPath,
[Parameter(Mandatory=$True,HelpMessage='Determine which Action you want to perform <Copy|Move>. Default Value is "Copy".')]
[ValidateSet("copy","move")]
[String]$Action="copy",
[Parameter(Mandatory=$False,HelpMessage='Give a String, which get the new Name of the File you are moving.')]
[ValidateNotNullOrEmpty()]
[String]$NewFileName,
[Parameter(Mandatory=$False,HelpMessage='Give a String, which get the new prefix Name of the File you are moving.')]
[ValidateNotNullOrEmpty()]
[String]$NewFileName_prefix,
[Parameter(Mandatory=$False,HelpMessage='Give a String, which get the new suffix Name of the File you are moving.')]
[ValidateNotNullOrEmpty()]
[String]$NewFileName_suffix
) #end param
#Clear Error Variable
$error.clear()
#Checking if "Write-LogFile" Module was loaded
IF (Get-Module -Name "Write-LogFile") {
IF ((Test-Path $SourceFile -PathType Leaf) -eq $True) {
Write-Logfile -LogLine ""
$SourcePath = ([System.IO.Path]::GetDirectoryName($SourceFile).ToString())
$FileName = ([System.IO.Path]::GetFileName($SourceFile).ToString())
$FileName_noExt = ([System.IO.Path]::GetFileNameWithoutExtension($SourceFile).ToString())
$FileExtension = ([System.IO.Path]::GetExtension($SourceFile).ToString())
IF (!$VersionSeperator) {
Write-Logfile -LogLine "ERROR: Wrong Function call."
Write-Logfile -LogLine "INFO: Variable VersionSeperator is not valid."
Write-Logfile -LogLine "Application was unplannd terminated."
EXIT
} #end if
IF ($newfilename) {
Write-Logfile -LogLine "New Filename ($newfilename) will replace the old one."
Set-Variable -Name Filename -Value ($newfilename+$FileExtension)
Set-Variable -Name Filename_noExt -Value ($newfilename)
} #end if
IF ($newfilename_prefix) {
Write-Logfile -LogLine "New prefix ($newfilename_prefix) has been set for file."
Set-Variable -Name Filename -Value (($newfilename_prefix+$FileSeperator+$Filename) -replace "$FileSeperator$FileSeperator","$FileSeperator")
Set-Variable -Name Filename_noExt -Value (($newfilename_prefix+$FileSeperator+$Filename_noExt) -replace "$FileSeperator$FileSeperator","$FileSeperator")
} #end if
IF ($newfilename_suffix) {
Write-Logfile -LogLine "New suffix ($newfilename_suffix) has been set for file."
Set-Variable -Name FileName -Value (($Filename -split "$VersionSeperator")[0])
Set-Variable -Name FileName_noExt -Value (($Filename_noExt -split "$VersionSeperator")[0])
Set-Variable -Name Filename -Value (($Filename_noExt+$FileSeperator+$newfilename_suffix+$FileExtension) -replace "$FileSeperator$FileSeperator","$FileSeperator")
Set-Variable -Name Filename_noExt -Value (($Filename_noExt+$FileSeperator+$newfilename_suffix) -replace "$FileSeperator$FileSeperator","$FileSeperator")
} #end if
# Does file already exist in the target directory?
$FileTest = Test-Path "$DestinationPath\$FileName" -PathType Leaf
IF ($Filetest -eq $True) {
Write-Logfile -LogLine "The File ($Filename) already exists in the target directory, starting Version Check."
[String]$FileNameSplit = ($Filename_noExt -split $VersionSeperator)
[Int]$FileVersion = 0
IF ($FileNameSplit[1] -is [Int]) {
[Int]$FileVersion = $FileNameSplit[1]
} #end if
# Has the new file already a Version tag?
IF ($FileVersion -eq 0) {
Write-Host "DEBUG Info: Sourcefile includes no VersionSeperator."
# To skip Version ~1.
$FileVersion++
} #end if
ELSE {
Write-Host "DEBUG Info: Sourcefile includes VersionSeperator."
$FileName_noExt = $FilenameSplit[0]
} #end else
DO {
Write-Host "DEBUG Info: Count file version:" $FileVersion; $FileVersion++
}
WHILE (($Filetest = Test-Path -Path "$DestinationPath\$FileName_noExt$VersionSeperator$FileVersion$FileExtension") -eq 'True')
# code block for the copy or move process
Try {
Set-Variable -Name FileHash_befor -Value (Get-FileHash -Path "$SourceFile" -Algorithm SHA512 | Select-Object Hash | Foreach-Object {$_ -replace "@{Hash=", ""} | Foreach-Object {$_ -replace "}", ""})
Set-Variable -Name FileTemp -Value (Copy-Item -Path "$SourceFile" -Destination "$DestinationPath\$Timestamp3`_$FileName" -PassThru -Force)
Write-Logfile -LogLine "The $Action command has been completed."
Set-Variable -Name FileFinal -Value (Rename-Item -Path "$FileTemp" -NewName "$FileName_noExt$VersionSeperator$FileVersion$FileExtension" -PassThru -Force )
Write-Logfile -LogLine "File was renamed to: $FileName_noExt$VersionSeperator$FileVersion$FileExtension, and is now transferd to: $DestinationPath."
Set-Variable -Name FileHash_after -Value (Get-FileHash -Path "$FileFinal" -Algorithm SHA512 | Select-Object Hash | Foreach-Object {$_ -replace "@{Hash=", ""} | Foreach-Object {$_ -replace "}", ""})
Write-Host "DEBUG Info: Hash Value before: $FileHash_befor"
Write-Host "DEBUG Info: Hash Value after: $FileHash_after"
IF ($Action -eq 'move') {
Write-Host "DEBUG Info: Moving action was choosen, will delete source file."
IF ($FileHash_befor -eq $FileHash_after) {
Try {
Remove-Item -Path "$SourceFile" -Force
} #end try
Catch {
Write-Logfile -LogLine "Error removing the source file."
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine "HASH Value does mismatch!"
Write-Logfile -LogLine "Aborting delete operation!"
} #end else
} #end if
Return $FileFinal.Fullname
} #end try
Catch {
Write-Logfile -LogLine "Error at $Action command, processing file: $SourceFile"
Write-Logfile -LogLine "Please check your privileges"
exit
} #end catch
} #end if
ELSE {
Set-Variable -Name FileHash_befor -Value (Get-FileHash -Path "$SourceFile" -Algorithm SHA512 | Select-Object Hash | Foreach-Object {$_ -replace "@{Hash=", ""} | Foreach-Object {$_ -replace "}", ""})
Set-Variable -Name FileFinal -Value (Copy-Item -Path "$SourceFile" -Destination "$DestinationPath\$FileName" -PassThru -Force)
Set-Variable -Name FileHash_after -Value (Get-FileHash -Path "$FileFinal" -Algorithm SHA512 | Select-Object Hash | Foreach-Object {$_ -replace "@{Hash=", ""} | Foreach-Object {$_ -replace "}", ""})
Write-Logfile -LogLine "The $Action command has been completed for file: $SourceFile"
Write-Logfile -LogLine "And is now transferd to: $DestinationPath"
Write-Host "DEBUG Info: Hash Value before: $FileHash_befor"
Write-Host "DEBUG Info: Hash Value after: $FileHash_after"
IF ($Action -eq 'move') {
Write-Host "DEBUG Info: Moving action was choosen, will delete source file."
IF ($FileHash_befor -eq $FileHash_after) {
Try {
Remove-Item -Path "$SourceFile" -Force
} #end try
Catch {
Write-Logfile -LogLine "Error removing the source file."
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine "HASH Value does mismatch!"
Write-Logfile -LogLine "Aborting delete operation!"
} #end else
} #end if
ELSEIF ($Action -eq 'copy') {
Write-Host "DEBUG Info: Coping action was choosen, will not delete original file."
} #end elseif
Return $FileFinal.Fullname
} #end else
} #end if
ELSE {
Write-LogFile -LogLine "Cannot find Source File!"
Return $False
} #end else
} #end if
ELSE {
Write-Host ""
Write-Host "DEBUG Info: Write-LogFile - Module does not exist!"
Write-Host "DEBUG Info: Please load the Module and try again, running this Function/Module!"
Write-Host "DEBUG Info: Exiting, because of this Issue."
EXIT
} #end else
} #end function