# ---------------------------------------------------------
# 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#contentPowerShell中找不到项目 - 路径用空格IOException异常
我已经玩了有和创建一个批处理文件来帮助处理日志输出这个文件等等。但是,我注意到,其中有空格的路径不会被读取。例如..Documents \我的音乐
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参数,用“”或“'等来表示它。
下面是批处理文件的一部分来执行它:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noe -command "& 'C:\test.ps1' -path "'C:\Users\MyUser\'""
如果你直接在PowerShell中运行它,你会得到什么?例如'Get-DirSize“c:\ Users \ username \ Saved Games”' –
同样的事物 - 不被识别为cmdlet的名称... – user99999991
@ user2229804'不识别为cmdlet的名称'可以是相同的'找不到项目C:\ Users \ MyUser \ Documents \ My Music'。他们是两个不同的错误。您需要在控制台中加载函数get-dirSize以使其可以调用! –