2020-05-22 03:52:48 -04:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Wrapper for Remove-Item to secuerly remove items allowing better handling for errors
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Removes files and folders from disk and catches possible exceptions with proper return
|
|
|
|
|
values to handle errors better
|
|
|
|
|
.FUNCTIONALITY
|
|
|
|
|
Wrapper for Remove-Item to secuerly remove items allowing better handling for errors
|
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS>Remove-ItemSecure -Path C:\icinga;
|
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS>Remove-ItemSecure -Path C:\icinga -Recurse;
|
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS>Remove-ItemSecure -Path C:\icinga -Recurse -Force;
|
|
|
|
|
.PARAMETER Path
|
|
|
|
|
The path to a file or folder you wish you delete
|
|
|
|
|
.PARAMETER Recurse
|
|
|
|
|
Removes sub-folders and sub-files for a given location
|
|
|
|
|
.PARAMETER Force
|
|
|
|
|
Tries to forefully removes a files and folders if they are either being used or a folder is
|
|
|
|
|
still containing items
|
|
|
|
|
.INPUTS
|
|
|
|
|
System.String
|
|
|
|
|
.OUTPUTS
|
|
|
|
|
System.Boolean
|
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/Icinga/icinga-powershell-framework
|
|
|
|
|
#>
|
|
|
|
|
|
2019-12-06 13:29:17 -05:00
|
|
|
function Remove-ItemSecure()
|
|
|
|
|
{
|
|
|
|
|
param(
|
|
|
|
|
[string]$Path,
|
2021-08-17 07:24:56 -04:00
|
|
|
[switch]$Recurse = $FALSE,
|
|
|
|
|
[switch]$Force = $FALSE
|
2019-12-06 13:29:17 -05:00
|
|
|
)
|
|
|
|
|
|
2021-08-17 07:24:56 -04:00
|
|
|
if ([string]::IsNullOrEmpty($Path) -Or (Test-Path $Path) -eq $FALSE) {
|
|
|
|
|
Write-IcingaConsoleError 'The provided path "{0}" does not exist' -Objects $Path;
|
2019-12-06 13:29:17 -05:00
|
|
|
return $FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2021-08-17 07:24:56 -04:00
|
|
|
Remove-Item -Path $Path -Recurse:$Recurse -Force:$Force -ErrorAction Stop;
|
2019-12-06 13:29:17 -05:00
|
|
|
return $TRUE;
|
|
|
|
|
} catch {
|
2021-08-17 07:24:56 -04:00
|
|
|
$ExMsg = $_.Exception;
|
|
|
|
|
Write-IcingaConsoleError 'Failed to remove items from path "{0}". Recurse is "{1}", Force is "{2}": "{3}"' -Objects $Path, $Recurse, $Force, $ExMsg;
|
2019-12-06 13:29:17 -05:00
|
|
|
}
|
|
|
|
|
return $FALSE;
|
|
|
|
|
}
|