繁体   English   中英

无法从 powershell7 外部运行此命令,默认 powershell 有效

[英]can't run this command from outside of powershell7, default powershell works

{哦,如果可能的话,我不想使用 powershell} 我有一台 powershell 默认值损坏的电脑,所以如果我运行这个,我不得不使用 ps7

import subprocess
import os

powershell = ''
if os.path.isfile('C:/Program Files/PowerShell/7/pwsh.exe'):
    powershell = 'pwsh'
elif os.path.isfile('C:/Program Files (x86)/PowerShell/7/pwsh.exe'):
    powershell = 'pwsh'
else:
    powershell = 'powershell'

filename = 'test.exe'

subprocess.call(powershell + ' curl -H '+'\'Authorization: token ghp_xxxxxxxxxxxxxxxxxxx\''+' -H '+'\'Accept: application/vnd.github.v4.raw\''+' -o ' + filename + ' https://raw.githubusercontent.com/xxxxxx/[repo]/[branch]/'+filename+'\'', shell=True)

它说

The argument 'curl' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

但如果我运行这个

curl.exe -H 'Authorization: token ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxx' -H 'Accept: application/vnd.github.v4.raw' -o test.exe https://raw.githubusercontent.com/[user]/[repo]/[branch]/test.exe

直接在ps7中命令,它有效,

我也尝试在命令提示符下运行它

"C:\Program Files\PowerShell\7\pwsh.exe" curl.exe -H 'Authorization: token ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxx' -H 'Accept: application/vnd.github.v4.raw' -o test.exe https://raw.githubusercontent.com/[user]/[repo]/[branch]/test.exe

并得到同样的错误

这就是您如何使用来自 python 的请求模块。

import requests

headers = {
    "Authorization": "token ghp_xxxxxxxxxxxx",
    "Accept": "application/vnd.github.v4.raw",
}
#### for file less than 1 mb
response = requests.get(
    "https://api.github.com/repos/[INSERT_OWNER_HERE]/[INSERT_REPO_HERE]/contents/[PATH/TO/FILE]", headers=headers
)
### If file is more than 1MB 
response = requests.get(
"https://raw.githubusercontent.com/[INSERT_OWNER_HERE]/[INSERT_REPO_HERE]/[BRANCH]/[PATH/TO/FILE]", headers=headers
)
with open("test.exe", "wb") as f:
    f.write(response.content)

这就是使用 powershell 和子进程的方法

import os
import subprocess

powershell = ""
if os.path.isfile("C:/Program Files/PowerShell/7/pwsh.exe"):
    powershell = "pwsh"
elif os.path.isfile("C:/Program Files (x86)/PowerShell/7/pwsh.exe"):
    powershell = "pwsh"
else:
    powershell = "powershell"
filename = "test.exe"
subprocess.call(
    powershell
    + " -Command curl.exe -H "
    + "'Authorization: token []'"
    + " -H "
    + "'Accept: application/vnd.github.v4.raw'"
    + " -o "
    + filename
    + " https://raw.githubusercontent.com/[INSERT_OWNER_HERE]/[INSERT_REPO_HERE]/[BRANCH]/"
    + filename,
    shell=True,
)

如果必须的话,使用 PowerShell 并不难

$headers = @{Authorization = "token ghp_xxxxxxxxxxxx";Accept = "application/vnd.github.v4.raw"}
$URL = "https://github.com/WoLvES-2x0/tags/blob/main/test.exe"
Invoke-RestMethod -Uri $URL -Headers $headers

在命令提示符下尝试

cd [script location]

如果你的电脑上有 python 作为一种语言,那么你应该能够做到:

python [script name].py

它应该可以运行,如果不能,只需尝试重新安装 power-shell 和 python。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM