locked
Write text file in a specific format using Powershell RRS feed

  • Question

  • Hi,

    I am looking to write a text file using Powershell in a very specific format.

    I have to put some head hardcoded field names and append text from database.

    Example is as below:

    ##Start
    #SysId=<This is value from db>
    #Message-type=Settlement-data
    #Time=<Getdate()>
    #ToID=<This is value from db>

    #AllValues=< 1.1 <space separated row values from a sql table>  1.41 2.12 0.12 5.1>

    For adding content after 'Time=' as Getdate() and Sysid as $FileName (Database content), I wrote this code 

    Add-Content $FileName '#Time=' + (Get-Date)

    Add-Content $FileName '#SysId=' + $FileName 

    And Got the below error:

    Add-Content : A positional parameter cannot be found that accepts argument '+'.
    At line:3 char:1
    + Add-Content $FileName '#Time=' + (Get-Date)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Add-Content], ParameterBindingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : A positional parameter cannot be found that accepts argument '+'.
    At line:5 char:1
    + Add-Content $FileName '#Id=' + $FileName
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Add-Content], ParameterBindingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddContentCommand

    Appreciate some help in appending database values in front of the hardcoded values 

    Thanks!


    • Edited by DD44P Friday, June 7, 2019 10:23 AM
    Friday, June 7, 2019 10:07 AM

Answers

  • How about this?

         Add-Content $FileName -Value ('#Time=' + (Get-Date))


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, June 7, 2019 6:25 PM

All replies