Foreach loop not functioning as expected

Welcome Forums General PowerShell Q&A Foreach loop not functioning as expected

This topic contains 2 replies, has 2 voices, and was last updated by

5 years, 5 months ago.

  • Author
  • #9303

    Points: 0
    Rank: Member


    I've been working on a script which gets Enterprise vault environment from SQL and merges it with Exchange data and finally generate a neat csv using both data sources. In near future, will add some more features to it but for now, that's what it does in nutshell.

    However, I am unable to use "foreach" loop on data collected from SQL servers and pass it for processing and merge with exchange data, I am not sure where I went wrong.

    In attached txt file, you can find the two functions I am using. First function is working as expected (Function GetEnterpriseVaultData), if I export csv files directly from there, no issues. It returns SQL tables in form of string array.

    However in second function (Function GetMailboxData), where I use foreach ($Archive in $EnterpriseVaultData), the $Archive takes complete $EnterpriseVaultData and basically remaining function doesn't works since it is expecting singular value.

    If you guys can take a look please and point me in right direction, will appreciate a lot.

    Thanks in Advance.

  • #9319

    Points: 0
    Rank: Member

    It looks like $EnterpriseVaultData will contain a DataTable object, not an array. I think what you're actually trying to accomplish is something like this:

    foreach ($Archive in $EnterpriseVaultData.Rows) {
        # ...

    Not sure if you'll be able to access columns in those rows as if they were properties of the DataRow. It's possible; PowerShell does that in a lot of other places (XML, WMI, etc), but if not, you'll need to use $Archive.Item('ColumnName')

  • #9321

    Points: 0
    Rank: Member

    Thanks Dave, will give it a shot. Meanwhile, attached is property set for $EnterpriseVaultData variable.

The topic ‘Foreach loop not functioning as expected’ is closed to new replies.