Exchange foreach from csv

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Deadly-Bagel Deadly-Bagel 8 months, 3 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #33577
    Profile photo of Reilstad
    Reilstad
    Participant

    Hi,

    I am stuck and it really annoys me 🙂

    I want to create an txt file based on customattribute1. Thinking of using import-csv command to maintain customer naming. The logic around foreach is all wrong. Having some trouble to wrap my head around the foreach looping.

    Can someone help ?

    What i got is :

    import-csv -path "c:\storage\customer.csv | Get-mailbox -resultsize unlimited | foreach ($customer in $customers) | where {$_.customattribute1 -eq $customer} | Get-Mailboxstatistics | select displayname,TotalItemSize,ItemCount,@{expression={$_.totalitemsize.value.ToMB()};label="Size(MB)"} | sort-object TotalItemSize | out-file c:\report\$customer.txt

    #33579
    Profile photo of Deadly-Bagel
    Deadly-Bagel
    Participant

    There are two Foreach loops in PowerShell:

    foreach ($i in $collection) { # do stuff with $i }

    $collection | Foreach-Object { # do stuff with $_ }

    You kind of have a mash of both, and I'm not quire sure what you're trying to achieve. I assume $customers is a collection you've declared elsewhere and you want to filter mailboxes to those included in it? If so, you don't even need a foreach as the pipeline is handling it for you:

    import-csv -path "c:\storage\customer.csv | 
    Get-mailbox -resultsize unlimited | 
    where {$customers -contains $_.customattribute1} | 
    Get-Mailboxstatistics | 
    select displayname,TotalItemSize,ItemCount,@{expression={$_.totalitemsize.value.ToMB()};label="Size(MB)"} | 
    sort-object TotalItemSize | 
    out-file c:\report\$customer.txt

    To use a foreach loop in this script you would replace your Where statement with

    foreach-object { if ($customers -contains $_.customattribute1) { $_ } } |

    This is saying to loop through each item, and if it's contained in $customers then send it on through the pipeline.

    To be honest I've not had any opportunity to work with PowerShell and Exchange so I can't say much on piping a CSV to Get-Mailbox, I can only assume it works.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.