简体   繁体   中英

Enhance Get-ChildItem pipeline data

I have a PowerShell function Invoke-SqlParser (using GSP ) that will extract the list of tables and columns in a SQL script.

I'd like to generate a CSV file that contains the source document's path, in addition to the results of the parsing engine:

PS > gci '\\path\to\files' -Include *.sql -Recurse | Get-Content -raw | Invoke-SqlParser -Syntax 'oracle' | ConvertTo-Csv | Out-File ~\Desktop\Output.csv

Desired content of Output.csv :

File                        Table  Column
----                        -----  ------
\\path\to\files\script0.sql table0 column0
\\path\to\files\script0.sql table1 column1
\\path\to\files\scriptB.sql tableB columnR
\\path\to\files\scriptB.sql tableC columnQ

Is there a way to add the content from the Invoke-SqlParser function to the pipeline such that it preserves the content from the Get-ChildItem Cmdlet? Or do I need to 'insert' it into the pipeline generated by the parsing engine?

Ok, so what you linked is not a function, that's a PowerShell module. What you want is a loop, or a function. I think for your purposes a function would work great for you. This function will allow you to pipe file objects to it, and it will perform your Get-Content, and Invoke-SqlParser commands, adding the full file path to the output with the property 'File'.

Function Add-SqlParsing{
    [CmdletBinding()]
    Param(
        [Parameter(ValueFromPipelinebyPropertyName = $true)]
        [Alias('Path')]
        [String]$FullName
    )
    Invoke-SqlParser (Get-Content $FullName -Raw) | Add-Member 'File' $FullName
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM