Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 获取内容被拒绝或未找到时退出脚本_Powershell - Fatal编程技术网

Powershell 获取内容被拒绝或未找到时退出脚本

Powershell 获取内容被拒绝或未找到时退出脚本,powershell,Powershell,因此,我正在编写一个小脚本,使用Get Content比较两个文件。到目前为止,情况有些好转,但有一点需要注意。如果找不到或拒绝获取$location的内容,我希望脚本给出一个错误并停止。我在想一些关于这个的if声明,但是我有困难 if([string]::IsNullOrWhiteSpace($location)){ write-host "please input a valid path" exit 更新-有一个输入值$location,可以输入c:\test\test.txt。当前

因此,我正在编写一个小脚本,使用Get Content比较两个文件。到目前为止,情况有些好转,但有一点需要注意。如果找不到或拒绝获取$location的内容,我希望脚本给出一个错误并停止。我在想一些关于这个的if声明,但是我有困难

if([string]::IsNullOrWhiteSpace($location)){

write-host "please input a valid path"
exit 
更新-有一个输入值$location,可以输入c:\test\test.txt。当前脚本的工作方式是接受该输入并将其放入“(Get Content-Path$location)”。如果您输入了正确的路径和文件,则此操作有效,脚本将继续并显示“比较完成”。但是如果您输入了一个不存在的位置和文件,让我们说c:\awoieroiuaf脚本继续,并给出错误Get-Content:找不到路径“c:\pscript\hashgenv4.ps1 c:\Test2 c:\awoieroiuaf”,因为它不存在

然后它说“比较是完整的”。正如我之前所说的,在继续使用脚本之前,我想首先测试用户输入,以确定路径是否存在

并尝试了几种不同的方法

}    
else
{

Compare-Object -ReferenceObject $(Get-Content -Path $title2hashstorage/$sha256) -DifferenceObject $(Get-Content -Path $location)| Out-GridView


Write-host "Comparison is complete"
}                                        
exit                          

您可以为获取内容执行终止错误:

'one'
get-content foo -erroraction stop
'three'

您遇到了什么困难?只需使用Test Path$location查看它是否存在Test Path工作!非常感谢。我会慢慢地但肯定地学习。