如何使用 PowerShell DSC 安裝 MSI 包?


要使用 DSC 安裝 MSI 包,我們需要使用 DSC 資源“Package”。讓我們看看此資源有哪些可用的屬性。

PS C:\> Get-DscResource -Name Package | Select -ExpandProperty Properties

Name PropertyType IsMandatory Values
---- ------------ ----------- ------
Name [string] True {}
Path [string] True {}
ProductId [string] True {}
Arguments [string] False {}
Credential [PSCredential] False {}
DependsOn [string[]] False {}
Ensure [string] False {Absent, Present}
LogPath [string] False {}
PsDscRunAsCredential [PSCredential] False {}
ReturnCode [UInt32[]] False {}

名稱、路徑ProductID引數對於此 DSC 資源是必需的。

獲取上述詳細資訊的最佳方法是在測試機器上安裝示例包,然後獲取詳細資訊。我們將在這裡使用安裝在一臺計算機上的 7Zip MSI 包。

Get-Package 7-zip* | fl *

從以上輸出,我們可以獲取安裝後包的名稱,ProductID (即 ProductCode)

Configuration Install7zip{
   Node @("LabMachine2k16","AD"){
      Package 7zip{
         Name = '7-Zip 19.00 (x64 edition)'
         ProductId = '23170F69-40C1-2702-1900-000001000000'
         Path = '\ad\shared\7z1900-x64.msi'
         Ensure = 'Present'
      }
   }
}

在以上示例中,源包位於源位置,我們希望在兩個節點上安裝 7zip。

為了在特定位置生成 MOF 檔案以便以後可以使用它們啟動配置,

Install7zip -OutputPath C:\Temp\7zipInstall -Verbose

輸出

生成 MOF 檔案後,我們可以啟動配置以應用 DSC,

Start-DscConfiguration -Path C:\Temp\7zipInstall -Wait -Force -Verbose

上述命令成功執行後,使用以下命令檢查配置是否已應用,

Test-DscConfiguration -Path C:\Temp\7zipInstall

您應該會得到如下輸出,顯示兩個伺服器都處於所需狀態,這意味著 MSI 包已安裝。

如果InDesiredState屬性為假,則表示伺服器錯過了配置。

更新於: 2021年4月28日

2K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告