Exchange PS and foreach

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 :slight_smile:

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 ?

(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

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

Oops … you’re right. Thanks for the correction.

Thanks for the response :slight_smile:

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 :slight_smile:

Thanks for replies :slight_smile:

Nevermind :slight_smile: Figured it out. Had mistyped :slight_smile:

Thanks for the help !!!