从服务器列表中获取所有共享文件夹(管理员共享除外)的总大小?

发布于 2024-08-27 13:02:49 字数 332 浏览 5 评论 0原文

我正在计算许多不同服务器上所有共享文件夹(管理员共享除外)的总大小(将所有访问的文件合并到 NAS 盒以便于备份/恢复),但在寻找解决方案时遇到了一些麻烦。

我确信这可以在 powershell 中完成,但我只是找不到正确的信息来让我继续,我目前可以吐出服务器上所有共享的列表,但不知道从这里去哪里:

$servers =@(
"server1",
"server2")

foreach($server in $servers)
{
    get-WmiObject Win32_Share -computerName $server -filter "Type = 0" 
}

I'm looking to calculate the total size of all shared folders (except admin shares) on a number of different servers (consolidating all accessed files to a NAS box for easier backup / restore) but am having a bit of trouble finding a solution.

I'm certain this could be done in powershell but I just can't find the right information to get me going, I can currently spit out a list of all shares on the servers but am not sure where to go from here:

$servers =@(
"server1",
"server2")

foreach($server in $servers)
{
    get-WmiObject Win32_Share -computerName $server -filter "Type = 0" 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高速公鹿 2024-09-03 13:02:49

我会尝试使用 Get-ChildItem 列出文件并使用 Measure-Object 来计算大小

$servers = @("server1", "server2")
$sizes = @()
foreach($server in $servers) {
  write-host "Server: $server"
  $serverSizes = @(gwmi -class Win32_Share -ComputerName $server -filter "Type = 0" | 
    % { 
      write-host " share: $($_.Name)"
      $s = gci \\$server\$($_.Name) -recurse -force | Measure-Object -Property length -Sum
      New-Object PSObject -property @{Name=$_.Name; Server=$server; TotalSize=$s.Sum } 
    })
  if ($serverSizes) {
     $totalServerSize = $serverSizes | Measure-Object -Property TotalSize -Sum
     $serverSizes += New-Object PSObject -property @{Name="__Total__"; Server=$server; TotalSize=$totalServerSize.Sum } 
     $sizes += $serverSizes
  }
}

然后您可以选择总大小,如下所示:

$sizes | 
  ? { $_.Name -eq '__Total__' } | 
  Select-Object Server,@{L='Size in MB'; E={$_.TotalSize/1mb}},@{L='Size in GB'; E={$_.TotalSize/1gb}}

I would try to use Get-ChildItem to list the files and Measure-Object to count the sizes

$servers = @("server1", "server2")
$sizes = @()
foreach($server in $servers) {
  write-host "Server: $server"
  $serverSizes = @(gwmi -class Win32_Share -ComputerName $server -filter "Type = 0" | 
    % { 
      write-host " share: $($_.Name)"
      $s = gci \\$server\$($_.Name) -recurse -force | Measure-Object -Property length -Sum
      New-Object PSObject -property @{Name=$_.Name; Server=$server; TotalSize=$s.Sum } 
    })
  if ($serverSizes) {
     $totalServerSize = $serverSizes | Measure-Object -Property TotalSize -Sum
     $serverSizes += New-Object PSObject -property @{Name="__Total__"; Server=$server; TotalSize=$totalServerSize.Sum } 
     $sizes += $serverSizes
  }
}

Then you can e.g. select the total sizes like this:

$sizes | 
  ? { $_.Name -eq '__Total__' } | 
  Select-Object Server,@{L='Size in MB'; E={$_.TotalSize/1mb}},@{L='Size in GB'; E={$_.TotalSize/1gb}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文