Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Change Powershell script get stats to get user from csv

Posted on 2014-08-10
5
Medium Priority
?
431 Views
Last Modified: 2014-08-16
Dear experts

who can help me with this one below? The input also should come from a csv with 1 column called "user"
with alias or samAccountNamenot no for user at all

Thanks
Mandy

#############################################################################
 # PS-Merge.ps1
 # Description:
 # This script combines three commands and sends the output into a CSV file
 #
 # Jonas Andersson, MCC 2011
 # http://www.testlabs.se/blog
 # Twitter @jonand82
 #############################################################################
 
$Database = @{Name="Database";Expression={ $stats.database }}
 $DisplayName = @{Name="DisplayName";Expression={ $stats.DisplayName }}
 $StorageLimitStatus = @{Name="StorageLimitStatus";Expression={ $stats.StorageLimitStatus }}
 $TotalItemSize = @{Name="MailboxSize";Expression={ $stats.TotalItemSize }}
 $itemcount = @{Name="MailboxSize";Expression={ $stats.itemcount }}
 $Company = @{Name="Company";Expression={ $user.Company}}
 $FirstName = @{Name="FirstName";Expression={ $user.FirstName}}
 $LastName = @{Name="LastName";Expression={ $user.LastName}}
 $LastLogon = @{Name="LastLogon";Expression={ $stats.LastLoggedOnUserAccount}}
 $LastLogonTime = @{Name="LastLogonTime";Expression={ $stats.LastLogonTime}}
 $UserUPN  = @{Name="UPN";Expression={$user.UserPrincipalName}}
 $Primary = @{Name="PrimarySMTP";Expression={$EmailAddress.PrimarySMTPAddress}}
 $Emails   = @{Name="Emails";Expression = { $EmailAddress.EmailAddresses | where { $_.prefixstring -ceq "smtp"}}}
 
 
$Mailboxes = Get-Mailbox -ResultSize Unlimited
 $Mailboxes | foreach {
 $user = Get-User $_
 $stats = Get-MailboxStatistics $_
 $EmailAddress = Get-Mailbox $_
 $_ | select Alias,$DisplayName,$itemcount,$TotalItemSize,$Database,$UserUPN,$Primary,$Emails
 } | Export-CSV -NoTypeInformation C:\AllMailboxesStats.csv

Open in new window

0
Comment
Question by:Mandy_
  • 3
  • 2
5 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 800 total points
ID: 40252023
Try this:

function Get-UserMailDetails {
  $Inputfile = Get-Content c:\filepath\input.csv
  foreach ($item in $inputfile) {
          if ($item -ne $null) {
		$user = get-user $item
		$mailbox = get-mailbox $user 
		$stats = get-mailboxstatistics $user
		$addr = $mailbox.EmailAddresses | where-object {$_.prefixstring -ceq 'smtp'}
		$obj = new-object psobject
		$obj | add-member noteproperty -name Alias $mailbox.alias
		$obj | add-member noteproperty -name DisplayName -value $stats.DisplayName
		$obj | add-member noteproperty -name Database -value $stats.database
		$obj | add-member noteproperty -name StorageLimitStatus -value $stats.TotalItemSize
		$obj | add-member noteproperty -name ItemCount -value $stats.itemcount
		$obj | add-member noteproperty -name Company -value $user.Company
		$obj | add-member noteproperty -name FirstName -value $user.FirstName
		$obj | add-member noteproperty -name LastName -value $user.LastName
		$obj | add-member noteproperty -name LastLogon -value $stats.lastloggedonuseraccount
		$obj | add-member noteproperty -name LastLogonTime -value $stats.lastlogontime
		$obj | add-member noteproperty -name UserUPN -value $user.userprincipalname
		$obj | add-member noteproperty -name Primary -value $mailbox.primarysmtpaddress
		$obj | add-member noteproperty -name Emails -value $addr
		write-output $obj
	  }
  }	
}

Get-UserMailDetails | Export-CSV -NoTypeInformation C:\filepath\AllMailboxesStats.csv

Open in new window

0
 
LVL 2

Author Comment

by:Mandy_
ID: 40253872
hi,

thats nice so far.  How should i create the input.csv?  Just Header "User"
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40253974
How should i create the input.csv?  Just Header "User"
From your requirements:
The input also should come from a csv with 1 column called "user"
I assumed you already had such a file?  I don't believe you need a header line though.
0
 
LVL 2

Author Closing Comment

by:Mandy_
ID: 40264669
verynice! Thank you
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40264891
Glad I could help - thanks for the grade :^)
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question