Exchange PS and foreach

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Reilstad Reilstad 1 week, 5 days ago.

  • Author
    Posts
  • #58009
    Profile photo of Reilstad
    Reilstad
    Participant

    Hi,

    I am really frustrated.

    I am trying to complete a script that give me all inactive users in AD and their current mailbox sizes in Exchange based on samaccountname.

    what i got :

    $Days=180
    $OU="Customer OU's"
    $OutputfilePath = "c:\Report\180daysincelogon_"+([datetime]::Now).tostring("yyyy-MMM-dd")+".csv"

    get-aduser -filter * -searchscope subtree -searchbase $OU -properties emailaddress,DisplayName,lastlogontimestamp,whencreated | ? {(((Get-date) – ([datetime]::FromFileTime($_.lastlogontimestamp))).TotalDays -gt $days)} | select Emailaddress,DisplayName,samaccountname,Userprincipalname,whencreated, @{Exp={([datetime]::FromFileTime($_.lastlogontimestamp))};label="Last logon time stamp"} | Sort-Object "Last logon time stamp" | export-csv $OutputfilePath -NoTypeInformation

    $csv = Import-Csv $OutputfilePath
    foreach ($line in $csv)
    {
    (get-mailboxstatistics -identity $line.samaccountname | select Totalitemsize,itemcount | export-csv $OutputfilePath -Force -Append)
    }

    I suppose this could be done in a much different and better way, but I am still learning 🙂

    What i need is to add header in csv file before the import-csv cmdlet. I know i need to add it as a property but is not able to do it.

    Is there any tips & tricks ?

  • #58015
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    (untested)

    If you want to have all information in one csv you have to export it in one chunk ... maybe like this:

    $Days=180
    $OU="Customer OU's"
    $OutputfilePath = "c:\Report\180daysincelogon_"+([datetime]::Now).tostring("yyyy-MMM-dd")+".csv"
    
    $ADUSers = get-aduser -filter * -searchscope subtree -searchbase $OU -properties emailaddress,DisplayName,lastlogontimestamp,whencreated | ? {(((Get-date) – ([datetime]::FromFileTime($_.lastlogontimestamp))).TotalDays -gt $days)} 
    foreach ($ADUser in $ADUSers) {
        $MBStats = get-mailboxstatistics -identity $ADUSer.samaccountname | select Totalitemsize,itemcount
        $OutputObj = [PSCustomObject]@{
            Emailaddress = $ADUser.Emailaddress
            DisplayName = $ADUser.DisplayName
            samaccountname = $ADUser.samaccountname
            Userprincipalname = $ADUser.Userprincipalname
            whencreated = $ADUser.whencreated
            LastLogonTimeStamp = ([datetime]::FromFileTime($ADUser.lastlogontimestamp))
            Totalitemsize = $MBStats.Totalitemsize
            itemcount = $MBStats.itemcount
        }
    }
    
    $OutputObj
    
    $OutputObj | export-csv $OutputfilePath -Force -NoTypeInformation
    
    • #58028
      Profile photo of Matt McNabb
      Matt McNabb
      Participant

      Olaf:

      The example above will only output one object, rather than the entire array, because OutputObj is being assigned inside the foreach loop. You should be able to fix this by moving the assignment directly before the foreach keyword:

      $Days=180
      $OU="Customer OU's"
      $OutputfilePath = "c:\Report\180daysincelogon_"+([datetime]::Now).tostring("yyyy-MMM-dd")+".csv"
      
      $ADUSers = get-aduser -filter * -searchscope subtree -searchbase $OU -properties emailaddress,DisplayName,lastlogontimestamp,whencreated | ? {(((Get-date) – ([datetime]::FromFileTime($_.lastlogontimestamp))).TotalDays -gt $days)} 
      $OutputObj = foreach ($ADUser in $ADUSers) {
          $MBStats = get-mailboxstatistics -identity $ADUSer.samaccountname | select Totalitemsize,itemcount
          [PSCustomObject]@{
              Emailaddress = $ADUser.Emailaddress
              DisplayName = $ADUser.DisplayName
              samaccountname = $ADUser.samaccountname
              Userprincipalname = $ADUser.Userprincipalname
              whencreated = $ADUser.whencreated
              LastLogonTimeStamp = ([datetime]::FromFileTime($ADUser.lastlogontimestamp))
              Totalitemsize = $MBStats.Totalitemsize
              itemcount = $MBStats.itemcount
          }
      }
      
      $OutputObj
      
      $OutputObj | export-csv $OutputfilePath -Force -NoTypeInformation
      
  • #58031
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Oops ... you're right. Thanks for the correction.

  • #58489
    Profile photo of Reilstad
    Reilstad
    Participant

    Thanks for the response 🙂

    When running without the $outputObj in front of the foreach loop i get one line written in the csv. When putting the $OutputObj in front the csv is empty.

    Seems the loop isnt looping 🙂

    Thanks for replies 🙂

  • #58507
    Profile photo of Reilstad
    Reilstad
    Participant

    Nevermind 🙂 Figured it out. Had mistyped 🙂

    Thanks for the help !!!

You must be logged in to reply to this topic.