如何获得一个foreach循环中的值成一个总和? [英] How to get a value in a foreach loop into a sum?

查看:375
本文介绍了如何获得一个foreach循环中的值成一个总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 foreach 循环中有一个变量,如下面的代码片段所示,它计算用户的主文件夹大小(MB):

  $ TotalColItems = @()
foreach($用户在$ ListofUsers)
{
$ colItems =(Get-ChildItem $ user .HomeDirectory -Recurse |
Measure-Object -Property Length -Sum)
Write-Host`r`n大小:$ user.HomeDirectory
{0:N2} - f($ colItems.Sum / 1MB)+MB
$ Calculation ={0:N2}-f($ colItems.Sum / 1MB)+MB
$ TotalColItems + = $ colItems.Sum
$ user.HomeDirectory +=+ $ Calculation | Out-File $ LogFileName -Append
Remove-Item $ user.HomeDirectory -Force -Recurse -ErrorAction SilentlyContinue
Write-Host`r`n Removed HomeDirectory:$ user.HomeDirectory
Write -Host`r`n验证你想删除用户:$ user.Name -ForegroundColor Yellow -BackgroundColor Red
Write-Host`r`n
Remove-ADObject $ user.DistinguishedName
Write-Host $ TotalColItems

但是我似乎无法弄清楚如何获得所有 $ colItems 的总价值,然后我可以计算MB关闭。



我试着把这个循环:

  $ TotalColItems + = $ colItems 

但它会在每个循环中重置,所以它不会保留一个正在运行的追加总数。



如何获取 $ colItems 每个循环的变量将其值附加到一个变量,以便我可以有所有的用户目录大小的总和?

$ TotalColItems 变量初始化为一个数组 @()是数组子表达式运算符),所以它就像一个。初始化它作为一个数字,你会发现它按预期工作(重新命名的变量名称为自我解释):

$ $ p $ $ code $ TotalHomeDirSizeMB = 0
foreach($用户在$ ListofUsers)
{
#计算文件大小的总和,直接取Sum属性值
$ HomeDirSize =(Get-ChildItem $ user.HomeDirectory -Recurse | Measure-Object -Property length -sum).Sum

#以MB计算并存储大小
$ HomeDirSizeMB = $ HomeDirSize / 1MB
#添加到累计大小变量
$ TotalHomeDirSizeMB + = $ HomeDirSizeMB

#将结果写入屏幕+文件
写入主机`r`n大小:$($ user.HomeDirectory)
Write-Host({0:N2} MB-f $ HomeDirSizeMB)
$($ user.HomeDirectory)= $ HomeDirSizeMB MB Out-File $ LogFileName -Append

#删除主目录
Remove-Item $ user.HomeDirectory -Force -Recurse -ErrorAction SilentlyContinue
Write-Host`r`n已删除HomeDirectory:$ user.HomeDirectory

#等待用户验证从AD删除,抢先确认,如果验证
写主机`r`n验证你想删除用户: $ user.Name -ForegroundColor Yellow -BackgroundColor Red
if((Read-Host -Prompty for yes)-eq'y'){
Remove-ADObject $ user.DistinguishedName -Confirm:$ false


$ b $写入累积大小到屏幕
写主机删除$ TotalHomeDirSizeMB MB


I have a variable in a foreach loop as seen in the following snippet that calculates a user's home folder size in MB:

$TotalColItems = @()
foreach ($user in $ListofUsers)
{
    $colItems = (Get-ChildItem $user.HomeDirectory -Recurse |
                Measure-Object -Property Length -Sum)
    Write-Host "`r`n Size of:" $user.HomeDirectory
    "{0:N2}" -f ($colItems.Sum / 1MB) + " MB"
    $Calculation = "{0:N2}" -f ($colItems.Sum / 1MB) + " MB"
    $TotalColItems += $colItems.Sum
    $user.HomeDirectory + " = " + $Calculation | Out-File $LogFileName -Append
    Remove-Item $user.HomeDirectory -Force -Recurse -ErrorAction SilentlyContinue
    Write-Host "`r`n Removed HomeDirectory: " $user.HomeDirectory
    Write-Host "`r`n Verifying you want to remove the user: " $user.Name -ForegroundColor Yellow -BackgroundColor Red
    Write-Host "`r`n"
    Remove-ADObject $user.DistinguishedName
    Write-Host $TotalColItems

But I can't seem to figure out how to get a total value of all $colItems that I can then calculate MB off of.

I tried putting this in the loop:

$TotalColItems += $colItems

but it resets itself per loop, so it doesn't keep a running appended total.

How can I get the $colItems variable per loop to append their value to a variable so that I can have a sum total of all the user directory sizes?

解决方案

The $TotalColItems variable in your example is initialized as an array (@() is the array subexpression operator), so it acts like one. Initialize it as a number and you'll find it works as expected (renamed variable names for self-explanatoryness):

$TotalHomeDirSizeMB = 0
foreach ($user in $ListofUsers)
{
    # Calculate sum of file sizes, grab Sum property value directly
    $HomeDirSize = (Get-ChildItem $user.HomeDirectory -Recurse | Measure-Object -Property length -sum).Sum

    # Calculate and store size in MB
    $HomeDirSizeMB = $HomeDirSize / 1MB
    # Add to cumulative size variable
    $TotalHomeDirSizeMB += $HomeDirSizeMB

    # Write results to screen + file    
    Write-Host "`r`n Size of: $($user.HomeDirectory)"
    Write-Host ("{0:N2} MB" -f $HomeDirSizeMB)
    "$($user.HomeDirectory) = $HomeDirSizeMB MB" | Out-File $LogFileName -Append

    # Remove home directory
    Remove-Item $user.HomeDirectory -Force -Recurse -ErrorAction SilentlyContinue
    Write-Host "`r`n Removed HomeDirectory: " $user.HomeDirectory

    # Wait for user to verify deletion from AD, preempt Confirmation if verified
    Write-Host "`r`n Verifying you want to remove the user: " $user.Name -ForegroundColor Yellow -BackgroundColor Red
    if((Read-Host -Prompt "y for yes") -eq 'y'){
        Remove-ADObject $user.DistinguishedName -Confirm:$false
    }
}

# Write cumulative size to screen
Write-Host "Removed $TotalHomeDirSizeMB MB"

这篇关于如何获得一个foreach循环中的值成一个总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