PowerShell - 临时文件检查 - 如果则 - 运行命令

PowerShell - 临时文件检查 - 如果则 - 运行命令

我对 PowerShell 还很陌生,并尝试执行一些基本的“创建临时文件”“检查临时文件是否存在”“如果 FileExists = True/False 则”编码逻辑,但我显然遗漏了一些东西,因为我没有得到一致的结果。

这是我目前所拥有的,问题似乎是$TmpFileExists在“重新运行”时没有更新(是否重新运行?)

我希望 IF 语句的第二次运行返回 ELSE 部分,但它只返回“TRUE”。

$TmpFile = New-Item -Path "$env:TEMP\jobrunning.log" -ItemType File
$TmpFileExists = (Test-Path $TmpFile) | Out-String
$DelTmpFile = Remove-Item $TmpFile -ErrorAction SilentlyContinue
IF ($TmpFileExists  = "True") {$DelTmpFile ; $TmpFileExists } ELSE {Write-Host "File NOT Found"}
IF ($TmpFileExists  = "True") {$DelTmpFile ; $TmpFileExists } ELSE {Write-Host "File NOT Found"}

其想法是,如果找到该文件,就删除它,重置/检查布尔值TmpFileExists并重新运行 If 语句,现在应该返回ELSE/FALSE语句。

答案1

逐行来看:

$TmpFile = New-Item -Path "$env:TEMP\jobrunning.log" -ItemType File

到目前为止一切顺利。您已创建文件。

$TmpFileExists = (Test-Path $TmpFile) | Out-String

$TmpFileExists 现在具有字符串值“True”如果不重新分配或其他操作,则不会改变。引用变量不会重新评估其赋值表达式。您应该将其置于Test-Path内联中或定义一个函数。

$DelTmpFile = Remove-Item $TmpFile -ErrorAction SilentlyContinue

现在,您已删除 引用的文件$TmpFile。您的后续代码似乎假设它是一个简单的函数。varialbe$DelTmpFile的值为$Null

IF ($TmpFileExists  = "True") {$DelTmpFile ; $TmpFileExists } ELSE {Write-Host "File NOT Found"}
IF ($TmpFileExists  = "True") {$DelTmpFile ; $TmpFileExists } ELSE {Write-Host "File NOT Found"}

正如 HelpingHand 所指出的,这些表达式正在重新分配“Ttue”的字符串值。任何成功的非空赋值表达式$True在转换为布尔值时都将求值为。因此,这些表达式简化为:

If ($True) { $Null; "True"} Else {Write-Host "File NOT Found"}

(希望) 明确说明为什么您的Else条款从未执行。


工作样本:

$TmpFile = New-Item -Path "$env:TEMP\jobrunning.log" -ItemType File
### File has been created
If ( Test-Path $TmpFile ) {
    Write-Host "File found. Deleting..."
    Remove-Item $TmpFile -ErrorAction SilentlyContinue
} Else {
    Write-Host "File NOT Found"
}
### File now deleted
If ( Test-Path $TmpFile ) {
    Remove-Item $TmpFile -ErrorAction SilentlyContinue
} Else {
    Write-Host "File NOT Found"
}

答案2

作为@Keith Miller 的回答解释说我试图引用一个变量,期望它“重新评估”。我直觉地决定使用函数,并使一切正常。这是我最终得到的结果,但它并不完全一样,因为我没有构建“If Else”“Delete File”代码片段,但这主要是一次学习练习,我现在对我的理解很有信心。

$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
}

结果:

File NOT Found
File Found
File NOT Found
File Found
Press Enter to continue...:

相关内容