【问题标题】:Files Sizes in shared folder with permissions具有权限的共享文件夹中的文件大小
【发布时间】:2019-09-28 09:40:46
【问题描述】:

我的任务是单独导出计算机上存在的所有共享文件夹中的所有文件大小,但具有 ACL 和共享权限的系统共享除外。 具有 Shared 和 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

【问题讨论】:

    标签: windows powershell fileserver


    【解决方案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
    

    希望有帮助

    【讨论】:

    • 感谢您的回复,西奥。我想要的是这些共享文件夹中的文件大小,而不是文件夹的总大小以及共享和 NTFS 文件夹权限
    猜你喜欢
    • 1970-01-01
    • 2022-11-22
    • 2023-03-16
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多