加入PowerShell Core 7的行列吧!

加入PowerShell Core 7的行列吧!

前篇說明加入 PowerShell Core 6 的原因。而 PowerShell Core 7 在 2020/3/5 GA 了。如果你已經安裝 PowerShell Core 6 的話,那一定要快點升級為 PowerShell Core 7,可以取得大量的好處。

升級 PowerShell Core 7

最簡單的方法是開啟 Visual Studio Core,它就會提示可以升級 PowerShell Core 7,按下確定,就如此簡單。

如果之前沒有安裝過,那麼參考一下安裝各種版本的 PowerShell,Windows、Linux、macOS、ARM 超完整。

支援 PSReadLine 擴充套件

更完善支援 PSReadLine,PSReadLine 提供許多語法可讀性功能。

Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck

參考 https://docs.microsoft.com/en-us/powershell/module/psreadline/about/about_psreadline?view=powershell-7

全新 Visual Studio Code ISE 相容性模組

如果開發過 PowerShell Script 一定發現過,PowerShell ISE 很難用,Visual Studio Code 加個 PowerShell 擴充好像還行,但邊寫邊執行除錯的方式又好像 PowerShell ISE 比較好些。現在,在 Visual Studio Core 整合 PowerShell Core 7 提供全新 ISE 相容性模組。

在 Visual Studio 按下 Ctrl+Shift+p,輸入 ise 就能 Enable / Disable ISE Mode 選項。

Enable / Disable ISE Mode

啟動 Visual Studio ISE Mode

Visual Studio ISE Mode

cmdlet 參數提示

cmdlet 參數提示

Code Map

Code Map

上面三角型與之前 PowerShell ISE 偵錯快速鍵一樣,像 F8 一樣可以執行選擇的 PowerShell。F5 可以下中斷點偵錯。除了不是黑色佈景之外,終於有個像樣一些的 PowerShell 開發環境了!

參考 How to replicate the ISE experience in Visual Studio Code

相容 PowerShell Module 模式

之前在 PowerShell Core 6 有個不方便的地方就是 Module 的相容性。例如有個常用 Posh-SSH Module,它基於所使用的函式庫問題,一直無法提供 PowerShell Core 版本。現在在 PowerShell Core 7 提供一個 -UseWindowsPowerShell 的相容模式:

Import-Module Posh-SSH -UseWindowsPowerShell

WARNING: Module Posh-SSH is loaded in Windows PowerShell using WinPSCompatSession remoting session; please note that all input and output of commands from this module will be deserialized objects. If you want to load this module into PowerShell Core please use 'Import-Module -SkipEditionCheck' syntax.

這無疑解決一部分無法升級 Module 的問題,讓 PowerShell Core 的通用性更大了。

參考 About Windows PowerShell compatibility

語法糖與啟動速度

隨著 .NET Core 功能不斷加強,PowerShell Core 也提供更多語法糖,這部分請參考:What's New in PowerShell 7.0

另外有個明顯改善,PowerShell Core 7 的啟動速度快很多。

小結

這是一個5還在摸,6還在學,7已經GA的技術速度!你要追嗎?

沒有留言:

張貼留言

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