Extract to csv and send
Hello, I am using the below script to get used space and number of users on my Database. --> wish to know if it would be possible somehow to get the result in a cvs file? --> Also it would be amazing if that file can be sent directly by email... I would be happy if you can help me out. I have done some test but could not manage to get that working :( # Get all the Mailbox servers and users count ForEach ($server in Get-MailboxServer) { # For each Mailbox server, get all the databases on it $strDB = Get-MailboxDatabase -Server $server # For each Database, get the information from it ForEach ($objItem in $strDB) { $intUsers = ($objitem | Get-Mailbox -ResultSize Unlimited).count # Get the size of the database file $edbfilepath = $objItem.edbfilepath $path = "`\`\" + $server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2) $strDBsize = Get-ChildItem $path $ReturnedObj = New-Object PSObject $ReturnedObj | Add-Member NoteProperty -Name "Server\StorageGroup\Database" -Value $objItem.Identity $ReturnedObj | Add-Member NoteProperty -Name "Size (GB)" -Value ("{0:n2}" -f ($strDBsize.Length/1048576KB)) $ReturnedObj | Add-Member NoteProperty -Name "Size (MB)" -Value ("{0:n2}" -f ($strDBsize.Length/1024KB)) $ReturnedObj | Add-Member NoteProperty -Name "User Count" -Value $intUsers Write-Output $ReturnedObj } } #End
January 26th, 2011 7:59am

Do something like this: ... $ReturnedObj | Add-Member NoteProperty -Name "Size (MB)" -Value ("{0:n2}" -f ($strDBsize.Length/1024KB)) $ReturnedObj | Add-Member NoteProperty -Name "User Count" -Value $intUsers $AllData += $ReturnedObj } } #Create CSV file naming based on date script is run $exportfile = "Report-" + (get-date -f MM-dd-yyyy-HHmm) + ".csv" $currentfilepath = pwd [string]$currentfilepath = $currentfilepath.path $exportfilepath = $currentfilepath + $exportfile #Puts the data into a csv and saves it to the path created above $AllData |export-csv $exportfilepath -notype -force $AllData = $AllData -Replace("@{","") $AllData = $AllData -Replace("}","`n") #To Send the csv through email $Recipient = "itguy@domain.com" $Sender = "Report <report@domain.com>" $Subject = "Database Report" $SmtpServer = "smtpserver.domain.com" Send-MailMessage -To $Recipient -From $Sender -Subject $Subject -SmtpServer $SmtpServer -BodyAsHtml -Attachments $exportfilepath #EndDJ Grijalva | MCITP: EMA 2007/2010 | www.persistentcerebro.com
Free Windows Admin Tool Kit Click here and download it now
January 26th, 2011 3:40pm

Thanks DJ Grijalva, I just don't manage to combine my script with yours :-( Shall I keep my script and add at the end yours?
January 28th, 2011 4:35am

This would be the entire script: # Get all the Mailbox servers and users count ForEach ($server in Get-MailboxServer) { # For each Mailbox server, get all the databases on it $strDB = Get-MailboxDatabase -Server $server # For each Database, get the information from it ForEach ($objItem in $strDB) { $intUsers = ($objitem | Get-Mailbox -ResultSize Unlimited).count # Get the size of the database file $edbfilepath = $objItem.edbfilepath $path = "`\`\" + $server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2) $strDBsize = Get-ChildItem $path $ReturnedObj = New-Object PSObject $ReturnedObj | Add-Member NoteProperty -Name "Server\StorageGroup\Database" -Value $objItem.Identity $ReturnedObj | Add-Member NoteProperty -Name "Size (GB)" -Value ("{0:n2}" -f ($strDBsize.Length/1048576KB)) $ReturnedObj | Add-Member NoteProperty -Name "Size (MB)" -Value ("{0:n2}" -f ($strDBsize.Length/1024KB)) $ReturnedObj | Add-Member NoteProperty -Name "User Count" -Value $intUsers $AllData += $ReturnedObj } } #Create CSV file naming based on date script is run $exportfile = "Report-" + (get-date -f MM-dd-yyyy-HHmm) + ".csv" $currentfilepath = pwd [string]$currentfilepath = $currentfilepath.path $exportfilepath = $currentfilepath + $exportfile #Puts the data into a csv and saves it to the path created above $AllData |export-csv $exportfilepath -notype -force $AllData = $AllData -Replace("@{","") $AllData = $AllData -Replace("}","`n") #To Send the csv through email $Recipient = "itguy@domain.com" $Sender = "Report <report@domain.com>" $Subject = "Database Report" $SmtpServer = "smtpserver.domain.com" Send-MailMessage -To $Recipient -From $Sender -Subject $Subject -SmtpServer $SmtpServer -BodyAsHtml -Attachments $exportfilepath #EndDJ Grijalva | MCITP: EMA 2007/2010 | www.persistentcerebro.com
Free Windows Admin Tool Kit Click here and download it now
January 28th, 2011 9:02am

