首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多变量条件输出

多变量条件输出
EN

Stack Overflow用户
提问于 2020-05-18 16:52:52
回答 1查看 35关注 0票数 0

我在“not”列中看到了输出问题,即使当DisplayName为非空时,它也将邮箱标记为“邮箱未找到”。对于前5个输出行来说,似乎还可以,直到它到达第一个‘DisplayName’为止,然后在“Not”列中用“邮箱未找到”填充每个单元格。

*if (($mb1 -eq 'No Cloud Mailbox') -and ($mb2 -eq 'No Onprem Mailbox‘){ $mb3 =’邮箱未找到‘}*

我是不是遗漏了什么?任何帮助都将不胜感激。

代码语言:javascript
复制
#========
#Get date
#========

$date = Get-Date -format dd-MM-yy

#===========================
#Setting up global variables
#===========================

$allmbadinfo = @()
$mbadinfo = @()
$users = Get-Content D:\import\allrgs.txt

#=================
#Grab mailbox info
#=================


Foreach ($user in $users ) {

      $mb1 = Get-RemoteMailbox $User

          if ($mb1 -ne $null) { $mb1 = $mb1.DisplayName }

                    else {
                          $mb1 = 'No Cloud Mailbox' }


      $mb2 = Get-Mailbox $User

           if ($mb2 -ne $null) { $mb2 = $mb2.DisplayName }

                    else {
                          $mb2 = 'No Onprem Mailbox' }


           if (($mb1 -eq 'No Cloud Mailbox') -and ($mb2 -eq 'No Onprem Mailbox')) { $mb3 = 'Mailbox Not Found' }


       $imputlist = Write-Output $user


#================================================================================================
#Create new array object and populate information from variables, add table column names and data
#================================================================================================

      $mbadinfo = New-Object PSObject
      $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Cloud' -Value $mb1
      $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Onprem' -Value $mb2
      $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Not Found' -Value $mb3
      $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Imput List' -Value $imputlist
      $allmbadinfo += $mbadinfo

      }

      #======================
      # Exporting data to csv
      #======================

      $allmbadinfo | Export-Csv D:\export\remotembxrgs-$date.csv -NoType -NoClobber
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-18 18:28:29

如果您再引入几个变量,那么代码就会变得更清晰,这样就不会重用$mb1$mb2$mb3

这方面的主要问题是,一旦变量$mb3被设置为‘邮箱未找到’,它将永远保留该值,因为您的最后一个if不说明条件不满足时它应该变成什么。

试试这个:

代码语言:javascript
复制
#================================
#Get date and read the users file
#================================

$date  = Get-Date -format dd-MM-yy
$users = Get-Content D:\import\allrgs.txt

#=================
#Grab mailbox info
#=================

$allmbadinfo = foreach ($user in $users ) {
    # test for remote mailbox
    $mb1 = Get-RemoteMailbox $user -ErrorAction SilentlyContinue
    $remote = if ($mb1) { $mb1.DisplayName } else { 'No Cloud Mailbox' }

    # test for on premise mailbox
    $mb2 = Get-Mailbox $user -ErrorAction SilentlyContinue
    $onPrem = if ($mb2) { $mb2.DisplayName } else { 'No Onprem Mailbox' }

    # if both are $null, set this to 'Mailbox Not Found'
    $noMailBox = if (!$mb1 -and !$mb2) { 'Mailbox Not Found' } else { '' }


#================================================================================================
# Create new array object and populate information from variables, add table column names and data
#================================================================================================

    # for Powershell v3.0 and newer:
    # output an object to get collected in variable $allmbadinfo
    [PsCustomObject]@{
        'Cloud'           = $remote
        'Onprem'          = $onPrem
        'Not Found'       = $noMailBox
        'Input List item' = $user
    }

    # for PowerShell versions less than 3.0:
    # $mbadinfo = New-Object PSObject
    # $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Cloud' -Value $remote
    # $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Onprem' -Value $onPrem
    # $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Not Found' -Value $noMailBox
    # $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Input List item' -Value $user

    # output this object to get collected in variable $allmbadinfo
    # $mbadinfo

}

#======================
# Exporting data to csv
#======================

$allmbadinfo | Export-Csv "D:\export\remotembxrgs-$date.csv" -NoType -NoClobber
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61874810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档