How to quote this raw command in Ansible YAML? - string

I am using Ansible to manage the configurations of a bunch of Windows clients. For the record, Ansible playbooks (i.e. recipes) are written in YAML.
I need to run this command on every client, and I could test it successfully in a Powershell prompt on a client machine:
> CMD /C 'control intl.cpl,, /f:"C:\Temp\intlsettings.xml"'
(note the single quotes, required because of the double quotes around the filename)
Now I "just" need to write a YAML playbook and use the raw module from Ansible to send this command to a remote Powershell session. And this is where I am stuck. My command contains :, ' and " symbols so I have no idea how to quote it properly.
I have tried a million variations, with no success so far:
CMD /C 'control intl.cpl,, /f:"C:\Temp\intlsettings.xml"'
"CMD /C 'control intl.cpl,, /f:"C:\\Temp\\intlsettings.xml"'"
"CMD /C 'control intl.cpl,, /f:\"C:\\Temp\\intlsettings.xml\"'"
"CMD /C 'control intl.cpl,, /f\:\"C\:\\Temp\\intlsettings.xml\"'"
For information, the final playbook will look something like:
---
- name: Configure keyboard mappings
hosts: windows
tasks:
- name: apply keyboard mappings config
raw: CMD /C 'control intl.cpl,, /f:"C:\Temp\intlsettings.xml"'

If you execute this powershell command
CMD /C 'control intl.cpl,, /f:"C:\Temp\intlsettings.xml"'
powershell creates this CommandLine:
"C:\Windows\system32\cmd.exe" /C "control intl.cpl,, /f:"C:\Temp\intlsettings.xml""
Note, that no ' are used in the CommandLine.
However, you don't need to go through cmd as no cmd functionality is used in your command.
control.exe intl.cpl,, /f:"C:\Temp\intlsettings.xml"
should be enough.
I don't know ansible, but if I understand the documentation correctly, all text after raw is sent as a command via ssh. This means, it is interpreded by some shell. If this shell is cmd.exe, this should work:
tasks:
- name: apply keyboard mappings config
raw: control.exe intl.cpl,, /f:"C:\Temp\intlsettings.xml"
If the shell is powershell (v3 and above (default for win8 and above, installable for win7)), this should work:
tasks:
- name: apply keyboard mappings config
raw: control.exe --% intl.cpl,, /f:"C:\Temp\intlsettings.xml"

Related

WS_FTP PRO wsftppro.exe command line using Connection Configuration

I am trying to create BAT file to run WS_FTP PRO wsftppro.exe command line using Connection Configuration. I save Connection Configuration is working in the UI, however, when trying to call it in windows command prompt it fails.
"c:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -s "local:c:\FTP\CLIENT\CLIENTS.C0000" -d "CJ PRODUCTION UPLOAD:INTAKE.UP.CLIENTS.C0000"
Error log: Use MVS Dsname conventions
I have also tried using single quotes:
"c:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -s "local:c:\FTP\CLIENT\CLIENTS.C0000" -d "CJ PRODUCTION UPLOAD:'INTAKE.UP.CLIENTS.C0000'"
Error log: Remote path not found
How can create a BAT file to run WS_FTP PRO wsftppro.exe command line using Connection Configuration?
The single quote is required for the remote dir path to be valid.
The single quote is only used once, no closing single quote.
-s "local:c:\FTP\CLIENT\CLIENTS.C0000" -d "CJ PRODUCTION UPLOAD:'INTAKE.UP.CSSCE.CLIENTS.C0000"
Powershell code to run it:
$args = #"
-s "local:c:\FTP\CLIENT\CLIENTS.C0000" -d "CJ PRODUCTION UPLOAD:'INTAKE.UP.CSSCE.CLIENTS.C0000"
"#
Start-Process "c:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" $args

batch script to launch node app disappears after execution

