Powershell error A positional parameter cannot be found that accepts argument

enthuguy
enthuguy used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Author

Commented:
Thanks oBdA, trying it now

Author

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

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Author

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
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.

Author

Commented:
sorry, didnt work
Please check the new log :)
cfn1.zip
Most Valuable Expert 2018
Distinguished Expert 2018

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

Author

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
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Author

Commented:
Apologies, I thought you missed it or typo ...sorry
Trying now

Author

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

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Author

Commented:
Thanks, testing now

Author

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

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

Author

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

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Yes, that should work.

Author

Commented:
yes, it worked oBdA, Thanks a lot!

You have lots of patience too :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial