【发布时间】:2016-05-09 13:25:38
【问题描述】:
我有以下 Powershell 代码:
function readConfigData
{
$workingDir = (Get-Location).Path
$file = ""
if ($Global:USE_LOCAL_SERVER)
{
$file = $workingDir + '\Configs\Localhost.ini'
}
else
{
$file = $workingDir + '\Configs\' + $env:COMPUTERNAME + '.ini'
}
Write-Host 'INIFILE: ' $file
if (!$file -or ($file = ""))
{
throw [System.Exception] "Ini fil är inte satt."
}
if (!(Test-Path -Path $file))
{
throw [System.Exception] "Kan inte hitta ini fil."
}
}
readConfigData
我应该如何声明可以传递给函数Test-Path的局部变量$file。
我的局部变量 $file 被填充,但是当我将它作为其他函数的参数放置时,它就像它超出了范围。
我阅读了about scopes 的文章,但无法弄清楚。
目前我收到错误:
INIFILE: D:\Projects\scripts\Configs\HBOX.ini 测试路径:无法将参数绑定到 参数 'Path' 因为它是一个空字符串。在 D:\Projects\freelancer.com\nero2000\cmd 脚本到 powershell\script.ps1:141 字符:27 + if (!(Test-Path -Path $file)) + ~~~~~ + CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand
【问题讨论】:
标签: powershell