2016-11-30 39 views
1

我需要在SQL作业中检查是否存在某个路径上的文件,因此如果存在文件,则停止进一步执行。喜欢的东西:CMDExec如果存在然后停止

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

回答

3

只是省略-exists开关,确保$ErrorActionPreference设置为stop

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

尔......如果文件确实存在,我想他会停下来。 –

+0

@WalterMitty你是对的。将编辑我的答案 - 只是想知道为什么他抛出一个异常,如果该文件不存在(多数民众赞成在实际的状态,他期待)... –

+1

好吧,解释它。有一些备份/恢复工作在这个机制上工作:1.从产品中取得备份2.在办公区域恢复它3.删除可以根据用户请求重新做的备份文件。不是我的逻辑家伙 –

0

测试路径cmdlet返回布尔和你差不多有:

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