Help creating CSV from long txt

Juan Ignacio Blanco 1 Reputation point
2021-11-24T14:49:24.227+00:00

Hi everyone,

I need to get the information on a txt file to a csv.

The txt file has the data as:

Add-ADSyncAttributeFlowMapping  `  
-SynchronizationRule $syncRule[0] `  
-Destination 'deviceOSType' `  
-FlowType 'Expression' `  
-ValueMergeType 'Update' `  
-Expression 'IIF(InStr([operatingSystem],"Windows") > 0,"Windows",[operatingSystem])' `  
-OutVariable syncRule  
  
  
Add-ADSyncAttributeFlowMapping  `  
-SynchronizationRule $syncRule[0] `  
-Source @('ServerAd') `  
-Destination 'deviceTrustType' `  
-FlowType 'Constant' `  
-ValueMergeType 'Update' `  
-OutVariable syncRule  

as you can see, there are 2 kinds of info being sync. One with source and destination and the other one has destination and expression.
TXT file has a lot of those parraphs.

What i need is to get that info and convert it to a csv to look something like
152332-image.png

is there any possible way to do this?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,621 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,596 Reputation points
    2021-11-24T19:35:56.39+00:00

    This is a somewhat naive parser for the data provided:

    $InFile = "c:\junk\parse.txt"
    
    $rule = [ordered]@{}
    $incmdlet = $false
    $lines = Get-Content $InFile
    ForEach ($ln in $lines){
        if ($ln.Trim().Length -eq 0){
            if ($incmdlet){
                [PSCustomObject]$rule
            }
            $rule = [ordered]@{}
            $incmdlet = $false
            continue
        }
        if ($ln -like "Add-ADSyncAttributeFlowMapping*"){
            $incmdlet = $true
            continue
        }
        if ($incmdlet -and $ln.substring(0,1) -eq '-'){ # a parameter and value or a switch with a value
            if ($ln -match "^-(\w+?)\s+:?([^``]+)") {
                $rule[$matches[1]] = $matches[2].Trim().Trim("'")
            }
            elseif ($ln -match "^-(\w+?)\s+``?)") {     # a switch with no value
                $rule[$matches[1]] = ""
            }
        }
    }
    

    Note: in its present form, the code depends on an empty line at the end of the file to produce the PSCustomObject. Also, if there are parameter names that are not present in all the cmdlets you may have to provide a "-Header" parameter to any Export-CSV because the objects don't contain a uniform set of parameter names.

    0 comments No comments

  2. Limitless Technology 39,796 Reputation points
    2021-11-29T09:36:31.177+00:00

    Hi there,

    You can use the Export-Csv cmdlet to convert objects to CSV strings. Export-CSV is similar to ConvertTo-CSV , except that it saves the CSV strings to a file. The ConvertTo-CSV cmdlet has parameters to specify a delimiter other than a comma or use the current culture as the delimiter.

    A tab-separated text file could be treated as a CSV file with a tab instead of a comma as its delimiter. To convert:

    import-csv tabdelimitedfile.txt -delimiter "`t" | export-csv csvfile.csv

    This might help you in digging out more info https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-csv?view=powershell-7.2

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.