PowerShell與UTF8(with BOM)的迷團解答

PowerShell與UTF8(with BOM)的迷團解答

前一篇,只是因為在Write-Debug寫了幾個中文,浪費了大量的時間查找問題。後來黑大在FB留言一句:「好奇 .ps1 存成含 BOM 的 UTF8 編碼可避開錯誤嗎?」不試不知道,原來是我書讀的太少。將原本.ps1的UTF8改以UTF with BOM儲存後,在英文版Windows Server匯入含中文.ps1的Import-Module立馬成功了!

Import-Module之「Missing argument in parameter list」鬼打牆筆記

Import-Module之「Missing argument in parameter list」鬼打牆筆記

故事是這樣的,我在PowerShell Core環境下開發了一個PowerShell Module。但不是每一台Windows Server都有PowerShell Core的執行環境,當部屬至Windows Server的PowerShell時才發現某些主機怪怪的。

此Module在我Windows 11開發機裡的PowerShell或PowerShell Core都有先測試過都,都能正常執行。

Import Module Success
Import Module Fail

兩台不同的Windows Server 2019主機上,PowerShell的Import-Module一台正常,一台不正常。而且在特定同事的開發機上也有類似的情況,也會出現匯入錯誤的情況。

停用不安全的TLS版本