Anlage des Repos
This commit is contained in:
122
current/Modules/Remove-Item-withLogging.psm1
Normal file
122
current/Modules/Remove-Item-withLogging.psm1
Normal file
@@ -0,0 +1,122 @@
|
||||
Function Remove-Item-withLogging {
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Function will delete Items selected by Path, Name and Age.
|
||||
|
||||
.DESCRIPTION
|
||||
Function will delete Items selected by Path, Name and Age, with logging this.
|
||||
For a successful performed deletion, Function will Return $True, for unccessful $False.
|
||||
|
||||
.REQUIREMENT General
|
||||
PowerShell V2
|
||||
|
||||
.REQUIREMENT Variables
|
||||
Path, FileKeepTime, FileBaseName, Item, Items
|
||||
|
||||
.REQUIREMENT Assembly
|
||||
<NONE>
|
||||
|
||||
.REQUIREMENT Functions
|
||||
Write-LogFile
|
||||
|
||||
.VERSION
|
||||
Number: 1.1.1.1 / Date: 25.04.2017
|
||||
|
||||
.PARAMETER Path
|
||||
Give the Full Path, where the Item(s) located, which should be deleted.
|
||||
|
||||
.PARAMETER FileKeepTime
|
||||
Give the time in Days, which Items should be deleted, which are older than...
|
||||
|
||||
.PARAMETER FileBaseName
|
||||
Give the <Filename> which will be selected, the Item(s) to delete. Given <Filename> will be set with Wildcards: *<Filename>*.
|
||||
|
||||
.PARAMETER FileExtension
|
||||
Give the FileExtension which Files will be selected for deletion (Default: log).
|
||||
|
||||
.EXAMPLE
|
||||
Remove-Item-withLogging -Path "E:\LogFiles" -FileKeepTime 5 -FileBaseName Filename
|
||||
#>
|
||||
|
||||
[cmdletbinding()]
|
||||
|
||||
Param (
|
||||
|
||||
[Parameter(Mandatory=$True,HelpMessage='Give the Full Path, where the Item(s) located, which should be deleted.')]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]$Path=$NULL,
|
||||
|
||||
[Parameter(Mandatory=$True,HelpMessage='Give the time in Days, which Item(s) should be deleted, which are older than...')]
|
||||
[ValidateRange(0,1000)]
|
||||
[Int]$FileKeepTime=$NULL,
|
||||
|
||||
[Parameter(Mandatory=$True,HelpMessage='Give the Filename which will be selected, the Item(s) to delete. Given <Filename> will be set with Wildcards: *<Filename>*.')]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]$FileBaseName=$NULL,
|
||||
|
||||
[Parameter(Mandatory=$False,HelpMessage='Give the FileExtension which Files will be selected for deletion (Default: log).')]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]$FileExtension="log"
|
||||
|
||||
) #end param
|
||||
|
||||
#Checking if "Write-LogFile" Module was loaded
|
||||
IF (Get-Module -Name "Write-LogFile") {
|
||||
|
||||
IF ($FileKeepTime -gt 0) {
|
||||
|
||||
Write-Logfile -LogLine "Files should be removed which are older than $FileKeepTime Day(s)."
|
||||
[Array]$Items = (Get-ChildItem -Path "$Path\*" -Filter *.$FileExtension | where {$_.Name -like "*$FileBaseName*" -and $_.lastwritetime -lt $((Get-Date).AddDays(-$FileKeepTime)) -and -not $_.psiscontainer})
|
||||
|
||||
IF ($Items.count -le 0) {
|
||||
|
||||
Write-Logfile -LogLine "Found no old Files."
|
||||
Return $False
|
||||
|
||||
} #end if
|
||||
|
||||
ELSE {
|
||||
|
||||
Write-Logfile -LogLine "Deleting old Files (Found: $($Items.count)) :"
|
||||
|
||||
Try {
|
||||
|
||||
$Items | Remove-Item -Path $Item -Force -Verbose -ErrorAction Stop
|
||||
Write-Logfile -LogLine "Files were removed!"
|
||||
Return $True
|
||||
|
||||
} #end try
|
||||
|
||||
Catch {
|
||||
|
||||
Write-Logfile -LogLine "Cannot remove Files!"
|
||||
Write-Logfile -LogLine "Please check your privileges!"
|
||||
Return $False
|
||||
|
||||
} #end catch
|
||||
|
||||
} #end else
|
||||
|
||||
} #end if
|
||||
|
||||
ELSE {
|
||||
|
||||
Write-Logfile -LogLine "You disabled File deletion, they all will be kept."
|
||||
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
|
||||
Reference in New Issue
Block a user