網頁

在Server Core進行Python 3安裝

在Server Core進行Python 3安裝

同事想在 Server Core 上安裝 Python 3,但看到 Server Core 純 command-line 畫面嚇到,不知如何安裝軟體。其實我也不會,但我有變通的方式。哈哈。

Step 1 - 進行 PowerShell 組態

Get-ExecutionPolicy -List

可以取得目前狀態清單:

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine    RemoteSigned

PowerShell 執行權限設計有以下種類,以下引用微軟文件的說明:

  • Restricted Does not load configuration files or run scripts. Restricted is the default execution policy.
  • AllSigned Requires that all scripts and configuration files be signed by a trusted publisher, including scripts that you write on the local computer.
  • RemoteSigned Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.
  • Unrestricted Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.
  • Bypass Nothing is blocked and there are no warnings or prompts.
  • Undefined Removes the currently assigned execution policy from the current scope. This parameter will not remove an execution policy that is set in a Group Policy scope.
cd ~
Set-ExecutionPolicy -Scope CurrentUser

輸入提示的地方,我們輸入 RemoteSigned

Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine    RemoteSigned

Step 2 - 安裝 Chocolatey

Chocolatey 是一個套件管理器,讓我們可以透過簡單的指令來去管理套件的搜尋、安裝、更新與移除。這不正是我們 Server Core 上想要的功能嗎。 😉

$script = New-Object Net.WebClient
$script | Get-Member

可以查看 .NET 的 WebClient 成員方法有那些。

    TypeName: System.Net.WebClient

Name                      MemberType Definition
----                      ---------- ----------
...
DownloadData              Method     byte[] DownloadData(string address), byte[] DownloadData(uri address)
DownloadDataAsync         Method     void DownloadDataAsync(uri address), void DownloadDataAsync(uri address, System.Object userToken)
DownloadDataTaskAsync     Method     System.Threading.Tasks.Task[byte[]] DownloadDataTaskAsync(string address), System.Threading.Tasks.Task[byt...
DownloadFile              Method     void DownloadFile(string address, string fileName), void DownloadFile(uri address, string fileName)
DownloadFileAsync         Method     void DownloadFileAsync(uri address, string fileName), void DownloadFileAsync(uri address, string fileName,...
DownloadFileTaskAsync     Method     System.Threading.Tasks.Task DownloadFileTaskAsync(string address, string fileName), System.Threading.Tasks...
DownloadString            Method     string DownloadString(string address), string DownloadString(uri address)
DownloadStringAsync       Method     void DownloadStringAsync(uri address), void DownloadStringAsync(uri address, System.Object userToken)
DownloadStringTaskAsync   Method     System.Threading.Tasks.Task[string] DownloadStringTaskAsync(string address), System.Threading.Tasks.Task[s...
...

進行連線與讀取測試:

$script.DownloadString("https://chocolatey.org/install.ps1")

如果上述語法成功讀取,那麼我們就能來安裝 Chocolatey 套件管理器:

iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex

執行請求可以選擇 iwrInvoke-WebRequest cmdlet,然後將結果交給 iexInvoke-Expression cmdlet 執行。

成功之後可以更新一下 Chocolatey:

choco upgrade chocolatey

Step 3 — 安裝 Python 3

接下來就簡單至極:

choco install -y python3

注意一下安裝訊息中有一小段說明,需要重開當前的 shell 才能重新讀取新的環境路徑(PATH),或執行 refreshenv 指令。

python -V

確認 Python 安裝是否成功。

如果環境路徑(PATH)沒有加入 Python 的安裝路徑,那麼可以執行 $ENV:PATH="$ENV:PATH;c:\Python36" 來手動加入。

這樣未來就能透過 Chocolatey 的幫忙在 Server Core 來進行軟體套件的管理。當然,前提是軟體有發行至 Chocolatey 上。

沒有留言:

張貼留言

感謝您的留言,如果我的文章你喜歡或對你有幫助,按個「讚」或「分享」它,我會很高興的。