Avatar of enthuguy
enthuguy
Flag for Australia asked on

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.
Powershell* aws-cloudformatonAWS

Avatar of undefined
Last Comment
enthuguy

8/22/2022 - Mon
oBdA

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

enthuguy

ASKER
Thanks oBdA, trying it now
enthuguy

ASKER
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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
oBdA

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

enthuguy

ASKER
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
oBdA

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
enthuguy

ASKER
sorry, didnt work
Please check the new log :)
cfn1.zip
oBdA

I'd need the exact, actual, "script" file you create in order to help.
enthuguy

ASKER
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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
oBdA

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

enthuguy

ASKER
Apologies, I thought you missed it or typo ...sorry
Trying now
enthuguy

ASKER
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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
enthuguy

ASKER
Thanks, testing now
enthuguy

ASKER
I think, you did it..thanks :)

I did a mistake/typo. let me retest :)
enthuguy

ASKER
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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
oBdA

Yes, that should work.
enthuguy

ASKER
yes, it worked oBdA, Thanks a lot!

You have lots of patience too :)