Querying Mailboxes with a Column within a Text File

Unanswered Querying Mailboxes with a Column within a Text File

  • Tuesday, April 24, 2012 10:57 PM
     
     

    I found a similar thread about this and it got me started.  What I want to do is have the script look at the usernames in a text file containing delimited columns and run a get-casmailbox -identity "Last, First" | ft ServerName.  As you can see, I want to get a list of the user's mailbox server.  I'm able to grab the list of usernames (first column in a text file).  When I try to loop on them, I get all kinds of strange results.  I've tried various ways of ordering commands, but they don't produce what I want.

    Here is an example of my input file:

    DisplayName;Address;City;State;Zip
    Smith, Ron;123 Main St;Marysville;WA;98322
    Jones, Harry;124 Main St;Marysville;WA;98323

    And here is the hunk of code:

    $Date = Get-Date -uformat "%Y%m%d"
    $File = Import-Csv "C:\temp\userlist-$Date.txt" -Delimiter ';'
    $File | Select-Object DisplayName
    $File | foreach {
    $mbx = get-casmailbox -identity $_.DisplayName
    write-output $mbx
    }

    I can tell it's close, but I'm too much of a newbie to get it right.  Oh, and extra bonus points for some code that can grab the mailbox server name and add it as a new column on the input file.  That would be way awesome!

    Thanks in advance.

All Replies

  • Wednesday, April 25, 2012 12:20 AM
     
      Has Code

    Hi,

    First query 1 mailbox with 

    $mbx = get-casmailbox -identity "MAILBOX_NAME"
    $mbx | fl * # this will return all the properties of $mbx

    Choose the properties you want to see or export, then

    $Date = Get-Date -uformat "%Y%m%d" $File = Import-Csv "C:\temp\userlist-$Date.txt" -Delimiter ';' $File | foreach { get-casmailbox -identity $_.DisplayName | Select Property1, Property2, Property3 } | ft -a

    # you can replace "} | ft -a' with " | export c:\mycsvfile.csv -notypeinfo"




    Cyreli


    • Edited by Cyreli Wednesday, April 25, 2012 12:21 AM
    •  
  • Wednesday, April 25, 2012 2:01 AM
    Moderator
     
      Has Code

    Hi,

    To add the mailbox server name as a new column on the input file, we could achieve this with the below method:

    $File = Import-Csv "C:\temp\userlist-$Date.txt" -Delimiter ';' $NewCSVObject = @() foreach ($item in $File) { $newvalue= get-casmailbox -identity $_.DisplayName |select name $NewCSVObject += $item | Add-Member -name "mailboxserver" -value $newvalue -MemberType NoteProperty } $NewCSVObject | export-csv $File -noType

    Hope this helps.

    Best Regards,

    Yan Li


    Yan Li

    TechNet Community Support


  • Monday, April 30, 2012 10:43 PM
     
     
    Finally got back to this.  I ran it, but it didn't like the last line of the file.  Here is the error it gave:

    Export-Csv : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not supported.
    At C:\temp\GetInfo1.2.ps1:20 char:27
    + $NewCSVObject | export-csv <<<<  $File -noType
        + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ExportCsvCommand

    Any ideas?

    Thank you!

    Bill
  • Monday, April 30, 2012 11:03 PM
     
     

    OK.  I changed the last line of the script to:  $NewCSVObject | export-csv "C:\temp\outputfile.csv" -noType  The script gets farther now, but gives this error:

    Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
    At C:\temp\GetInfo1.2.ps1:22 char:27
    + $NewCSVObject | export-csv <<<<  "C:\temp\outputfile.csv" -noType
        + CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand

    I appreciate your assistance.

    Thanks!


  • Tuesday, May 01, 2012 10:49 PM
     
     

    I made a few tweaks to your code and almost have what I'm looking for.  It fails when writing out the csv file.  The data looks good as I can see it with a write-output.  Just not sure why it's not able to write out the file.  Here is the error it gives:

    Export-Csv : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not supported.
    At C:\EAS-Scripts\NewGetDeviceInfo1.2.ps1:13 char:33
    + $NewCSVObject | export-csv -Path <<<<  $File -noType -encoding "unicode"
        + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException

        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ExportCsvCommand

    And here is the code I'm using:

    $Date = Get-Date -uformat "%Y%m%d"

    $File = Import-Csv "C:\temp\devices.csv" -Delimiter ';'
    $NewCSVObject = @()
    #$File | Select-Object DisplayName
    foreach ($item in $File) 
    {
    $newvalue= get-casmailbox -identity $item.DisplayName |select servername
    write-output $newvalue 
    $NewCSVObject += $item | Add-Member -name "mailboxserver" -value $newvalue -MemberType NoteProperty
    }

    $NewCSVObject | export-csv -Path $File -noType -encoding "unicode"

    Looks like the object needs to be converted to a string or something.  Not sure how to do that, but I've done similar export-csv that work fine.

    Thanks!