CMDExec 如果存在则停止

CMDExec if exists then stop

我需要介入 SQL 作业来检查某个路径上是否有文件,所以如果有文件则停止进一步执行。类似于:

$file = "\networklocation\file.bak"
if (-exists (Test-Path $file)) 
{
    throw "$file not found."
}

测试路径 cmdlet return 布尔值,您就快完成了:

$file = "\networklocation\file.bak"
if (Test-Path $file)
{
    throw "$file not found."
}

只需省略 -exists 开关并确保 $ErrorActionPreference 设置为 stop:

$ErrorActionPreference = 'stop'
$file = "\networklocation\file.bak"
if (Test-Path $file) 
{
    throw "$file found."
}