Thank you! I copied and paste into a notepad file and saved it as space.ps1 file. When I run it I get those error messages: + $AllData += <<<< $ReturnedObj Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At C:\Scripts\Space.ps1:22 char:16 + $AllData += <<<< $ReturnedObj Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At C:\Scripts\Space.ps1:22 char:16 + $AllData += <<<< $ReturnedObj Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At C:\Scripts\Space.ps1:22 char:16 + $AllData += <<<< $ReturnedObj Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At C:\Scripts\Space.ps1:22 char:16 + $AllData += <<<< $ReturnedObj Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At C:\Scripts\Space.ps1:22 char:16 + $AllData += <<<< $ReturnedObj Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At C:\Scripts\Space.ps1:22 char:16 + $AllData += <<<< $ReturnedObj Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At C:\Scripts\Space.ps1:22 char:16 + $AllData += <<<< $ReturnedObj Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At C:\Scripts\Space.ps1:22 char:16 + $AllData += <<<< $ReturnedObj Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At C:\Scripts\Space.ps1:22 char:16 + $AllData += <<<< $ReturnedObj Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At C:\Scripts\Space.ps1:22 char:16 + $AllData += <<<< $ReturnedObj The term 'Send-MailMessage' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again. At C:\Space.ps1:50 char:17 + Send-MailMessage <<<< -To $Recipient -From $Sender -Subject $Subject -SmtpServer $SmtpServer -BodyAsHtml -Attachment s $exportfilepath I don't get the meaning of
January 31st, 2011 8:14am

Sorry, forgot to make $alldata an array at the beginning. The error for "send-mailmessage" is because you are not running powershell 2.0. I changed the script to work on 1.0 for the email portion. Updated below: $AllData = @() # Get all the Mailbox servers and users count ForEach ($server in Get-MailboxServer) { # For each Mailbox server, get all the databases on it $strDB = Get-MailboxDatabase -Server $server # For each Database, get the information from it ForEach ($objItem in $strDB) { $intUsers = ($objitem | Get-Mailbox -ResultSize Unlimited).count # Get the size of the database file $edbfilepath = $objItem.edbfilepath $path = "`\`\" + $server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2) $strDBsize = Get-ChildItem $path $ReturnedObj = New-Object PSObject $ReturnedObj | Add-Member NoteProperty -Name "Server\StorageGroup\Database" -Value $objItem.Identity $ReturnedObj | Add-Member NoteProperty -Name "Size (GB)" -Value ("{0:n2}" -f ($strDBsize.Length/1048576KB)) $ReturnedObj | Add-Member NoteProperty -Name "Size (MB)" -Value ("{0:n2}" -f ($strDBsize.Length/1024KB)) $ReturnedObj | Add-Member NoteProperty -Name "User Count" -Value $intUsers $AllData = $AllData + $ReturnedObj } } #Create CSV file naming based on date script is run $exportfile = "Report-" + (get-date -f MM-dd-yyyy-HHmm) + ".csv" $currentfilepath = pwd [string]$currentfilepath = $currentfilepath.path $exportfilepath = $currentfilepath + $exportfile #Puts the data into a csv and saves it to the path created above $AllData |export-csv $exportfilepath -notype -force $AllData = $AllData -Replace("@{","") $AllData = $AllData -Replace("}","`n") #To Send the csv through email function sendmail($body) { $SmtpClient = new-object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage $SmtpClient.Host = "smtpserver.domain.com" $fromAddress = new-object System.Net.Mail.MailAddress('report@domain.com', "Reports") $MailMessage.From = $FromAddress $mailmessage.To.add("itguy@domain.com") $mailmessage.Subject = "Database Report" $MailMessage.IsBodyHtml = $false $mailmessage.Attachments.Add($exportfilepath) $smtpclient.Send($mailmessage) } sendmail $AllData #EndDJ Grijalva | MCITP: EMA 2007/2010 | www.persistentcerebro.com
Free Windows Admin Tool Kit Click here and download it now
January 31st, 2011 9:45am

Whaou :-D :-D It is just perfect!! only detail: I have a question mark in the Size (mb) column whenever a space is present. E.G: I got "25?226,96" Any idea how to change that? Many thanks again!
January 31st, 2011 10:33am

On Mon, 31 Jan 2011 15:27:55 +0000, Graiggoriz wrote: > > >Whaou :-D :-D It is just perfect!! > >only detail: I have a question mark in the Size (mb) column whenever a space is present. E.G: I got "25?226,96" > >Any idea how to change that? Assuming you don't need Unicode characters to represent any of the data, try adding "-encoding ascii" to the end of the export-csv cmdlet. Whatever your locale is using as a "thousands separator" is what's showing up as a "?". It's not a "space". I'd expect it to be a simple "." but I don't know that for certain. --- Rich Matheisen MCSE+I, Exchange MVP --- Rich Matheisen MCSE+I, Exchange MVP
Free Windows Admin Tool Kit Click here and download it now
January 31st, 2011 9:37pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics