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 ?
Olaf
November 17, 2016, 9:15am
2
(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
Olaf
November 17, 2016, 10:02am
4
Oops … you’re right. Thanks for the correction.
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
Nevermind Figured it out. Had mistyped
Thanks for the help !!!