In my DOCKERFILE I'm running some PowerShell script which creates a custom event log (on Windows Server), then writes an entry. [I should add that I'm currently doing this only for debug purposes.]
The script/command which creates the event log appear to execute without any problems, but an exception is thrown when writing to the log using...
RUN powershell.exe -command Write-EventLog -LogName "my_log_name" -Source "my_source" -Message "EventLog created by DOCKERFILE." -Category 0 -EventID 0 -EntryType Information
The following exception is thrown...
Write-EventLog : A positional parameter cannot be found that accepts argument
'created'.
At line:1 char:1
+ Write-EventLog -LogName my_log_name -Source my_source -EntryType Messag ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Write-EventLog], Parameter
BindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
.Commands.WriteEventLogCommand
I also tried wrapping "EventLog created by DOCKERFILE." in parentheses but then it complained about the word by.
Why does it seem unable to parse "EventLog created by DOCKERFILE." as a string argument, but is instead parsing the words within the string?
UPDATE
Even if I wrap the command in double-quotes and the individual strings in single quotes, I get the same error.
UPDATE 2
Removing the quotes and escaping the spaces doesn't work either.
You need to use ` to escape the spaces:
RUN powershell.exe -command Write-EventLog -LogName my_log_name -Source my_source -Message EventLog` created` by` DOCKERFILE. -Category 0 -EventID 0 -EntryType Information
See: https://docs.docker.com/engine/reference/builder/#escape
This works for me from cmd (except for the unknown source). I'm not sure if docker has issues with single quotes. Double quotes are special in cmd. Note that new-winevent has replaced write-eventlog.
powershell write-eventLog my_log_name my_source 0 information 'EventLog created by DOCKERFILE.' -category 0
Related
I want to create a file in C drive while building docker image and using command as below
RUN mkdir "C:\Program Files\Microsoft Passport RPS"
but it throws error:
Step 6/6 : RUN mkdir "C:\Program Files\Microsoft Passport RPS"
---> Running in ab58c6f2948d
[91mmkdir : A positional parameter cannot be found that accepts argument 'Files\Microsoft'.
At line:1 char:76
+ ... e = 'SilentlyContinue'; mkdir C:\Program Files\Microsoft Passport RPS
[0m[91m+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[0m[91m + CategoryInfo : InvalidArgument: (:) [mkdir], ParentContainsErro
[0m[91m rRecordException
+ FullyQualifiedErrorId : PositionalParameterNotFound,mkdir
Tried above command with forward as well as back slash.
I have tried multiple other command as below:
ENV PATH_WITH_SPACE "C:/Program Files/Microsoft Passport RPS"
RUN mkdir $[PATH_WITH_SPACE]
RUN mkdir ["C:\Program Files\Microsoft Passport RPS"]
Can you please help me with appropriate command?
Came across same problem. None of the answers worked for me.
I finally got it working by escaping space with `
RUN mkdir "C:\Program` Files\Microsoft` Passport` RPS"
COPY . "C:\Program` Files\Microsoft` Passport` RPS"
Another approach is to use Shell, and declare escape explictly
While the JSON form is unambiguous and does not use the un-necessary cmd.exe, it does require more verbosity through double-quoting and escaping. The alternate mechanism is to use the SHELL instruction and the shell form, making a more natural syntax for Windows users, especially when combined with the escape parser directive
# escape=`
FROM microsoft/nanoserver
SHELL ["powershell","-command"]
RUN New-Item -ItemType Directory C:\Example
ADD Execute-MyCmdlet.ps1 c:\example\
RUN c:\example\Execute-MyCmdlet -sample 'hello world'
You have to escape the space, some like:
RUN mkdir "C:\Program Files\Microsoft\ Passport\ RPS"
Or using the JSON format:
RUN ["mkdir", "C:\\Program Files\\Microsoft\ Passport\ RPS"]
note: using JSON format is necessary to escape backslashes. This is particularly relevant on Windows where the backslash is the path separator.
We can use PowerShell command to create folder with spaces.
Try this:
RUN powershell -Command New-Item -Path 'C:\Program Files\Microsoft Passport RPS' -ItemType Directory
It Works !!!
Normal way to open the command-prompt in tmux is prefix + :. I want to bind the sequence prefix + ; to open the command prompt. I am too lazy to hit the shift key.
When I put this in my tmux.conf: bind-key ; command-prompt, I get this error: /Users/skilbjo/.tmux.conf:19: usage: bind-key [-cnr] [-t mode-table] [-T key-table] key command [arguments]
which is funny, because when I do prefix + ? (alias for tmux list-keys), this is listed: bind-key -T prefix : command-prompt. How does this sorcery work? I even tried bind-key -T prefix ; command-prompt to no avail, same error message
tmux uses semicolon as a command separator.
From the tmux man page:
Multiple commands may be specified together as part of a command sequence. Each command should be separated by spaces and a semicolon; commands are executed sequentially from left to right and lines ending with a backslash continue on to the next line, except when escaped by another backslash. A literal semicolon may be included by escaping it with a backslash (for example, when specifying a command sequence to bind-key).
What you'll want to do is:
unbind-key \;
bind-key \; command-prompt
I am writing a Lua script to manage Virtualbox on windows.
It seems that multiple double quotes are not parsed correctly. I am using the following function to implement this:
--Get output from an OS command - http://stackoverflow.com/questions/132397/get-back-the-output-of-os-execute-in-lua
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
This code works so long an the machine name doesn't have a space, but machines can have spaces so I have to support them:
local command = '"\\Program Files\\Oracle\\VirtualBox\\VBoxManage\" showvminfo '..key
The following code does not work at all but it does give the correct format of the command to the log file so the syntax should be correct:
local command = '"\\Program Files\\Oracle\\VirtualBox\\VBoxManage\" showvminfo "'..key..'"'
logger:write("[",os.date("%Y-%m-%d %H:%M:%S"),"] Command: ",command,"\n")
vmStateRaw = os.capture(command, "raw")
Log file entry:
[2014-12-06 16:09:18] Command: "\Program Files\Oracle\VirtualBox\VBoxManage" showvminfo "Cerium"
Interpreter output:
'\Program' is not recognized as an internal or external command,
operable program or batch file.
I have found that the following syntax works:
local command = '""\\Program Files\\Oracle\\VirtualBox\\VBoxManage\" showvminfo "'..key..'"'
Log file output:
[2014-12-06 16:27:54] Command: ""\Program Files\Oracle\VirtualBox\VBoxManage" showvminfo "Cerium"
So this question isn't to solve a problem as I have aleady done that. I want to understand why the last command works as my current understand means this should not work.
TIA
The issue has to do with how system in C works. Under windows, system internally calls
cmd /c yourinput
Since os.execute just delegates to system (see here), your command likely ends up executing as:
cmd /c "\Program Files\Oracle\VirtualBox\VBoxManage" showvminfo "Cerium"
For reference, from help cmd:
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:
If all of the following conditions are met, then quote characters
on the command line are preserved:
no /S switch
exactly two quote characters
no special characters between the two quote characters,
where special is one of: &<>()#^|
there are one or more whitespace characters between the
two quote characters
the string between the two quote characters is the name
of an executable file.
Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
Since your command contains 4 double quotes in there, it parses your command with the old behavior. This is why you need to surround your entire command with an extra set of " double quotes.
I am extreme newbie to powershell. I have a script that I want to pass a URL as a parameter. The url runs a PHP process that creates and downloads a PDF file then the script prints the pdf and then deletes the pdf. I cannot get the URL parm to work.
Below is my script
$w=$args[0]
Start $w
$Directory = "C:\Users\pslessor\downloads\"
Get-ChildItem -path $Directory -recurse -include *.pdf | ForEach-Object {Start-Process -FilePath $_.fullname -Verb Print -PassThru | %{sleep 5;$_} | kill }
Remove-Item C:\Users\pslessor\downloads\* -include *.PDF
This script is being executed by a batch file PrintPl.bat
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%PrintPl.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%' %1";
And I am testing with testprint.bat
C:\Wget\PrintPl "https://partners.wayfair.com/print_shipping_docs.php?Print=1&printPackingSlips=1&PackingSlipPOs=CS287851107"
The URL is all in one string this editor is forcing the line feed at .php?
The Error I am getting is
The string starting:
At Line:1 Char:25
+ & 'C:\Wget\Printpl.ps1' <<<< 'https://partners.wayfair.com/print_shipping_docs.php?print;
is missing the terminator: '.
At line:1 Char:85
+ & 'C:\Wget\PrintPl.psl' 'https://partners.wayfair.com/print_shipping_docs.php
?print; <<<<
+ CategoryInfo :ParserError: <https://partner...docs.php?print;:
String> [], ParentContainsErrorRecordException
+ FullyqualifiedErrorid : TerminatorExpectedAtEndOfString
'printPackingSlips' is not recognaized as internal or external command,
Operable program or batch file.
'PackingSlipPOs' is not recognized as an internal or external command,
operable program or batch File
This is happening because when you pass the arguments the command interpreter expands your variables and see this:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Wget\PrintPl.ps1' https://partners.wayfair.com/print_shipping_docs.php? Print=1&printPackingSlips=1&PackingSlipPOs=CS287851107"
So the command that it is trying to execute is C:\Wget\PrintPl.ps1, and it assumes that what comes next are arguments. Since what it is passed has a space, and is not enclosed in quotes or double quotes it assumes that it is multiple arguments. It sees it as:
Execute this script: C:\Wget\PrintPl.ps1
With the following arguments:
$Args[0]=?
$Args[1]=Print=1&printPackingSlips=1&PackingSlipPOs=CS287851107
To stop this from happening you will need to enclose the URL in quotes as well, so your command should look like this:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%' '%1'"
Edit: Ok, that didn't work for your case. So we're going to change this a little bit. Instead of -Command I'm going to suggest you use -File. So your Powershell execution line within the batch file will look like this:
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%PowerShellScriptPath%" %*
You should be able to run the batch file exactly as you previously had been. I'm fairly confident that will work for you.
In executing
secedit /configure /db %~1\tomcat.sdb" /cfg %~1\2003.inf" /log %~1\dtomcat.log" /quiet
where
%~1 == C:\Program Files\myDirectory\mySubDirectory\mySuperSubDirectory
a file titled "C:\Program" is generated and within the file is the output for calling
secedit /?
I am curious as to why this is occuring because it makes it quite difficult to start certain services after an installation of a new product, for instance a PostGres windows service.
You appear to be missing quotes in a couple of places:
secedit /configure /db "%~1\tomcat.sdb" /cfg "%~1\2003.inf" /log "%~1\dtomcat.log" /quiet
^ ^ ^
You need to enclose the pathname in quotes:
"C:\Program Files\myDirectory\mySubDirectory\mySuperSubDirectory"
The space in "Program Files" is treated as a delimiter.