Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

There is a specific CMDLET for Temp Files, but generally I use TempFiles when something "hacky" is going on like two scripts/programs running that don't communicate and using the file as a flag. I therefor use a hardcoded tempfile and check if it exists etc etc.

https://superuser.com/questions/1579709/powershell-temp-file-checking-if-then-run-commands

$FilePath="$env:TEMP\jobrunning.log"

#Del File if Exists and Test
Remove-File -FilePath $FilePath
$FExistBool = Test-DoesFileExistBool -FilePath $FilePath
Compare-IsBoolTrue -FilePath $FilePath -TFBool $FExistBool

#Create File And Re-Try
Add-EmptyFile -FilePath $FilePath
$FExistBool = Test-DoesFileExistBool -FilePath $FilePath
Compare-IsBoolTrue -FilePath $FilePath -TFBool $FExistBool

#Del File and Re-Try
Remove-File -FilePath $FilePath
$FExistBool = Test-DoesFileExistBool -FilePath $FilePath
Compare-IsBoolTrue -FilePath $FilePath -TFBool $FExistBool

#Last Run
Add-EmptyFile -FilePath $FilePath
$FExistBool = Test-DoesFileExistBool -FilePath $FilePath
Compare-IsBoolTrue -TFBool $FExistBool

##End Script##
PAUSE
##Funcs#
function Compare-IsBoolTrue {
    param (
        [string[]]$TFBool
    )
    IF ($TFBool -eq "True") {
     Write-Host "File Found"
    } ELSE {
     Write-Host "File NOT Found"
    }
}
function Test-DoesFileExistBool {
    param (
        [string[]]$FilePath
    )
    Test-Path $FilePath 2> $null
}

function Remove-File {
    param (
        [string[]]$FilePath
    )
    Remove-Item $FilePath -ErrorAction SilentlyContinue
}

function Add-EmptyFile {
    param (
        [string[]]$FilePath
    )
    New-Item -Path $FilePath -ItemType File 2>&1>$null
}
  • No labels