目前有以下脚本将一些文件数据输出到报告中。文件长度以字节为单位,并想知道如何在输出到数组之前将其转换为MB。
$arr = @()
gci C:\stuff -recurse | ? {$_.PSIsContainer -eq $False} | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Length $_.Length
$obj | Add-Member NoteProperty created $_.creationtime
$obj | Add-Member NoteProperty Access $_.LastAccessTime
$obj | Add-Member NoteProperty LastWritten $_.LastWriteTime
$obj | Add-Member NoteProperty Extension $_.Extension
$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
$arr += $obj
}
$arr | Export-CSV -notypeinformation "c:\files.csv"
答案 0 :(得分:1)
转换为MB时,只需在其周围加上括号:
$obj | Add-Member NoteProperty Length ($_.Length/1MB)
或者更有用:
$obj | Add-Member NoteProperty MB ("{0:N3}" -f ($_.Length/1MB))
仅显示该点之后的前三位数。