Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell中的输出格式_Powershell - Fatal编程技术网

powershell中的输出格式

powershell中的输出格式,powershell,Powershell,我正在尝试编写Get foldersize函数,但无法安排完整的输出。它以圆点结束 下面是脚本的详细信息 Function Get-FolderSize { [CmdletBinding(DefaultParameterSetName='FolderPath')] param ( [Parameter(Mandatory=$true,Position=0,ParameterSetName='FolderPath')] [String[]]$FolderPath, [Parameter(Ma

我正在尝试编写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
我正在尝试获取完整的文件路径。甚至我也尝试了
输出字符串宽度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

请有人建议正确的方法来打印完整的文件路径和所有没有点的输出。提前感谢:)

老实说,这不需要你担心。数据没有丢失,只是为了显示而被截断

$data = Get-FolderSize
$data.FilePath
当你直接看房子的时候,它就在那里

最终用户将决定何时查看完整输出,或根据需要设置格式:

$data | Format-Table -AutoSize

$data | Format-List
如果确实希望特别显示命令的输出,可以这样做,但这也需要创建一个新类型来应用该格式

以你为例,我真的认为这不值得努力。使用PowerShell的人往往会很快习惯返回对象的想法,以及如何使用和操作这些对象进行显示

不要在函数中使用
输出字符串
;这样做是在破坏原始对象并将输出转换为纯文本