Function Test-FileState-withLogging { <# .SYNOPSIS Function will check if given File is accessible. .DESCRIPTION This Function will check the accessibility of a File. If the given File was readable, Function will Return "readable" Additional Function can be called with the Parameter "-Action writetest", then after a successful readtest, there will be a writetest. On Success Return Value will be "writeable". If File doesn exist Function will return $NULL. If File Attribute was not editable (by resetting them), Function will return "noteditable". .REQUIREMENT General PowerShell V3 .REQUIREMENT Assembly .REQUIREMENT Variables SourceFile, SourceFileAttributeReset, Action, FileInfo, FileCheckResult, FileStream .REQUIREMENT Variables preSet .REQUIREMENT Functions Write-Logfile .VERSION Number: 1.1.0.0 / Date: 21.03.2017 .PARAMETER SourceFile Give the full Path and Filename you want to check. .PARAMETER SourceFileAttributeReset Set on $True, if Function should reset the File Attributes (if ReadOnly was set). Default Value is $True. .PARAMETER Action Determine which Action you want to perform . Default Value is "readtest". .EXAMPLE Test-FileState-withLogging -SourceFile "E:\Path\file_to_check.txt" .EXAMPLE Test-FileState-withLogging -SourceFile "E:\Path\file_to_check.txt" -Action writetest #> Param ( [Parameter(Mandatory=$True,HelpMessage='Give the full Path and Filename you want to check.')] [ValidateNotNullOrEmpty()] [String]$SourceFile, [Parameter(Mandatory=$False,HelpMessage='Set on $True, if Function should reset the File Attributes (if ReadOnly was set). Default Value is $True.')] [ValidateSet($True,$False)] [Switch]$SourceFileAttributeReset=$True, [Parameter(Mandatory=$False,HelpMessage='Determine which Action you want to perform . Default Value is "readtest".')] [ValidateSet("readtest","writetest")] [String]$Action="readtest" ) #end param #Checking if "Write-LogFile" Module was loaded IF (Get-Module -Name "Write-LogFile") { IF ((Test-Path -Path $SourceFile) -eq 'True') { $FileInfo = (New-Object System.IO.FileInfo $SourceFile) $FileCheckResult = "unknown" Write-Logfile -LogLine " " Write-Logfile -LogLine "Checking if file: $SourceFile is locked/readonly..." IF (($FileInfo.Attributes -match "ReadOnly") -and ($SourceFileAttributeReset -eq $True)) { Write-Host "DEBUG Info: ReadOnly Attribute found, removing if possible" Try { $FileInfo.Attributes="Archive","Temporary" Write-Host "DEBUG Info: File Attributes has been recessed!" } #end try Catch { Write-Logfile -LogLine "WARNING: Cannot recessed File Attributes!" Return "noteditable" } #end catch } #end if Write-Logfile -LogLine "Checking if file: $SourceFile is readable." # Check if file is readable Try { $FileStream = $FileInfo.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read) Write-Host "DEBUG Info: Is file readable?" $FileStream.CanRead $FileStream.Close() $FileCheckResult = "readable" } #end try Catch { Write-Logfile -LogLine "File: $SourceFile is not even readable." Write-Host "DEBUG Info: Is file readable?" $FileStream.CanRead $FileCheckResult = "notreadable" } #end catch # Check if file is writeable IF (($FileCheckResult -eq "readable") -and ($Action -eq 'writetest')) { Write-Logfile -LogLine "Checking if file: $SourceFile is writeable." Try { $FileStream = $FileInfo.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::Write, [System.IO.FileShare]::Write) Write-Host "DEBUG Info: Is file writeable?" $FileStream.CanWrite $FileStream.Close() $FileCheckResult = "writeable" } #end try Catch { Write-Logfile -LogLine "File: $SourceFile is not writeable." Write-Host "DEBUG Info: Is file writeable?" $FileStream.CanWrite $FileCheckResult = "notwriteable" } #end catch } #end if Write-Logfile -LogLine "File $SourceFile checked with result: $FileCheckResult" Return $FileCheckResult } #end if ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "Given file: $SourceFile seems not to exist, or access is denied." Return $NULL } #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