Parameter interups docker build - powershell

I've created a dockerfile and now I want to build it, but I need to run a powershell command with a parameter inside. Running the command outside the docker build, then it works fine. Unfortionatly not when I run it inside the docker build.
Dockerfile:
FROM microsoft/nanoserver COPY wrapperfiles /wrapperfiles
RUN powershell start-process -filepath "C:\wrapperfiles\wrapper.exe -s C:\wrapperfiles\tomcat-wrapper-default.conf"
EXPOSE 80
CMD ["powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]
Now when I want to build it, then it comes with the following error:
Sending build context to Docker daemon 1.664GB Step 1/5 : FROM microsoft/nanoserver ---> e2c314f76df6 Step 2/5 : COPY wrapperfiles /wrapperfiles ---> Using cache ---> 6047da8320f0 Step 3/5 : RUN powershell start-process -filepath "C:\wrapperfiles\wrapper.exe -s C:\wrapperfiles\tomcat-wrapper-default.conf" ---> Running in 0d6b47577b35 Start-Process : A parameter cannot be found that matches parameter name 's'. At line:1 char:75
+ ... filepath C:\wrapperfiles\wrapper.exe -s C:\wrapper ...
+ ~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterB indingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm ands.StartProcessCommand
The command 'cmd /S /C powershell start-process -filepath "C:\wrapperfiles\wrapper.exe -s C:\wrapperfiles\tomcat-wrapper-default.conf"' returned a non-zero code: 1
How can I get the -s parameter included in the filepath command?

How can I get the -s parameter included in the filepath command?
-s cannot be part of a filepath, unless you have a file literally called C:\wrapperfiles\wrapper.exe -s C:\wrapperfiles\tomcat-wrapper-default.conf.
Probably you want to include -s and C:\wrapperfiles\tomcat-wrapper-default.conf as arguments, so try:
RUN powershell start-process -filepath "C:\wrapperfiles\wrapper.exe" -ArgumentList "-s","C:\wrapperfiles\tomcat-wrapper-default.conf"

Related

How to start cmd from within powershell and pass a command to the cmd instance?

I am trying to create an instance of a windows cmd terminal from powershell every minute for a maximum of 8 times and get each cmd instance to run a nodejs script from there.
Here is my code so far:
For ($i=0; $i -le 8; $i++) {
start cmd.exe /k node index.js
Start-Sleep -Seconds 60
}
but I keep on getting errors:
Start-Process : A positional parameter cannot be found that accepts argument 'node'.
At C:\Users\user\Documents\x\x\build\src\start.ps1:2 char:5
+ start cmd.exe /k node index.js
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
I have already looked at this answer posted on SuperUser, however, it is not clear to me what I am doing wrong.
The second answer on this stack overflow thread seems to be doing exactly what I am trying to do, but I keep getting the above error.
Start is an alias for the Start-Process cmdlet as mentioned by #lit
Any arguments have to passed on with the -ArgumentList parameter.
start "cmd.exe" -ArgumentList "/k node index.js"

Powershell output redirection not working

I'm trying to call docker from a powershell script, but for some reason powershell prints a NativeCommandError everytime I do, even though I'm redirecting output:
PS> docker build -f .\Proj\Dockerfile . 2>&1
docker : #2 [internal] load .dockerignore
At line:1 char:1
+ docker build -f .\Proj\Dockerfile . 2>&1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (#2 [internal] load .dockerignore:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Why redirection is not working? Running powershell 5.1.
You are redirecting the error stream to the success stream (2>&1), but the success stream will still be printed to console, and error records printed as errors. To actually discard the streams, redirect to null:
docker build -f .\Proj\Dockerfile . 2>&1 | Out-Null
# or
docker build -f .\Proj\Dockerfile . 2>&1 > $null

How to use ".\" in -file in a command file

I need to execute a command file (B.cmd) on Machine B from a Powershell script (A.ps1) on Machine A. I don't want to statically specify the path
B.cmd is supposed to execute C.ps1 which is in the same folder as B.cmd
MACHINE A: A.ps1
MACHINE B: B.cmd, C.ps1 (all in same folder)
so my command file looks like this B.cmd
#echo off
powershell.exe -file ".\C.ps1" -Iterations 10
echo
echo
pause
There's an error thrown in a A.ps1 file from which I'm calling the B.cmd file
A.ps1
Invoke-Command -ComputerName $systemName -credential $credentials -ScriptBlock {Invoke-Expression -Command: "cmd.exe /c C:\Temp\Batch\Test\B.cmd"}
A.ps1 throws error:
**The argument '.\C.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.**
+ CategoryInfo : NotSpecified: (The argument '....File parameter.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : XXX
How do I make the .\ work or is there an alternative or is it wrong to use the .\ for a remote execution ?
Please forgive my ignorance anywhere as I'm very very new to PS, Thank You !
If you're able to change the ABC.cmd you could use the following
powershell.exe -file "%~dp0\XYZ.ps1"
This will get the folder the ABC.cmd script is running from. Note that %~dp0 won't work in cmd for testing you'll need to test it from within a script.
So don't wrap a batch file around it. A waste of time and confuses the issue.
invoke-command -comp $computername -filepath C:\Temp\Batch\Test\XYZ.ps1
If you need to run something 10 times, do it in a loop inside your PS script.

PowerShell Start-Process -redirectStandardOutput throws: The system cannot find the file specified

In a PowerShell window I am executing:
Start-Process XXXXX.exe -ArgumentList "some valid arguments" -wait
-redirectStandardOutput "D:\\test1.txt"
And getting:
Start-Process : This command cannot be executed due to the error: The system cannot find the file specified.
At line:1 char:14
+ Start-Process <<<< XXXXX.exe -ArgumentList "some valid arguments here" -redirectStandardOutput "D:\\test1.txt"
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
I have checked that the start-process works as expectedif I omit the -redirectStandardOutput argument (it does).
The test1.txt is a NEW file, not trying to append.
The suprising thing is that the test1.txt file on D:\ is created when I run the line, it just remains empty.
Has anyone any idea what is happening here?
Thanks.
EDIT
I discovered that if I run:
Start-Process XXXXX.exe -ArgumentList "some valid arguments" -wait
-redirectStandardOutput "D:\\test1.txt"
it fails (as originally posted)
If I run:
Start-Process XXXXX.exe -ArgumentList "some valid arguments" -wait
it works fine but doesnt save the console output to a file
and if I run
Start-Process .\XXXXX.exe -ArgumentList "some valid arguments" -wait
-redirectStandardOutput "D:\\test1.txt"
It works pretty much as expected.
So why do I need to specify the path when I am using the redirection but when I am not it runs happily?
EDIT
To recapitulate the problem; there appears to be some inconsistancy regarding the requirement that scripts/exes in the current directory require a ./ prefix to be allowed to run. When I am not redirecting the output the ./ is NOT required. Anyone know if this is expected behavior?
RichyRoo,
The issue you're seeing is most likely because your XXXXX.exe location isn't registered inside your PATH environment variable.
If you try to run your command using an application registered inside one of the folders defined inside your PATH environment variable, it should work without the trailing .\
ie:
Start-Process cmd.exe -ArgumentList "/c ping 127.0.0.1" -redirectStandardOutput "D:\ABC\test.txt" -Wait
I tried a 2nd example using a sample batch file and it doesn't work without prefixing the path by .\ - It replicates your behavior.
So it works on your end by qualifying your .exe location by prefixing your xxxxx.exe with .\ as I assume your current directory in your shell is your .exe location.
I haven't looked up why current path isn't being looked at the command execution level though.

Powershell execute commandline locally

Powershell script executes commandline on local machine
$j = "remote_machine"
$comp = "\\"+$j
$command = 'D:\PSTools\PsExec.exe $comp -u Administrator -p plaintextpassword -accepteula powershell.exe c:\share\script.ps1'
Invoke-Expression "& $command"
This works, but it outputs following
PsExec.exe : At line:1 char:1
+ & D:\PSTools\PsExec.exe $comp -u Administrator -p plaintextpassword -accepteula powersh ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError PsExec v2.0 - Execute processes remotely Copyright (C) 2001-2013 Mark Russinovich
Sysinternals - www.sysinternals.com Connecting to
remote_machine...Starting PSEXESVC service on
remote_machine...Connecting with PsExec service on
remote_machine...Starting powershell.exe on remote_machine...
powershell.exe exited on remote_machine with error code 0.
How to fix?
After re-reading your question I see that this is the standard PSExec vs PowerShell issue that has been seen and discussed before. This is due to the fact that PSExec outputs its header lines to the stderr (Standard Error) stream for some of its text. The execution works fine, and it does show an exit code of 0 indicating that there is not actually an error.
This issue is only evident in PowerShell ISE, not the standard PowerShell console (unless you redirect StdErr to StdOut with PSExec <command & args> 2>&1 or something similar). To work around this, if you are going to run the script in the ISE, you can use Start-Process's -RedirectStandardError argument, or redirect StdErr through other means.