Answered by:
Write text file in a specific format using Powershell

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.AddContentCommandAdd-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.AddContentCommandAppreciate 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)
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Monday, June 10, 2019 7:02 AM
- Marked as answer by DD44P Tuesday, June 18, 2019 7:24 AM
Friday, June 7, 2019 6:25 PM
All replies
-
You must ask a specific question about code you have written and post the code in issue with the full error message.
Please carefully review the following links to set your expectation for posting in technical forums.
This Forum is for Scripting Questions Rather than script requests
Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
\_(ツ)_/
Friday, June 7, 2019 10:15 AM -
How about this?
Add-Content $FileName -Value ('#Time=' + (Get-Date))
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Monday, June 10, 2019 7:02 AM
- Marked as answer by DD44P Tuesday, June 18, 2019 7:24 AM
Friday, June 7, 2019 6:25 PM -
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
LeeJust do it.
Monday, June 10, 2019 7:03 AM