【问题标题】:Test-Path, path is not valid测试路径,路径无效
【发布时间】:2017-03-17 08:47:50
【问题描述】:

我得到一个“路径无效”,即使它是有效的。我用自己的用户运行它,我可以浏览文件夹并删除文件夹中的文件。但是当我运行我的代码时,我得到“路径无效”。我也尝试在管理控制台中运行它。难道我做错了什么? Powershell 版本:4

param (  
    $rootPath = "$env:ProgramFiles\NetApp\SnapManager for Exchange\Report\",
    $paths = ("Debug [11EXS05]\","Backup [11EXS05]\"),
    $wildCard = "*.txt",
    $daysToKeep = "14"
)

#define LastWriteTime parameter based on $daysToKeep
$now = Get-Date
$lastWrite = $now.AddDays(-$daysToKeep)

#Loop through each path in paths
foreach($path in $paths)
{
    $currentPath = Join-Path -Path $rootPath $path    
    if(Test-Path $currentPath)
    {
        #get all the files that has LastWriteTime less than today minus $daysToKeep, then delete those files, pipe output to screen
        Get-ChildItem $currentPath `
            -Include $wildCard -Recurse | `
            Where {$_.LastWriteTime -le "$lastWrite"} | foreach{ "Removing file $($_.FullName)"; Remove-Item $_}
    }
    else
    {
        Write-Host ($currentPath + " is not valid") -ForegroundColor Red
    }

}

编辑,解决办法:

必须像下面的答案一样修复[]。而且,我必须将$currentPath 添加到Remove-Item 命令,完整的脚本:

param (  
    $rootPath = "$env:ProgramFiles\NetApp\SnapManager for Exchange\Report\",
    $paths = ('Debug `[11EXS05`]\','Backup `[11EXS05`]\'),
    $wildCard = "*.txt",
    $daysToKeep = "17"
)

#define LastWriteTime parameter based on $daysToKeep
$lastWrite = (Get-Date).Date.AddDays(-$daysToKeep)

#Loop through each path in paths
foreach($path in $paths)
{
    $currentPath = Join-Path -path $rootPath $path

    if(Test-Path $currentPath)
    {
        #get all the files that has LastWriteTime less than today minus $daysToKeep, then delete those files, pipe output to screen
        Get-ChildItem $currentPath `
            -Filter $wildCard -Recurse | `
            Where {$_.LastWriteTime -le "$lastWrite"} | foreach{ "Removing file $($_.FullName)"; Remove-Item ($currentPath + "\" + $_) -Force}        

    }
    else
    {
        Write-Host ($currentPath + " is not valid") -ForegroundColor Red
    }
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    是文件夹名称中的方括号给您带来了问题。见https://stackoverflow.com/a/21008352/1001100。您需要在每个方括号之前放置一个反引号。由于 PowerShell 使用反引号作为转义字符,因此如果要使用双引号字符串,则需要 两个 个反引号。

    将您的第二行更改为:

    $paths = ("Debug ``[11EXS05``]\","Backup ``[11EXS05``]\"),
    

    或者这个,如果你想切换到单引号字符串:

    $paths = ('Debug `[11EXS05`]\','Backup `[11EXS05`]\'),
    

    它会起作用的。

    【讨论】:

      【解决方案2】:

      $RootPath 可能有问题

      $rootPath = "$env:ProgramFiles\NetApp\SnapManager for Exchange\Report\",
      

      $env: 或 "$env:HOMEDRIVE"+"\ProgramFiles\NetApp\SnapManager for Exchange\Report"

      $env: 调用不正确..

      (或)

      $paths = ('Debug `[11EXS05`]\','Backup `[11EXS05`]\')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-04
        • 2018-08-11
        • 2016-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多