具有权限的共享文件夹中的文件大小

时间:2019-05-10 13:01:45

标签: windows powershell fileserver

我的任务是单独导出计算机上所有共享文件夹中的所有文件大小,但具有ACL和“共享”权限的系统共享除外。 具有共享和ACL权限的Treesize输出之类的东西。

我尝试了以下代码,但未显示输出中所需的内容。

任何帮助将不胜感激。

function Get-ShareSize {
    Param(
    [String[]]$ComputerName = $env:computername
    )

Begin{$objFldr = New-Object -com Scripting.FileSystemObject}

Process{
    foreach($Computer in $ComputerName){
        Get-WmiObject Win32_Share -ComputerName $Computer -Filter "not name like '%$'" | %{
            $Path = $_.Path -replace 'C:',"\\$Computer\c$"
            $Size = ($objFldr.GetFolder($Path).Size) / 1GB
            New-Object PSObject -Property @{
            Name = $_.Name
            Path = $Path
            Description = $_.Description
            Size = $Size
            }
        }
    }
}
}

Get-ShareSize -ComputerName localhost

1 个答案:

答案 0 :(得分:1)

您的代码已经看起来不错,但是..

您使用-Filter的方式是错误的,并且将$_.Path转换为UNC路径的部分也不正确。

除此之外,我们不需要Com对象(Scripting.FileSystemObject)即可获得共享的实际大小。

尝试一下

function Get-ShareSize {
    Param(
        [String[]]$ComputerName = $env:computername
    )

    foreach($Computer in $ComputerName){
        Get-WmiObject Win32_Share -ComputerName $Computer | Where-Object { $_.Name -notlike '*$' } | ForEach-Object {
            # convert the Path into a UNC pathname
            $UncPath = '\\{0}\{1}' -f $Computer, ($_.Path -replace '^([A-Z]):', '$1$')
            # get the folder size
            try {
                $Size = (Get-ChildItem $UncPath -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB
            }
            catch {
                Write-Warning "Could not get the file size for '$uncPath'"
                $Size = 0
            }
            # output the details
            [PSCustomObject]@{
                'Name'        = $_.Name
                'LocalPath'   = $_.Path
                'UNCPath'     = $UncPath
                'Description' = $_.Description
                'Size'        = '{0:N2} GB' -f $Size  # format the size to two decimals
            }
        }
    }
}

Get-ShareSize -ComputerName localhost

希望有帮助