8
0
Skriptentwickung/archive/Modules/Test-Path-withLogging.psm1
2024-01-24 16:42:38 +01:00

114 lines
2.7 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Function Test-Path-withLogging {
<#
.SYNOPSIS
Function will check the given Path for existence.
.DESCRIPTION
If Path doesn´t exist, Function will try to create it.
You can disable the create Function by calling with "-Action "ignore"".
.REQUIREMENT General
PowerShell V2
.REQUIREMENT Assembly
<NONE>
.REQUIREMENT Variables
Path, PathTest
.REQUIREMENT Variables preSet
<NONE>
.REQUIREMENT Functions
Write-Logfile
.VERSION
Number: 1.0.1.0 / Date: 17.12.2016
.PARAMETER Path
Give the full Path you want to check or create.
.PARAMETER Action
Determine which Action you want to perform <create|ignore". Default Value is "create".
.EXAMPLE
Test-Path-withLogging -Path "E:\Path\to\create"
.EXAMPLE
Test-Path-withLogging -Path "E:\Path\to\check" -Action "ignore"
#>
Param (
[Parameter(Mandatory=$True,HelpMessage='Give the full Path you want to check.')]
[ValidateNotNullOrEmpty()]
[String]$Path,
[Parameter(Mandatory=$False,HelpMessage='Determine which Action you want to perform <create|ignore". Default Value is "create".')]
[ValidateSet("create","ignore")]
[String]$Action="create"
) #end param
#Checking if "Write-LogFile" Module was loaded
IF (Get-Module -Name "Write-LogFile") {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "Checking Path $Path for existence."
$PathTest = Test-Path -PathType Container $Path
#If given Path already exists
IF ($PathTest -eq "True") {
Write-Logfile -LogLine "Path $Path already exists and can be used."
Return $True
} #end if
#If given Path does not exist
ELSE {
Write-Logfile -LogLine "Path $Path does not exist."
IF ($Action -eq "create") {
Try {
Write-Logfile -LogLine "Path $Path has to been created."
New-Item -Path $Path -ItemType directory -Force -ErrorAction Stop
Return $True
} #end try
Catch {
Write-Logfile -LogLine "ERROR: Unable to create Path."
Write-Logfile -LogLine "INFO: Maybe there is an access or rights Problem."
Return $False
} #end catch
} #end if
ELSE {
Return $False
} #end else
} #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