Solved

way to query the result set of invoke-sql?

Posted on 2013-12-26
3
386 Views
Last Modified: 2013-12-30
I have  a result set from invoke-sql that I've modified so that I no longer need any record that has a field where $CSV.Path="IGNORE". I want to somehow filter this result set so that the records that contain Path="IGNORE" are not exported to a CSV file.
I was wondering if there was a way to invoke-sql on the modified $CSV or maybe a way to filter the ignored records from being exported? Any other ideas?
thanks!
I've tried .REMOVE and .DELETE, but it seems to error saying $CSV is a fixed object and somebody else told me $CSV is meant to be read-only even thought I can modified the data it returns.

#Read the SQL table
$CSV = Invoke-Sqlcmd -Query $Query
#modify the data and set $CSV.Path to "IGNORE" for obsolete records
...
#export the results to a CSV file
#Need some way to delete, or filter out, records set to be ignored.
$CSV | export-csv -path $sf -NoTypeInformation
0
Comment
Question by:johnj_01201
  • 2
3 Comments
 
LVL 1

Author Comment

by:johnj_01201
ID: 39740161
Powershell 3+

I was able to find a workaround on the following web page, but I would still like to do before or while exporting the file the first time.
The work around is to export the file with the field E3Path="IGNORE", use Import-CSV with a where-object, and then export-csv to save the results again.
$CSV | export-csv -path $sf -NoTypeInformation
$tempC = Import-csv -path $sf | Where-Object {$_.E3Path -ne 'IGNORE'}
$tempC | export-csv -path $sf -NoTypeInformation


http://www.powershellmagazine.com/2012/12/04/skipping-empty-csv-objects/

COOL! Now I can programmatically delete records that I flag from invoke-sql results!!!
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39745333
Doesn't this work?
$CSV | ? { $_.E3Path -ne 'IGNORE' } |  export-csv -path $sf -NoType

Open in new window

BTW - path or E3Path ?
0
 
LVL 1

Author Closing Comment

by:johnj_01201
ID: 39746195
Perfect! Thanks!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now