PowerShell 找不到项目 - 带有空格的路径 IOException [英] PowerShell Could Not Find Item - Path With Spaces IOException

查看:68
本文介绍了PowerShell 找不到项目 - 带有空格的路径 IOException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

# --------------------------------------------------------- 
# ScriptingGamesBeginnerEvent8_PS1.ps1 
# ed wilson, msft 8/21/2009 
# PS1 version of HSG-08-19-09 http://bit.ly/1d8Rww 
# 
# --------------------------------------------------------- 
Param( 
 [string]$path = 'C:\', 
 [int]$first = 50 
)# end param 
# *** Function Here *** 

function Get-DirSize ($path){ 

  BEGIN {} 

  PROCESS{ 
    $size = 0 
    $folders = @() 

    foreach ($file in (Get-ChildItem $path -Force -ea SilentlyContinue)) { 
      if ($file.PSIsContainer) { 
        $subfolders = @(Get-DirSize $file.FullName) 
        $size += $subfolders[-1].Size 
        $folders += $subfolders 
      } else { 
        $size += $file.Length 
      } 
    } 

    $object = New-Object -TypeName PSObject 
    $object | Add-Member -MemberType NoteProperty -Name Folder -Value (Get-Item $path).fullname
    $object | Add-Member -MemberType NoteProperty -Name Size -Value $size 
    $folders += $object 
    Write-Output $folders 
  } 

  END {} 
} # end function Get-DirSize 

Function Get-FormattedNumber($size) 
{ 
  IF($size -ge 1GB) 
   { 
      "{0:n2}" -f  ($size / 1GB) + " GigaBytes" 
   } 
 ELSEIF($size -ge 1MB) 
    { 
      "{0:n2}" -f  ($size / 1MB) + " MegaBytes" 
    } 
 ELSE 
    { 
      "{0:n2}" -f  ($size / 1KB) + " KiloBytes" 
    } 
} #end function Get-FormattedNumber 

 # *** Entry Point to Script *** 

 if(-not(Test-Path -Path $path))  
   {  
     Write-Host -ForegroundColor red "Unable to locate $path"  
     Help $MyInvocation.InvocationName -full 
     exit  
   } 
 Get-DirSize -path $path |  
 Sort-Object -Property size -Descending |  
 Select-Object -Property folder, size -First $first | 
 Format-Table -Property Folder,  
  @{ Label="Size of Folder" ; Expression = {Get-FormattedNumber($_.size)} } 

所以我有这个脚本,我从http://gallery.technet.microsoft.com/scriptcenter/36bf0988-867f-45be-92c0-f9b24bd766fb#content

So I have this script which I got from http://gallery.technet.microsoft.com/scriptcenter/36bf0988-867f-45be-92c0-f9b24bd766fb#content

我一直在玩弄它并创建了一个批处理文件来帮助处理此文件的日志输出等.但是,我注意到其中带有空格的路径不会被读取.例如..Documents\My Music

I've been playing around with it and created a batch file to help handle the log output of this file and such. However, I'm noticing that paths with spaces in them don't get read. For example ..Documents\My Music

    Get-Item : Could not find item C:\Users\MyUser\Documents\My Music.
    At C:\test.ps1:32 char:80
    +     $object | Add-Member -MemberType NoteProperty -Name Folder -Value (Get-It
    em <<<<  $path).fullname
+ CategoryInfo          : ObjectNotFound: (C:\Users\MyUser\Documents\My
Music:String) [Get-Item], IOException
 + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetIt
emCommand

在代码的 TechNet 页面上,有人提出了问题,但没有给出解决方案.我不知道如何在这里修复它.我已经使用了 $path 参数,将它包围在"或""等中.

On the TechNet page for the code, someone brings the issue up but no solution is given. I'm not sure how to fix it here. I've played with the $path argument, surrounding it in " " or ' ' and such.

这是执行它的批处理文件的一部分:

Here is part of the batch file to execute it:

  C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noe -command "&       'C:\test.ps1' -path "'C:\Users\MyUser\'""

推荐答案

不是路径中的空格.如果是,错误会说找不到路径 C:\Users\MyUser\Documents\My.Get-ChildItemGet-Item 的行为……很奇怪……对于某些文件/目录,返回的错误就像您看到的一样.这就是 Get-ChildItem 有一个 -ErrorAction SilentlyContinue 参数的原因.我会将相同的内容添加到对 Get-Item 的调用中,即更改

It's not the spaces in the path. If it was, the error would say path C:\Users\MyUser\Documents\My couldn't be found. Get-ChildItem and Get-Item behave... strangely... with certain files/directories, returning errors like you're seeing. That's why Get-ChildItem has an -ErrorAction SilentlyContinue parameter on it. I would add the same to the call to Get-Item, i.e. change

(Get-Item $path).FullName

(Get-Item $path -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName

甚至完全放弃对Get-Item的调用:

or even forgo the call to Get-Item completely:

$path

这篇关于PowerShell 找不到项目 - 带有空格的路径 IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