Powershell error A positional parameter cannot be found that accepts argument

HI Powershell Experts

trying to execute a PS script as part of AWS userdata

This is how I invoke from aws cloudformation userscript.

 2-addConfigFilesPath:
  command:
    powershell.exe (Set-Content -Path C:\filepath.txt -Value "C:\Program Files\app\folder\Agility.Server.Web\web.config`nC:\Program Files\app\folder\Agility.Server.Web\web.config`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.Executor.exe.config`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.ExportService.exe.config`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.WorkerService.exe`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.StreamingService.exe`nC:\Program Files\app\folder\Transformation Server\productname.CEBPM.CPUServer.ServiceHost.exe.config")

Open in new window



Error:
2019-02-22 10:44:30,799 [INFO] Waiting 60 seconds for reboot
2019-02-22 10:45:30,849 [DEBUG] Running command 2-addConfigFilesPath
2019-02-22 10:45:30,849 [DEBUG] No test for command 2-addConfigFilesPath
2019-02-22 10:45:31,256 [ERROR] Command 2-addConfigFilesPath (powershell.exe (Set-Content -Path C:\filepath.txt -Value "C:\Program Files\app\folder\Agility.Server.Web\web.config`nC:\Program Files\app\folder\Agility.Server.Web\web.config`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.Executor.exe.config`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.ExportService.exe.config`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.WorkerService.exe`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.StreamingService.exe`nC:\Program Files\app\folder\Transformation Server\productname.CEBPM.CPUServer.ServiceHost.exe.config")) failed
2019-02-22 10:45:31,256 [DEBUG] Command 2-addConfigFilesPath output: Set-Content : A positional parameter cannot be found that accepts argument 
'Files\app\folder\Agility.Server.Web\web.config
C:\Program'.
At line:1 char:2
+ (Set-Content -Path C:\filepath.txt -Value C:\Program Files\productname\Tota ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Content], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetContentCommand

Open in new window



Please note: when I executed manually on the target windows server. it worked fine

thanks in advance.
enthuguyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try it like this:
powershell.exe -Command "'C:\Program Files\app\folder\Agility.Server.Web\web.config', 'C:\Program Files\app\folder\Agility.Server.Web\web.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.Executor.exe.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.ExportService.exe.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.WorkerService.exe', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.StreamingService.exe', 'C:\Program Files\app\folder\Transformation Server\productname.CEBPM.CPUServer.ServiceHost.exe.config' | Set-Content -Path C:\Temp\filepath.txt"

Open in new window

enthuguyAuthor Commented:
Thanks oBdA, trying it now
enthuguyAuthor Commented:
unfortunately, it still fails

2019-02-22 11:28:59,697 [ERROR] Command 2-addConfigFilesPath (powershell.exe powershell.exe Set-Content -Path C:\filepath.txt -Value "'C:\Program Files\Kofax\TotalAgility\Agility.Server.Web\web.config', 'C:\Program Files\Kofax\TotalAgility\Agility.Server.Web\web.config', 'C:\Program Files\Kofax\TotalAgility\CoreWorkerService\Agility.Server.Core.Executor.exe.config', 'C:\Program Files\Kofax\TotalAgility\CoreWorkerService\Agility.Server.Core.ExportService.exe.config', 'C:\Program Files\Kofax\TotalAgility\CoreWorkerService\Agility.Server.Core.WorkerService.exe', 'C:\Program Files\Kofax\TotalAgility\CoreWorkerService\Agility.Server.StreamingService.exe', 'C:\Program Files\Kofax\TotalAgility\Transformation Server\Kofax.CEBPM.CPUServer.ServiceHost.exe.config' | Set-Content -Path C:\Temp\filepath.txt") failed
2019-02-22 11:28:59,697 [DEBUG] Command 2-addConfigFilesPath output: Set-Content : A positional parameter cannot be found that accepts argument 
'Files\Kofax\TotalAgility\Agility.Server.Web\web.config'.

Open in new window



However, if I use below, file gets created but all in single line, where we expect it in multiple lines
 2-addConfigFilesPath:
  command:
    powershell.exe (Set-Content -Path C:\filepath.txt -Value 'C:\Program Files\app\folder\Agility.Server.Web\web.config`nC:\Program Files\app\folder\Agility.Server.Web\web.config`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.Executor.exe.config`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.ExportService.exe.config`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.WorkerService.exe`nC:\Program Files\app\folder\CoreWorkerService\Agility.Server.StreamingService.exe`nC:\Program Files\app\folder\Transformation Server\productname.CEBPM.CPUServer.ServiceHost.exe.config')

Open in new window

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

