【发布时间】:2015-07-14 13:30:27
【问题描述】:
我可以使用 Test-Path 来检查输入的文件名是否存在,但是如果用户点击RETURN 并且输入字符串为空白,我想避免产生系统错误。我认为-ErrorAction 通用参数可以解决问题,但是:
$configFile = Read-Host "Please specify a config. file: "
$checkfile = Test-Path $configFile -ErrorAction SilentlyContinue
仍然产生:
Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
At C:\Scripts\testparm2.ps1:19 char:31
+ $checkfile = Test-Path <<<< $configFile -ErrorAction SilentlyContinue
+ CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand
我是否必须明确检查字符串是否为空白或 NULL?
我正在使用 PowerShell v2.0
【问题讨论】:
-
如果您检查 Ansgar Wiechers 的答案...您的代码在传递 0 时返回 $true
-
只是为了避免混淆无辜的旁观者:0和null是不同的值,具有不同的含义。