header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Variable conversion from import-csv not converting to.. variable
Last Post 28 Jul 2010 12:08 AM by George Howarth. 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
MitsuUser is Offline
New Member
New Member
Posts:19
Avatar

--
27 Jul 2010 01:01 AM
    Hi all,

    I have a bit of a tricky one that I've been working on all morning and for the life of me I can't figure this one out.  Hope someone out there can tell me to write .totring at the end of a command and make it all magically work :)

    I have 2 csv files that I'm pulling data from.
    The first file - DistroGroups.csv contains two columns -
    GroupNames,SharePointFilters
    Stores ALL, ($_.'Banner Type_ID.Name' -eq 'Diegos' -and $_.'Banner_ID.Name' -eq 'Bannered')

    The second - Stores.csv contains everything I'm look up.  It's a huge list, but condensed down it's something like:
    Banner Type_ID.Name, Banner_ID.Name
    Diegos, Bannered

    What I'm trying to do is use SharePointFilters to do a where-object on stores.csv .  Essentially, when I manually input the data from the csv file into a command, it runs fine, but when I use a variable $_.'SharepointFilters' it never works, or gives me back everything ignoring the filter.  I've tried taking off the ('s and )'s, tried importing and exporting the distrogroups csv file, just for the sake of it, but it never works.  I've also tried assigning $_.sharepointfilters to it's own variable first off.  When I merely do a write-host on sharepointfilters they both appear exactly the same (manually entered and from the csv).

    Code is below, and the output is essentially empty unless I manually do it.  I'm wondering if a) there's an issue using pipeline input as variables (do they go in as text only?), or b) if I need to convert it into something else first.

    $QueryStores = import-csv $SPWork\Stores.csv
    $QueryDG = Import-Csv C:\Scripts\DistroGroups.csv
    $QueryDG | ForEach-Object {
        $SPFilter = $_.'SharePointFilter'
        $QueryStores | where-object {($_.'Banner Type_ID.Name' -eq 'Diegos' -and $_.'Banner_ID.Name' -eq 'Bannered')} | export-csv ($SPWork + "\Stores-Match-BanneredVIC2P" + ".csv") # This DOES work
        $QueryStores | where-object {$_.'SharepointFilter'} | export-csv spfilters.csv # this doesn't work
        $QueryStores | where-object {$SPFilter} | export-csv spfilters.csv # this doesn't work
        }

    Thanks for your help,
    Josh
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    27 Jul 2010 02:05 AM

    I don't think this is correct:

    GroupNames,SharePointFilters
    Stores ALL, ($_.'Banner Type_ID.Name' -eq 'Diegos' -and $_.'Banner_ID.Name' -eq 'Bannered')

    I managed to get this far:

    $stores = Import-CSV -Path "$SPWork\Stores.csv"
    $distroGroups = Import-CSV -Path "C:\Scripts\DistroGroups.csv"
    $distroGroups | ForEach-Object {
        $spFilter = $_.SharePointFilters
        $stores | Where-Object { $spFilter <#-eq somecondition #> } | Export-CSV -Path "spfilters.csv"
    }

    MitsuUser is Offline
    New Member
    New Member
    Posts:19
    Avatar

    --
    27 Jul 2010 05:48 AM
    GW,

    Thanks for the reply, I've tested yours and all looks like it works ok, until we get some more records in there.  Try this one. 
    distrogroups.csv
    GroupName,SharePointFilters
    Stores Bannered,($_.'Banner Type_ID.Name' -eq 'Diegos' -and $_.'Banner_ID.Name' -eq 'Bannered')
    Stores Non-Bannered,($_.'Banner Type_ID.Name' -eq 'Diegos' -and $_.'Banner_ID.Name' -eq 'Non-Bannered')

    stores.csv
    Banner Type_ID.Name,Banner_ID.Name
    Diegos,Bannered
    Diegos,Non-Bannered

    Script:
    $stores = Import-CSV -Path C:\Temp\Stores.csv
    $distroGroups = Import-CSV -Path C:\Temp\DistroGroups.csv
    $distroGroups | ForEach-Object {
        $spFilter = $_.SharePointFilters
        $spGroup = $_.'GroupName'
        $stores | Where-Object {$spFilter} | Export-CSV -Path ("Filtered-" + $spGroup + ".csv")
    }

        $stores | Where-Object {($_.'Banner Type_ID.Name' -eq 'Diegos' -and $_.'Banner_ID.Name' -eq 'Bannered')} | Export-CSV -Path "FilteredManual-Bannered.csv"
        $stores | Where-Object {($_.'Banner Type_ID.Name' -eq 'Diegos' -and $_.'Banner_ID.Name' -eq 'Non-Bannered')} | Export-CSV -Path "FilteredManual-Non-Bannered.csv"

    Now have a look at the output files - you'll notice that the Filtered-* files have the same number of records in there, but each time we're asking for different results - one asks for non-bannered, the other for bannered.  When we manually ask for it in the lower part of the script, it returns the right results.

    I should have had a larger sample size in my first post as this was also happening to me on the real data - all queries gave the same results, unless I manually input them.

    Thanks,
    Josh

    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    27 Jul 2010 07:13 AM
    Ohhhhhh I get you now, you're trying to use SharePointFilters as a predicate.

    $stores = Import-CSV -Path C:\Temp\Stores.csv
    $distroGroups = Import-CSV -Path C:\Temp\DistroGroups.csv
    $distroGroups | ForEach-Object {
        $spFilter = $_.SharePointFilters
        $spGroup = $_.GroupName
        $stores | Where-Object { Invoke-Expression -Command $spFilter } | Export-CSV -Path ("C:\Temp\Filtered-" + $spGroup + ".csv") -NoTypeInformation
    }

    You need to use Invoke-Expression so that the string is executed as an expression.
    MitsuUser is Offline
    New Member
    New Member
    Posts:19
    Avatar

    --
    27 Jul 2010 04:10 PM
    GW, I'm glad someone out there knows what I'm trying to do..

    I've just tested that command in with the actual script, and it's all now running fine.  Thankyou for your help!  Just a sign-out question though, was the $SPFilter coming through as a text value and thus being ignored by where-object ?

    Thanks again,
    Josh.
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    28 Jul 2010 12:08 AM
    To be exact, it wasn't being ignored, the value just wasn't being evaluated against anything, so therefore the default value of false was returned.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer