我正在尝试编写Get-foldersize函数,但我无法安排完整的输出。最后是圆点。
以下是脚本详细信息。
Function Get-FolderSize
{
[CmdletBinding(DefaultParameterSetName='FolderPath')]
param
(
[Parameter(Mandatory=$true,Position=0,ParameterSetName='FolderPath')]
[String[]]$FolderPath,
[Parameter(Mandatory=$false,Position=1,ParameterSetName='FolderPath')]
[String]$graterthan,
[Parameter(Mandatory=$false,Position=2,ParameterSetName='FolderPath')]
[switch]$Recurse
)
Begin
{
#$graterthan and $ZeroSizeFolders cannot be used together
#Convert the size specified by Greaterhan parameter to Bytes
$size = 1000000000 * $graterthan
}
Process {#Check whether user has access to the folders.
Try {
Write-Host "Performing initial tasks, please wait... " -ForegroundColor Magenta
$subfolders = If ($Recurse) {Get-ChildItem $FolderPath -Recurse -ErrorAction SilentlyContinue }
Else {Get-ChildItem $FolderPath -ErrorAction SilentlyContinue }
}
Catch [exception]{}
#Calculate folder size
If ($subfolders)
{
Write-Host "Calculating size of folders in $FolderPath. This may take sometime, please wait... " -ForegroundColor Magenta
$Items = $subfolders | Where-Object {$_.PSIsContainer -eq $TRUE -and `
@(Get-ChildItem -LiteralPath $_.Fullname -Recurse -ErrorAction SilentlyContinue | Where-Object {!$_.PSIsContainer}).Length -gt '0'}}
ForEach ($i in $Items)
{
$subFolders =
If ($graterthan)
{Get-ChildItem -Path $i.FullName -Recurse | Measure-Object -sum Length | Where-Object {$_.Sum -ge $size -and $_.Sum -gt 1000000 } }
Else
{Get-ChildItem -Path $i.FullName -Recurse | Measure-Object -sum Length | Where-Object {$_.Sum -gt 1000000 }}
#Return only values not equal to 0
ForEach ($subFolder in $subFolders) {
#If folder is less than or equal to 1GB, display in MB, If above 1GB, display in GB
$si = If (($subFolder.Sum -ge 1000000000) ) {"{0:N2}" -f ($subFolder.Sum / 1GB) + " GB"}
ElseIf (($subFolder.Sum -lt 1000000000) ) {"{0:N0}" -f ($subFolder.Sum / 1MB) + " MB"}
$Object = New-Object psobject -pro @{
'Folder Name' = $i.Name
'Size' = $si
'Full Path' = $i.FullName
}
[array]$space = $Object
$Object | Select-Object 'Folder Name', 'Full Path',Size
}
}
}
End {
Write-Host "Task completed...if nothing is displayed:
you may not have access to the path specified or
all folders are less than 1 MB" -ForegroundColor Cyan
}
}
输出:
Name Created **FilePath** SizeMB
---- ------- -------- ------
Microsoft Analysis Services 3/13/2017 11:31:16 PM C:\Program Files\Microsoft An... 102.14
Microsoft DNX 3/16/2017 3:17:02 PM C:\Program Files\Microsoft DNX 0.08
Microsoft Help Viewer 3/20/2017 3:31:30 PM C:\Program Files\Microsoft He... 55.89
Microsoft IT Diagnostics U... 3/17/2017 5:36:08 PM C:\Program Files\Microsoft IT... 0.09
我正在尝试获取完整的文件路径。甚至我试过out-string -width 300
并尝试外包。当我尝试输出字符串时,它显示完整的文件路径,但每次在新输出行之前打印对象。
喜欢这个
Name Created FilePath SizeMB
---- ------- -------- ------
Program Files (x86) 9/29/2017 6:46:33 AM C:\Program Files (x86) 10616.26
Name Created FilePath SizeMB
---- ------- -------- ------
TURBOC3 12/2/2017 4:57:56 AM C:\TURBOC3 1
请有人建议正确的方法来打印完整的文件路径和所有没有点的输出。在此先感谢:)
答案 0 :(得分:1)
说实话,这不是你担心的。数据不会丢失,只是为了显示目的而被截断。
public void capture(View v){
captureImage();
}
当你直接看房子时,就在那里。
最终用户将决定何时查看完整输出,或根据需要对其进行格式化:
$data = Get-FolderSize
$data.FilePath
如果您真的希望特别显示命令的输出,可以create a custom format specification,但这也需要您创建一个新类型以应用该格式。
对于你的例子,我真的不认为这是值得的。使用PowerShell的人倾向于快速习惯返回对象的想法,以及如何使用和操纵这些对象进行显示。
不要在你的功能中使用$data | Format-Table -AutoSize
$data | Format-List
;通过这样做,您正在销毁原始对象并将输出转换为纯文本。