This node app for work starts up with a starter script in batch. The cmd window stays there after launch and it opens up the correct port, but it always launched the browser too fast so it showed a 404. After npm launched you could refresh the page and it launches fine.
#echo off
#setlocal
set NODE_HOME=%~dp0/nodejs
set PATH=%NODE_HOME%;%PATH%
call npm install
start http://localhost:8090/
call npm start
I wanted to tweak this a bit to have it wait a few seconds before launching the browser. So I figured I just need to open localhost after npm start is outputting "ready for work" to the CLI. But that command actually never 'finishes' so open localhost is never executed. I tried to resolve it like this:
#echo off
#setlocal
SET NODE_HOME=%~dp0/nodejs
SET PATH=%NODE_HOME%;%PATH%
CALL npm install
START /B CMD /C CALL openlocalhost8090.cmd
CALL npm start
And this is what openlocalhost.cmd looks like:
#echo off
TIMEOUT /T 5 /NOBREAK >NUL
START http://localhost:8090/
It actually launches fine and it waits in the background at the same time as npm start is executed so the browser launches without 404 and the app runs fine! But now that I'm testing it again the cmd window disappears after launching the browser. This is quite inconvenient because I'd still like to see log output and be able to stop the server by closing the cmd window. That's how it was with the original script. I need to go into task management to find the process to stop it now.
How can I fix this to make the cmd window stay alive?
Also, is there a better way to handle this 404 "bug"? (I love bash, I could just use open localhost:8090 & npm start and be done!)
Edit: After suggestions by #Mofi this is what I came up with:
#echo off
#setlocal
SET NODE_HOME=%~dp0nodejs
SET PATH=%NODE_HOME%;%PATH%
CALL "%NODE_HOME%\npm.cmd" install
START /B CMD /K CALL openlocalhost8090.cmd
CALL "%NODE_HOME%\npm.cmd" start
I still do the subscript with openlocalhost8090.cmd because contrary to what #Mofi claims that just putting open localhost and call npm start under each other will solve my issue, it doesn't. Putting everything directly under each other is what was wrong with the first script and what I wanted to solve. The browser is started too fast so it 404's before npm can start. If this is not what you meant #Mofi, then I'm sorry but I didn't find your answer very clearly written.
The /K flag after CMD however prevents the cmd window from closing and solves that issue, so thank you for that!
1. Complete definition of execution environment for batch file
The usage of just #setlocal results in creating a copy of existing list of environment variables, pushing current directory path on stack as well as the states of command extensions and delayed expansion without changing the state for command extensions and of delayed environment variable expansion. So the command is incomplete to define completely the execution environment for the batch file. The execution environment still depends on what is defined for command extensions and delayed expansion outside the batch file which is in general not good for any batch file.
The perfect command line to define execution environment is for this batch file:
setlocal EnableExtensions DisableDelayedExpansion
2. Correct variable definition using batch file path
The command line set NODE_HOME=%~dp0/nodejs is not good because of
\ is the directory separator on Windows and not / as on Linux/Mac as it can be read in Microsoft documentation about Naming Files, Paths, and Namespaces and
%~dp0 expands to a path always ending already with a backslash and for that reason no additional backslash should be added on concatenating the full batch file path with a file/folder name or a wildcard pattern which Windows has to remove later on every usage of the path containing now inside \\ and
the entire argument string of command SET is not enclosed in double quotes which results in this command line does more than just assigning a folder path to environment variable NODE_HOME on full path of batch file contains by chance one or more & like on folder path being C:\Temp\Development & Test which is interpreted by cmd.exe outside a double quoted argument string as a command operator explained in detail on single line with multiple commands using Windows batch file.
The perfect command line is as follows after making sure the command extensions are enabled and delayed environment variable expansion is disabled:
set "NODE_HOME=%~dp0nodejs"
An ampersand in full path of batch file is interpreted here as literal character in double quoted argument string of command SET.
It is important on writing a batch file how a command line looks after Windows command processor parsed it and replaced all environment variable, loop variable and batch file argument references and not how the command line is written in batch file. This command line does not contain an & in batch file, but it can contain & on execution of the command line after replacing %~dp0 by full path of batch file.
It is possible to see how the command line looks after parsing by cmd.exe by running a batch file from within an opened command prompt window with #echo off removed or commented out temporarily to get displayed also the command lines finally executed.
3. Correct modification of local environment variable PATH
set PATH=%NODE_HOME%;%PATH% is again not good as it results in undefined behavior if the string assigned to either environment variable NODE_HOME or environment variable PATH contains an ampersand. The perfect and safe command line (on valid PATH string) would be:
set "PATH=%NODE_HOME%;%PATH%"
4. Referencing executables and scripts with full qualified file names
It is always best to reference executables and scripts in a batch file with full qualified file name on being well known instead of just file name as in this case cmd.exe does not need to search for the file in current directory and all directories listed in local environment variable PATH for a file with an extension listed in local environment variable PATHEXT.
Please take a look on answer on What is the reason for "X is not recognized as an internal or external command, operable program or batch file"? for details.
A batch file referencing executables and scripts with full qualified file names works also on PATH corrupted somehow by an installer or a user, and works also if there are (mainly batch) files created with a file name identical to a Windows command which is used in the batch file, but found by chance by cmd.exe instead of the executable in %SystemRoot%\System32.
So with batch file npm.cmd stored in subdirectory nodejs of batch file directory it would be better to use as replacement for the last three command lines:
call "%NODE_HOME%\npm.cmd" install
call "%NODE_HOME%\npm.cmd" start
start "" "http://localhost:8090/"
5. No need for an additional batch file
It is unclear why another command processor instance is started running parallel to the cmd.exe instance processing the batch file after calling batch file npm.cmd with argument install which is processing one more batch file which runs %SystemRoot%\System32\timeout.exe (without file path and file extension) to wait five seconds and then launch default internet browser with a local url as argument as separate process resulting in second started instance of cmd.exe terminating itself.
It should be enough to call npm.cmd with argument start and start default internet browser as separate process with local url as argument after processing of npm.cmd with argument start finished.
So the entire batch file could be:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "NODE_HOME=%~dp0nodejs"
set "PATH=%NODE_HOME%;%PATH%"
call "%NODE_HOME%\npm.cmd" install
call "%NODE_HOME%\npm.cmd" start
start "" "http://localhost:8090/"
It could be that call "%NODE_HOME%\npm.cmd" start results in starting an application as separate process or a service which takes some time to initialize completely and for that reason some seconds must be waited before the internet browser is started with the local url which requires that the started application/service is running already. In this case the batch file should be:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "NODE_HOME=%~dp0nodejs"
set "PATH=%NODE_HOME%;%PATH%"
call "%NODE_HOME%\npm.cmd" install
call "%NODE_HOME%\npm.cmd" start
%SystemRoot%\System32\timeout.exe /T 5 /NOBREAK >nul
start "" "http://localhost:8090/"
It does not look so according to posted information posted, but in case of call "%NODE_HOME%\npm.cmd" start results in cmd.exe processing the batch file terminates itself because of a command exit without option /B before reaching end of batch file or the processing of the called batch file never finishes because of cmd.exe is waiting for self-termination of a started executable which does not terminate itself, the batch file should be:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "NODE_HOME=%~dp0nodejs"
set "PATH=%NODE_HOME%;%PATH%"
call "%NODE_HOME%\npm.cmd" install
start "Launch browser delayed" /MIN %ComSpec% /C %SystemRoot%\System32\timeout.exe /T 5 /NOBREAK >nul & start "" "http://localhost:8090/"
call "%NODE_HOME%\npm.cmd" start
There is no additional batch file needed in any case.
6. Keep command processor running after finishing execution of batch file
Windows executes implicit %ComSpec% /c "Full qualified batch file name" on double clicking a batch file with making the batch file directory the current directory for cmd.exe. The help output on running in a command prompt window cmd /? explains that option /C is for running a command line and close cmd.exe after finishing execution of this command line. This default behavior is not wanted here. The command processor should keep running after batch file execution which requires starting cmd.exe with option /K and the full qualified batch file name.
Therefore a shortcut file (.lnk) file needs to be created on Windows desktop or in Windows start menu or somewhere else and pinned to Windows taskbar which contains in properties:
Target: %SystemRoot%\System32\cmd.exe /K "Full qualified batch file name"
Start in: "Full path of batch file folder"
The property Target can be also:
%ComSpec% /K "Full qualified batch file name"
ComSpec is a system environment variable used even by cmd.exe itself and must be defined always with %SystemRoot%\System32\cmd.exe as it is by Windows default.
In properties of the shortcut file can be configured also the number of lines and columns for the console window opened on running cmd.exe via this shortcut, the font, the text foreground and background colors and much more. It is even possible to define a Shortcut key to launch cmd.exe for processing the batch file from within any application by pressing this key (or combination of keys).

