如何忽略powershell中的foreach中的零值



跟进此问题:如何将foreach循环中的值变成总和?

我有这个powershell代码的片段:

$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 = "{0:N2}" -f ($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-Host "Removed $TotalHomeDirSizeMB MB"

我需要一种处理脚本的方法,如果用户的homeDirectory在此AD属性中为null。

我认为这将是一个嵌入式if foreach的语句,但是我无法获得if语句正常工作,我会继续遇到语法错误。

任何人都可以协助IF语句如何确保仅在homeDirectory具有值时才能运行foreach代码?

我个人喜欢进行过滤,因此您在每次通过时不必使用 if 进行评估。我发现这更容易阅读/简洁,并且还可以使代码无法朝向"厄运金字塔"。

如果循环中的所有代码仅取决于要设置的单个属性(没有它的单个属性,则您对此不感兴趣)。

$FilteredListOfUsers = $ListofUsers | ?{$_.HomeDirectory -ne $null}
foreach ($user in $FilteredListOfUsers) { #do work...}

话虽如此,第一响应者的工作同样正确。

在这里:

$TotalHomeDirSizeMB = 0
foreach ($user in $ListofUsers)
{
    if ($user.HomeDirectory) {
        # 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 = "{0:N2}" -f ($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-Host "Removed $TotalHomeDirSizeMB MB"
foreach ($user in $ListofUsers)
{
    if ($user.HomeDirectory) {
        your original code
    }
}

这将检查$user.HomeDirectory是否不是$null

相关内容

  • 没有找到相关文章

最新更新