
我安裝了 Visual Studio Code 並啟用了 PowerShell 擴充功能
我想用一些自訂模組(例如xHyper-V
和 )編寫所需的狀態配置xPSDesiredStateConfiguration
。
現在的問題是,PSDesiredStateConfiguration
當我想通過Import-DSCResource
. Visual Studio Code 無法載入任何其他已安裝的 DSC 資源。在 PowerShell ISE 中,一切正常,如您在此處看到的那樣(左側是 VSCode,右側是 ISE):
但奇怪的是,這只影響編輯器本身。在 vscode 的終端機中,它在所有模組中找到 DSC 資源。看這個例子:
PS Z:\Powershell-Scripts\DesiredStateConfiguration> Get-DscResource xVHD
ImplementedAs Name ModuleName Version Properties
------------- ---- ---------- ------- ----------
PowerShell xVHD xHyper-V 3.17.0.0 {Name, Path, DependsOn, Ensure...}
PS Z:\Powershell-Scripts\DesiredStateConfiguration> Get-DscResource WindowsFeature
ImplementedAs Name ModuleName Version Properties
------------- ---- ---------- ------- ----------
PowerShell WindowsFeature PSDesiredStateConfiguration 1.1 {Name, Credential, DependsOn, Ensure...}
它還找到模組本身:
PS Z:\Powershell-Scripts\DesiredStateConfiguration> Get-Module xHyper-V, PSDesiredStateConfiguration -ListAvailable
Verzeichnis: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 3.17.0.0 xHyper-V
Verzeichnis: C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.1 PSDesiredStateConfiguration {Set-DscLocalConfigurationManager...}
唯一的區別是它們位於不同的 s 中,但當我將模組複製到也駐留的 Path$env:PSModulePath
時,它仍然不起作用。xHyper-V
PSDesiredStateConfiguration
有趣的是,這似乎只影響編輯器的 IntelliSense。如果我使用自訂 DSC 模組運行配置,它仍然可以*.mof
正確建立檔案。
我怎樣才能解決這個問題?
我試圖解決這個問題:
- 刪除並重新安裝所有自訂 DSC 模組
- 在 VSCode 中卸載 PowerShell 擴展,重新啟動 VSCode 並再次安裝
- 完全重新安裝VSCode
答案1
摀臉。有點尷尬,但在我發布此文後 1 分鐘,我找到了答案:
我也PowerShell Core
安裝了。在後面的 VSCode 終端機中打開了一個 Core Session,它不是活動的,但不知何故仍然影響了 Intellisense。
只需關閉Core
終端,一切就會如預期進行。
或者
PowerShell Core
如果$env:PSModulePath
包含 DSC 模組所在的資料夾,則簽入提升的資料夾$env:PSModulePath -split ';'
。該資料夾很可能不在那裡。使用以下命令添加它,然後在 vscode 重新啟動和 pwsh 重新啟動後,IntelliSense 將始終工作,因為 pwsh 現在也會在正確的目錄中找到:
$CurrentValue = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
[Environment]::SetEnvironmentVariable("PSModulePath", $CurrentValue + [System.IO.Path]::PathSeparator + "C:\Program Files\WindowsPowerShell\Modules", "Machine")