0

基本上我要做的是从他们的网络文件夹中收集用户文件夹大小,然后将其导出到 .csv,目录结构如下所示:network:\Department\user...User's-stuff

我现在拥有的脚本获取部门文件名和用户的文件夹大小,但不是用户名(部门中的文件夹名)。至于时间戳,我不确定它是否正常工作。它的意思是在下一个部门的用户开始时制作一个时间戳,所以基本上,同一部门的所有用户都将具有相同的时间戳。

这是我到目前为止所拥有的:

$root = "network"
$container= @()
$place = "C:\temp\"
$file = "DirectoryReport.csv"

Function Get-FolderSize
{

    BEGIN{$fso = New-Object -comobject Scripting.FileSystemObject}

    PROCESS
        {
        $prevDept = (Split-Path $path -leaf)

        $path = $input.fullname

        $folder = $fso.GetFolder($path)

        $Volume = $prevDept + "-users"

        $user = $folder.name #can't figure this part out...

        $size = $folder."size(MB)"

        if ( (Split-Path $path -leaf) -ne $prevDept)
        {
            $time = Get-Date -format M/d/yyy" "HH:mm #Probably wrong too..
        } 

        return $current = [PSCustomObject]@{'Path' = $path; 'Users' = $user; 'Size(MB)' = ($size /1MB ); 'Volume' = $Volume; 'TimeStamp' = $time;} 

        }

} 

$container += gci $root -Force -Directory -EA 0 | Get-FolderSize

$container

#Creating the .csv path

$placeCSV = $place + $file

#Checks if the file already exists
if ((test-path ($placeCSV)) -eq $true)
    {
       $file = "DirectoryReport" + [string](Get-Date -format MM.d.yyy.@h.mm.sstt) + ".csv" 
       rename-item -path $placeCSV -newname $file
       $placeCSV = $place + $file
    }

#Exports the CSV file to desired folder

$container | epcsv $placeCSV -NoTypeInformation -NoClobber

但在 CSV 文件中,用户和时间戳是错误的。感谢您的任何/所有帮助

4

1 回答 1

1

这似乎真的很难做到。为什么您不只使用 Get-ChildItem 来执行此操作几乎使此脚本对我来说有点自虐,所以我将使用该 cmdlet 而不是创建一个 comobject 来执行此操作。

我有点困惑为什么你不想递归大小,但是好的,我们会走那条路。这将为您提供文件夹大小,以 MB 为单位。

#Get a listing of department folders
$Depts = GCI $root -force -Directory

#Loop through them
ForEach($Dept in $Depts){
    $Users = @()
    $Timestamp = Get-Date -Format "M/d/yyy HH:mm"

    #Loop through each user for the current department
    GCI $Dept -Directory |%{
        $Users += [PSCustomObject]@{
            User=$_.Name
            Path=$_.FullName
            "Size(MB)"=(GCI $_|Measure-Object -Sum Length|Select Sum)/1MB
            Volume="$($Dept.Name)-Users"
            TimeStamp=$Timestamp
        }
    }
}

#Rename output file if it exists
If(Test-Path "C:\Temp\DirectoryReport.csv"){
    Rename-Item "C:\Temp\DirectoryReport.csv" "DirectoryReport.$(Get-Date -format MM.d.yyy.@h.mm.sstt).csv"
}

#Output file
$Users | Export-Csv "C:\Temp\DirectoryReport.csv" -NoTypeInformation

如果您想获取每个用户文件夹中所有文件(包括子文件夹中的文件)的总大小,请将其更改"Size(MB)"=(GCI $_|Measure-Object -Sum Length|Select Sum)/1MB为递归,将其替换为"Size(MB)"=(GCI $_ -recurse|Measure-Object -Sum Length|Select Sum)/1MB,这样您就可以开始使用了。

于 2014-05-22T23:32:13.387 回答