这是我尝试过的第一个powershell脚本.当我运行它时,第一部分运行良好并创建log.txt,但在再次运行后它仍然运行第1部分.如何检查日志文件是否存在?
编辑:我忘了提到我从PowerShell ISE运行脚本,如果这有所作为.
#Variables.
#Set working directory to scripts location.
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
#Check if log file exists.
$ChkFile = "%userprofile%\Desktop\log.txt"
$FileExists = (Test-Path $ChkFile -PathType Leaf)
#Set dir to script location.
Set-Location $dir
#Part 1.
If (!($FileExists))
{
Write-Host "Part 1"
echo 0 >>log.txt
}
#Part 2.
ElseIf ($FileExists)
{
Write-Host "Part 2"
}
Run Code Online (Sandbox Code Playgroud)
%用于cmd.exe变量扩展.PowerShell需要不同的语法.而是使用:
"$env:userprofile\Desktop\log.txt"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26262 次 |
| 最近记录: |