2016-10-04 83 views
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 
    } 
} 

显然我失去了一些基本的东西,但我不明白这一点。完整的原始脚本在另一个区域具有相似的代码,并且工作正常。

+0

仔细检查文件名,特别是因为你指出类似的代码工作正常。 – rrirower

+1

Get-Item $ allfile'产生什么结果? –

+0

如果我在脚本中运行Get-Item $ allfile,它会出现“Get-Item:Can not find path'C:\ Users \ RS02130 \ Desktop \ SonarRules \ jsrulesall.xlsx”错误,因为它不存在。如果我在脚本之外运行,它运行良好。该文件绝对存在。我做了一个从Windows资源管理器复制为路径,并将路径直接粘贴到脚本中... –

回答

0

想通了。一个坏文件通配符的组合进一步说明了在进一步下去之前删除了该文件,以及Get-ChildItem和Write-Host的异步性质,这使得我认为该文件在那时不存在。