On Windows Server 2012R2, how would I go about making a specific python-script (D:/App/applic.py
) executable from the terminal at any directory without evoking neither python
nor the .py
file-ending? On this server, I would like all users to be able to type:
applic 2017 -v
anywhere in the terminal to evoke
python D:/Applic_dev/applic.py 2017 -v
I'm not looking for a py2exe-kind of solution since Python 3 will always be on the server.
PS. My python-script uses plac
to parse commandline arguments.
Create a cmd file in a protected location (where users cannot edit it)
applic.cmd
@ECHO OFF
REM This will be wherever the python executable is
SET "PY=%PROGRAMFILES%\Pythonx.x\python.exe"
REM This checks to see if the script is called without arguments
IF [%*] EQU [] (
@CALL %PY% "D:/App/applic.py"
EXIT /B
)
REM %* contains all arguments the script received
@CALL %PY% "D:/App/applic.py" %*
EXIT /B
From here, edit your PATH environment variable to include the path where this protected location is.
$ProtectedFolderLocation = 'C:\Windows\System32\Applic'
$RegKey = 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment'
$Path = (Get-ItemProperty -Path $RegKey).Path
If ($Path[-1] -ne ';') { $Path += ";$ProtectedFolderLocation;" }
Else { $Path += "$ProtectedFolderLocation;" }
Set-ItemProperty -Path $RegKey -Name 'PATH' -Value $Path
Now when users call applic
from cmd.exe
, they'll be calling the script specified above.