共用方式為


在指定的 HTTPS URL 上安裝具有指定識別碼的 MSI 檔案

描述

此範例示範如何使用 MsiPackage 資源來確保已安裝套件。

[確定] 設定為 、[ProductID] 設定為 {DEADBEEF-80C6-41E6-A1B9-8BDB8A05027F}Present ,並將[路徑] 設定為 https://contoso.com/example.msi ,如果尚未安裝套件,資源就會 example.msi 安裝套件。

如果未安裝套件,資源會在資源強制執行所需的狀態時,從 https://contoso.com/example.msi 下載套件。 如果下載失敗,資源會擲回例外狀況。

使用 Invoke-DscResource

此腳本示範如何搭配 Invoke-DscResource Cmdlet 使用 MsiPackage 資源,以確保已安裝 Web URI 上的套件。

[CmdletBinding()]
param()

begin {
    $SharedParameters = @{
        Name       = 'MsiPackage'
        ModuleName = 'PSDscResource'
        Properties = @{
            ProductId = '{DEADBEEF-80C6-41E6-A1B9-8BDB8A05027F}'
            Path      = 'https://contoso.com/example.msi'
            Ensure    = 'Present'
        }
    }

    $NonGetProperties = @(
        'Ensure'
    )
}

process {
    $TestResult = Invoke-DscResource -Method Test @SharedParameters

    if ($TestResult.InDesiredState) {
        $QueryParameters = $SharedParameters.Clone()

        foreach ($Property in $NonGetProperties) {
            $QueryParameters.Properties.Remove($Property)
        }

        Invoke-DscResource -Method Get @QueryParameters
    } else {
        Invoke-DscResource -Method Set @SharedParameters
    }
}

使用組態

此程式碼片段示範如何使用資源區塊來定義 ConfigurationMsiPackage ,以確保已安裝 Web URI 上的套件。

Configuration InstallPackageFromHttps {
    Import-DscResource -ModuleName 'PSDscResources'

    Node localhost {
        MsiPackage ExampleMsiPackage {
            ProductId = '{DEADBEEF-80C6-41E6-A1B9-8BDB8A05027F}'
            Path      = 'https://contoso.com/example.msi'
            Ensure    = 'Present'
        }
    }
}