私はrestを介して取得した入力に基づいてコマンドをビルドするpythonスクリプトを持っています。 コマンドはプリントアウトされ、コピーされてpowershellに貼り付けられたときに機能します。 私の質問は、実際にコマンドを実行させる方法です。pythonでコマンドを書いて、powershellで実行します。
これまでのところ私は、同じディレクトリ内のファイルにコマンドを書き込み、そのように動作して試してみました:
import subprocess, sys
p = subprocess.Popen(["powershell.exe", "test.ps1"],stdout=sys.stdout)
p.communicate()
しかし、私はエラーを取得する:
Traceback (most recent call last): File "C:/Users/pschaefer/src/python/executePowerShell.py", line 2, in p = subprocess.Popen(["powershell.exe", "test.ps1"],stdout=sys.stdout) File "C:\Python27\lib\subprocess.py", line 703, in init errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr) File "C:\Python27\lib\subprocess.py", line 850, in _get_handles c2pwrite = msvcrt.get_osfhandle(stdout.fileno()) UnsupportedOperation: fileno
更新が
を削除する,stdout=sys.stdout1
は以前のエラーを取り除き、今すぐ表示されます
test.ps1 cannot be loaded because running \r\nscripts is disabled on this system.
私は".\\test.ps1 Set-ExecutionPolicy -ExecutionPolicy unrestricted"
にコマンドを切り替えてみましたが、まだ問題があります。
また、.ps1ファイルを作成する代わりに、コマンドを文字列としてビルドし、それを1つずつビルドすることも可能ですか?それを理解できる人なら誰でも大歓迎です!
'stdout = sys.stdout'はデフォルトです。 'os.system()'だけで試しましたか?または 'subprocess.check_call()'? –
ああ、 'stdout = sys.stdout'を削除すると、私はPythonシェルで見られるエラーを止めてしまいましたが、そこにはエラーが表示されたpowershellウィンドウのポップアップが表示されますが、私はそれを捕まえられません。私はps1ファイルに追加することができます閉鎖からそれを停止する方法に関する任意のアイデア?申し訳ありませんが、ここpowershellのnoobは –
IDEから実行しないでください。標準コンソールから実行してください。 –