Problem running a powershell command containing path with spaces inside multiple nested quotes using node.js

Explanation
I'm trying to run the command bellow from within Node js:
child_process.exec(`powershell -command "Start-Process cmd '/k cd /d \"${path}\"' -Verb RunAs"`)
Which powershell receives in the formatted form:
powershell -command "Start-Process cmd '/k cd /d "E:\test"' -Verb RunAs"
Goal
The goal is to open specified directory in a new cmd window from node.js:
Open cmd as administrator by using powershell (if there's a better way, please let me know)
Run specified command in cmd which opens a new cmd window and changes the directory
Problem
It does work for paths without spaces, like E:\test, but doesn't work for paths like E:\test name (contains space).
I get the following error:
The string is missing the terminator: '.
I tried using back ticks to escape some nested quotes but it didn't help:
"Start-Process cmd '/k cd /d `"E:\test name`"' -Verb RunAs"
You have to escape the backslashes, because \" becomes " in your string but it should stay as \" in the final string, so replace \" with \\".
child_process.exec(`powershell -command "Start-Process cmd '/k cd /d \\"${path}\\"' -Verb RunAs"`)

How to pass application path to task in Azure batch

I am using Azure batch. I have an exe which I need to execute on compute nodes.
I am using this path in the azure portal in my task window "cmd /c %AZ_BATCH_APP_PACKAGE_MyAppCreateRG%\CreateRG.exe -args HelloRG eastUs"
But I am getting an error: The system cannot find the path specified.
For your issue, the error you got is the core point of your problem. You can create a task with the command cmd /c "echo %AZ_BATCH_APP_PACKAGE_MyAppCreateRG%" to show the exact path if it exists or not.
Just like if you want to get the environment variable PATH in windows, you should use the command echo %PATH%. And the result will like below:
So, if it could not show the path you want, it means the environment variable doesn't exist and you should set it up first and check then.
I suggest if you want to execute an exe, you should check if the path of it is right and if the exe already exist for you.
It appears that you have mis-quoted your command to execute. Try:
cmd.exe /c "%AZ_BATCH_APP_PACKAGE_MyAppCreateRG%\CreateRG.exe -args HelloRG eastUs"
As an alternative i am able to execute powershell script instead of exe
powershell C:\Scripts\CreateRG.ps1 -resourceGroup "MyRG" -location "eastUs"

Create files in Inno Setup setup needed by the [Files] section

In my Inno Setup script, I need to execute a command that generates temp files that are to be copied in the [Files] section. I have tried the following:
; cd to the directory of the Inno Setup script and execute a python file
#expr Exec('cmd /C "cd /d %cd% & C:\Python34\python.exe run.py"','','',1,SW_HIDE)
This does not seem to execute as I do not see the files created, which can obviously not be included in the installer.
Along the same lines, how would I execute a command when complete to delete these temp files?
Edit I did execute the cmd by hand and validated that it works
It should be:
#expr Exec('cmd.exe', '/C "cd /d %cd% & C:\Python34\python.exe run.py"','',1,SW_HIDE)
The cmd.exe is the process you are executing, the rest are arguments.
Though even better would be to do without cmd.exe as you do not need it actually:
#expr Exec('C:\Python34\python.exe', 'run.py','c:\startupfolder',1,SW_HIDE)
See Inno Setup Preprocessor: Exec.
Though personally, I'd create a batch file that first runs the Python and then runs Inno Setup compiler. It's way easier and more understandable.

Resources