0
哇。我今天必须在这些愚蠢的药丸上。我在脚本中使用了一个简单的测试路径,当为时,我无法弄清楚它为什么不进入IF模块。我跑在ISE命令窗口相同的代码,它工作正常:Powershell测试路径不工作?
$allfile="c:\users\rs02130\desktop\sonarRules\jsrulesall.xlsx"
Write-Output "Testing $allfile"
if (Test-Path $allfile)
{
Write-Output "In If"
try
{
Write-Output "Removing $allfile"
Remove-Item $allfile -Force -ErrorAction Stop
Write-Output "Removed $allfile"
}
catch
{
Write-Error "Error removing $allfile"
exit 1
}
}
显然我失去了一些基本的东西,但我不明白这一点。完整的原始脚本在另一个区域具有相似的代码,并且工作正常。
仔细检查文件名,特别是因为你指出类似的代码工作正常。 – rrirower
Get-Item $ allfile'产生什么结果? –
如果我在脚本中运行Get-Item $ allfile,它会出现“Get-Item:Can not find path'C:\ Users \ RS02130 \ Desktop \ SonarRules \ jsrulesall.xlsx”错误,因为它不存在。如果我在脚本之外运行,它运行良好。该文件绝对存在。我做了一个从Windows资源管理器复制为路径,并将路径直接粘贴到脚本中... –