简体   繁体   中英

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

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,

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.

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

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

$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 [script name].py

and it should run, if not just try re-installing power-shell and python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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