oBdACommented:
If you're using that as described in
https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html
then the syntax would need to be (basically the part inside the double quotes from the command above):
<powershell>
'C:\Program Files\app\folder\Agility.Server.Web\web.config', 'C:\Program Files\app\folder\Agility.Server.Web\web.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.Executor.exe.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.ExportService.exe.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.WorkerService.exe', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.StreamingService.exe', 'C:\Program Files\app\folder\Transformation Server\productname.CEBPM.CPUServer.ServiceHost.exe.config' | Set-Content -Path C:\Temp\filepath.txt
</powershell>

Open in new window

enthuguyAuthor Commented:
Hi OBdA,
pls check the attached ps script (only powershell portion), execution log from the target windows server. you can see how aws converts this ps script on the target server.

you could see I replace `n with newline char in step3 after above step2 create file. I think the logic what I have in step 3 is incorrect and producing different output

pls suggest :)
cfn-powershell.yaml
cfn.zip
oBdACommented:
In that yaml format, the second command would need to be
powershell.exe 'C:\Program Files\app\folder\Agility.Server.Web\web.config', 'C:\Program Files\app\folder\Agility.Server.Web\web.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.Executor.exe.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.ExportService.exe.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.WorkerService.exe', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.StreamingService.exe', 'C:\Program Files\app\folder\Transformation Server\productname.CEBPM.CPUServer.ServiceHost.exe.config' | Set-Content -Path C:\Temp\filepath.txt

Open in new window

and the third will not be required anymore.
enthuguyAuthor Commented:
sorry, didnt work
Please check the new log :)
cfn1.zip
oBdACommented:
I'd need the exact, actual, "script" file you create in order to help.
enthuguyAuthor Commented:
If I provide, I would really confuse you :)
moreover I have to strip off some code and it would it make sense to you...sorry.

have attached bit more into.

Pls note:  for testing, if I update the file manually and test rest of the scenario...they work fine.
ec2.yaml
oBdACommented:
Well, you're not entering what I posted.
You added Set-Content -Path C:\filepath.txt -Value at the beginning.
That does not belong there. I posted the complete command to use, not just the value to pass to Set-Content.
The third command will be unnecessary, because the command I posted will create CRLF separated lines.
In that yaml syntax, the complete command (Set-Content appears at the end!) needs to be
powershell.exe 'C:\Program Files\app\folder\Agility.Server.Web\web.config', 'C:\Program Files\app\folder\Agility.Server.Web\web.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.Executor.exe.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.ExportService.exe.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.WorkerService.exe', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.StreamingService.exe', 'C:\Program Files\app\folder\Transformation Server\productname.CEBPM.CPUServer.ServiceHost.exe.config' | Set-Content -Path C:\filepath.txt

Open in new window

enthuguyAuthor Commented:
Apologies, I thought you missed it or typo ...sorry
Trying now
enthuguyAuthor Commented:
I get his error

2019-02-22 13:48:22,242 [ERROR] Command 2-addConfigFilesPath (powershell.exe 'C:\Program Files\app\folder\Agility.Server.Web\web.config', 'C:\Program Files\app\folder\Agility.Server.Web\web.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.Executor.exe.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.ExportService.exe.config', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.Core.WorkerService.exe', 'C:\Program Files\app\folder\CoreWorkerService\Agility.Server.StreamingService.exe', 'C:\Program Files\app\folder\Transformation Server\productname.CEBPM.CPUServer.ServiceHost.exe.config' | Set-Content -Path C:\filepath.txt) failed
2019-02-22 13:48:22,242 [DEBUG] Command 2-addConfigFilesPath output: 'Set-Content' is not recognized as an internal or external command,

Open in new window

oBdACommented:
Then try to enclose the complete command (after the powershell.exe) inside double quotes:
powershell.exe "'C:\Program Files ...  | Set-Content -Path C:\filepath.txt"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
enthuguyAuthor Commented:
Thanks, testing now
enthuguyAuthor Commented:
I think, you did it..thanks :)

I did a mistake/typo. let me retest :)
enthuguyAuthor Commented:
You are fantastic eBdA, it worked.

one of my other logic failed during step 5 (backup), Reason, file did not exist on that server.
Current line
powershell.exe foreach($line in Get-Content C:\filepath.txt){Copy-Item $line -Destination 'C:\Backup'}

Open in new window


With your Test-Path suggestion. Is this correct?
 powershell.exe foreach($line in Get-Content C:\filepath.txt) {If (Test-Path -Path $line) {Copy-Item $line -Destination 'C:\Backup'}}

Open in new window

oBdACommented:
Yes, that should work.
enthuguyAuthor Commented:
yes, it worked oBdA, Thanks a lot!

You have lots of patience too :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.