functions/function-Set-LogPath.ps1
function Set-LogPath { <# .SYNOPSIS Tests if the logging path exists and creates it if not. .DESCRIPTION Tests if the logging path exists and creates it if not. Use parameter -WriteLog if you want to leverage the Write-Log Cmdlet to write to a specific log file. If it is not used it will write status messages to the screen. .PARAMETER Path The directory path to test and create. .PARAMETER WriteLog Switch parameter to use the Write-Log Cmdlet. .EXAMPLE Set-LogPath -Path $env:\SystemDrive\MyLogFolder .EXAMPLE Set-LogPath -Path $env:\SystemDrive\MyLogFolder -WriteLog .INPUTS [string] .OUTPUTS [bool] #> [CmdletBinding()] param ( [Parameter(Mandatory)][string]$Path, [Parameter()][switch]$WriteLog = $false ) try { if (!(Test-Path -Path $Path)) { switch ($WriteLog) { $true { Write-Log -Message "[$($MyInvocation.MyCommand)]: Logging directory [$Path] was not found. Attempting to create." } $false { Write-Host "[$($MyInvocation.MyCommand)]: Logging directory [$Path] was not found. Attempting to create." } } $null = New-Item -ItemType Directory -Path $Path -Force -ErrorAction Stop switch ($WriteLog) { $true { Write-Log -Message "[$($MyInvocation.MyCommand)]: Logging directory creation was successful." } $false { Write-Host "[$($MyInvocation.MyCommand)]: Logging directory creation was successful." } } return $true } switch ($WriteLog) { $true { Write-Log -Message "[$($MyInvocation.MyCommand)]: Logging directory [$Path] was found. Skipping creation." } $false { Write-Host "[$($MyInvocation.MyCommand)]: Logging directory [$Path] was found. Skipping creation." } } return $true } catch { Write-Error -Message "[$($MyInvocation.MyCommand)]: Failure encountered attempting to create logging directory [$Path]. Exception: $($_.Exception.Message)" return $false } } |