PowerShell Could Not Find Item - Path With Spaces IOException

杀马特。学长 韩版系。学妹 提交于 2021-02-08 21:06:52

问题


# --------------------------------------------------------- 
# 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)} } 

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

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

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\'""

回答1:


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

to

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

or even forgo the call to Get-Item completely:

$path



回答2:


Might be a bit late for answer here, but, as Aaron mentioned, this is not due to spaces in the path.

If you read the documentation for Get-Item cmdlet, there is a -Force switch, which allows the cmdlet to get items that cannot otherwise be accessed, such as hidden items.

Moreover, it seems from your code that you are not expecting to pass a wildcard pattern to the cmdlet, so instead of (Get-Item $path).FullName you should use

(Get-Item -force -LiteralPath $path).FullName

That should resolve this issue.




回答3:


As suggested by TheTrowser in a comment above: The problem may be resolved if you replace the double-quotes with single quotes surrounding the file directory with spaces. This is what solved it for me.



来源:https://stackoverflow.com/questions/17572541/powershell-could-not-find-item-path-with-spaces-ioexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!