簡體   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