[英]Files Sizes in shared folder with permissions
I have been tasked to export all file sizes individually in all the shared folder exists on a computer except the system shares with ACL and Shared permissions. 我的任务是单独导出计算机上所有共享文件夹中的所有文件大小,但具有ACL和“共享”权限的系统共享除外。 Something like Treesize output with the Shared and ACL permissions.
具有共享和ACL权限的Treesize输出之类的东西。
I have tried the below code but it is not showing what I need in output. 我尝试了下面的代码,但未显示输出中需要的内容。
Any help will be greatly appreciated. 任何帮助将不胜感激。
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
Your code already looks quite good, but.. 您的代码已经看起来不错,但是..
The way you use -Filter
is wrong and also the part where you convert the $_.Path
into a UNC path is not correct. 您使用
-Filter
的方式是错误的,而且将$_.Path
转换为UNC路径的部分也不正确。
Apart from that, there is no need to us a Com object ( Scripting.FileSystemObject
) to get the actual size of the share. 除此之外,我们不需要Com对象(
Scripting.FileSystemObject
)来获取共享的实际大小。
Try this 尝试这个
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
Hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.