简体   繁体   English

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

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

{Oh if possible I don't want to use powershell} I've a pc where powershell default is broken so I'm forced to use ps7, if I run this {哦,如果可能的话,我不想使用 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)

it says它说

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.

but if I run this但如果我运行这个

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

command in ps7 directly, it works,直接在ps7中命令,它有效,

I also tried running this in command prompt我也尝试在命令提示符下运行它

"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

and got same error并得到同样的错误

This is how you can use the requests module from python.这就是您如何使用来自 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)

And this is how to do it with powershell and subprocess这就是使用 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,
)

Not so difficult with PowerShell if you have to如果必须的话,使用 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

In command prompt try在命令提示符下尝试

cd [script location]

If you have python as a language on your PC then you should just be able to do:如果你的电脑上有 python 作为一种语言,那么你应该能够做到:

python [script name].py

and it should run, if not just try re-installing power-shell and python.它应该可以运行,如果不能,只需尝试重新安装 power-shell 和 python。

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

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