RUN Powershell Script in Docker Container From Host Powershell Script - powershell

I have a powershell script in host which copy some files and starts the container.
#Copy File
docker cp "D:\addApplication.ps1" website:/inetpub/wwwroot/
#Start Container
docker start website
Write-Host 'Process has started'
#Execute Container
docker exec -ti website powershell
#Run Script
Invoke-Expression "C:\inetpub\wwwroot\addApplication.ps1"
Second last command executes fine but last command will only execute when I exit the container session and returns error(File Not Found which is because it finds that file on host)
Question: Is there anyway I can execute the command in container session from the script. Or execute any command from script in any process(confused)
Any help is appreciated.
Thanks

Do not use the -ti flags to start an interactive session, just execute the script directly via the docker exec command
docker exec website powershell -command "C:\inetpub\wwwroot\addApplication.ps1"

Related

Running an exe file in windows container using docker

I want to run an exe file on window container using Docker. My exe accepts parameters. New file gets created in predefined directory
ie:- Test.exe -f1=file1.txt -f2=file2.txt
**Output** :
Writing file file1.txt
Successfully created file file1.txt
Writing file2 file2.txt
Successfully created file file2.txt
Enjoy!!
My docker file looks like below
FROM microsoft/aspnet:3.5-windowsservercore-10.0.14393.1715
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
Copy Test.exe ./TestFolder/
WORKDIR /TestFolder
ENTRYPOINT ["Test.exe"]
CMD ["f1=file1.txt","f2=file2.txt"]
I build the image and run the container
docker build -t image1 .
docker run -it image1
Once the container runs I get the exact above output but when I login to container machine using scripts, I don't see any file created in the predefined path.
Am I missing out anything ? Is this the correct way to run exe file on windows base image?
Any advice is appreciated. Thanks
Found out that the exe was running fine and file was created.
The way I logged in to container was wrong
Use
docker exec -it containername powershell
docker run -it --entrypoint powershell imagename --> Runs new instance of image not existing

How to pass a string variable to a command in powershell?

I intend to use a powershell variable in a script to pass a string value to a command whereupon the command is executed with the parameters as defined in the string. I have been looking at this for a couple of hours now and I don't see what I am doing wrong. I have simplified as much as I can. In the hope of fresh eyes I attach an image, showing:
a. the script that I am using. It's three lines long.
$Parms = "-it amazonlinux2 /bin/sh"
Write-Output "& docker run $Parms"
& docker run $Parms
b. the PS command line where I execute the script.
PS C:\dev\gamelift\serversdk\build> ./test
c. the result of the script's execution
& docker run -it amazonlinux2 /bin/sh
unknown shorthand flag: ' ' in - amazonlinux2 /bin/sh
See 'docker run --help'.
d. the same command executed immediately at the prompt.
PS C:\dev\gamelift\serversdk\build> docker run -it amazonlinux2 /bin/sh
e. the result of the prompt command, which is correct
sh-4.2#
Image of the above
What magic is happening so that the Write-Output is not being interpreted as the command? I have tried:
a. docker run $Parms (without the &)
b. & docker run "$Parms" (in quotes)
c. $Parms = "amazonlinux2 /bin/sh" and & docker run -it $Parms (different error)
d. $Parms = "amazonlinux2" and & docker run -it $Parms /bin/sh (works)
e. escaping the hyphen (different error)
f. $Parms = "-it amazonlinux2 /bin/sh" and each of & docker run $($Parms), & docker run ${$Parms} and & docker run ${Parms}
I'm feeling a bit stupid. I can't see how the Write-Output is right but the call command is bork. It can't be that hard...
The Call operator (&) does not parse strings so "you cannot use command parameters within a string when you use the call operator". You can, however, pass an array so the following will work:
$Parms = "-it amazonlinux2 /bin/sh"
Write-Output "& docker run $Parms"
$myargs = -split $Parms
& docker run $myargs
Alternatively using Invoke-Expression (alias iex) will also do the trick e.g.:
$Parms = "-it amazonlinux2/bin/sh"
Write-Output "& docker run $Parms"
iex "docker run $Parms"
This is a frequently asked question, although not well documented. The call operator parameters work as an array:
$Parms = '-it','amazonlinux2','/bin/sh'
Write-Output "& docker run $Parms"
& docker run $Parms
I would just run docker directly.
docker -it amazonlinux2 /bin/sh

Running interactive container with power shell

I'm trying to setup an automated build container in Windows(host and guest). Right now I'm having problems executing a simple powershell inside the container. I've done the following:
Created this DockerFile:
# escape=`
FROM microsoft/windowsservercore
SHELL ["cmd", "/S", "/C"]
CMD ["powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]
Executed this build command:
docker build -t test:latest .
Started the docker with this command:
docker run test
The PowerShell prints this and the container exits:
PS C:\>
D:\repo\docker\Teste
Tried again with this command:
docker start d05ee -ai
The PowerShell prints the same output:
PS C:\>
D:\repo\docker\Teste
I wish to use the container interactively in a first moment to validate the tools I will install on it, but I'm not able to do that. I don't now which error is blocking me to do it and that is my question.
Obs1: The powershell in a windows cmd with the same parameters work fine.
Obs2: I've based my DockerFile on the one in this tutorial.
Obs3: Running this works fine:
docker run -it microsoft/windowsservercore powershell -NoLogo -ExecutionPolicy Bypass
Therefore I presume the problem is on the image generation.
you need to run your container with the -it switch. this will make you container interactive, so you can poke around
docker run -it test

running command as local service (not user)

ss.exe command -I- won't run using local service
If I run the below command as a network user it works.
"C:\Program Files\SourceSafe\win32\SS.EXE" GET $/JavaSource -R -W -GL "d:\Release\com\fitltd" -GF- -O- -I- Set d:\Release\com\fitltd as the default folder for project $/JavaSource?(Y/N)N
However if this command is called using a local service user (Jenkins service) the command stops waiting for input (you can't see the question it is waiting (see above) for unless you run the same thing using a network user).
so the output looks like: "C:\Program Files\SourceSafe\win32\SS.EXE" GET $/JavaSource -R -W -GL "d:\Release\com\fitltd" -GF- -O- -I-
I am running Windows 2008 R2.

psexec cannot kill cmd.exe & psexec.exe after execute

i had used a batch cmd (xxx.bat) to execute a psexec function as follows:
C:\psexec.exe \192.168.xxx.xx -u server1\admin -p password C:\xxx.bat
the above batch file can run successfully to remote execute file.
but i found that there are many cmd.exe and psexec.exe process in task manager, that the batch file cannot kill process after execute.
do you know how to kill cmd.exe and psexec.exe process after execute ?
thanks
Joe
I'm sure there's a better way, but personally, I do this using pskill, e.g.:
pskill.exe \\\\192.168.x.x -u user -p pass -t <imagename>
You can find the name of the executable that you want to kill using the pslist command, also in the pstools toolset.