2017-12-09 20 views
1

Get-foldersize関数を記述しようとしていますが、完全な出力を手配できません。ドットで終わる。powershellの出力形式

ここにスクリプトの詳細があります。

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

OUTPUT:

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を試して、ファイルを外そうとしました。 out-stringを試してみると、完全なファイルパスが表示されていますが、オブジェクトは新しい出力行の前に毎回印刷されます。この

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 

同様

誰かが完全なファイルパスとドットなしのすべての出力を印刷するための正しい方法を提案することができます。事前に感謝:)

答えて

1

これはあなたが心配するためのものではありません。データが欠落しているわけではなく、表示目的で切り捨てられているだけです。

$data = Get-FolderSize 
$data.FilePath 

プロパティを直接見ると、そこにあります。

エンドユーザーは、彼らが完全な出力を見たいと思ったときに決定、または必要に応じてそれをフォーマットします:

$data | Format-Table -AutoSize 

$data | Format-List 

あなたが本当にあなたのコマンドの出力は、特別に表示させたい場合は、あなたがcreate a custom format specificationこともできますが、この意志そのフォーマットを適用する新しいタイプを作成する必要もあります。

あなたの例として、私は本当にそれが努力する価値があるとは思わない。 PowerShellを使用する人は、返されるオブジェクトの概念、およびそれらのオブジェクトを表示および操作する方法に素早く慣れ親しんでいる傾向があります。

関数内にOut-Stringを使用しないでください。そうすることで元のオブジェクトを破壊し、出力を純粋なテキストに変換します。