/ / Windows Server 2016上の任意の場所からpython-scriptをターミナルプログラムとして(.py-file-endingなしで)実行 - python-3.x、powershell、windows-server-2012-r2

Python-3.x、powershell、windows-server-2012-r2のいずれかの場所から、ターミナルプログラム(.py-file-endingなし)としてpython-scriptを実行します。

Windows Server 2012 R2では、特定のPythonスクリプトを作成する方法を教えてください(D:/App/applic.pyどちらのディレクトリも起動せずに端末から実行可能 python また .py ファイル終了?このサーバーでは、すべてのユーザーが次のように入力できるようにします。

applic 2017 -v

起動する端末内の任意の場所

python D:/Applic_dev/applic.py 2017 -v

私はPython 2がいつもそうなるのでpy2exeのような解決策を探していません。 サーバー。

PS。私のPythonスクリプトは使っています plac コマンドライン引数を解析します。

回答:

回答№1は0

保護された場所(ユーザーが編集できない場所)にcmdファイルを作成します。

applic.cmd

@ECHO OFF
REM This will be wherever the python executable is
SET "PY=%PROGRAMFILES%Pythonx.xpython.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

ここから、この保護された場所がある場所のパスを含むようにPATH環境変数を編集します。

自動化されたPowerShellの方法

$ProtectedFolderLocation = "C:WindowsSystem32Applic"
$RegKey = "HKLM:SystemCurrentControlSetControlSession ManagerEnvironment"

$Path = (Get-ItemProperty -Path $RegKey).Path
If ($Path[-1] -ne ";") { $Path += ";$ProtectedFolderLocation;" }
Else { $Path += "$ProtectedFolderLocation;" }

Set-ItemProperty -Path $RegKey -Name "PATH" -Value $Path

ユーザーが電話をかけたとき applic から cmd.exe彼らは上で指定されたスクリプトを呼び出すでしょう。