如何透過 PowerShell DSC 安裝 .NET 4.5.2?

如何透過 PowerShell DSC 安裝 .NET 4.5.2?

我可以使用 PowerShell DSC 透過 WindowsFeature 資源和 NET-Framework-45-Core 功能在 Windows Server 2012 R2 上安裝 .NET Framework 4.5。我的問題是,如何使用 PowerShell DSC 確保安裝 .NET 4.5.2?

答案1

不確定 OP 是否仍然需要這樣做,但我最近遇到了完全相同的挑戰,當嘗試在 2012 R2 伺服器上僅使用套件資源時,發現安裝程式本身存在許多問題。最終不得不編寫腳本資源並使用 Web 安裝程序,因為完整的軟體包始終無法解壓縮,並出現非常普遍的錯誤。

無論如何,這是我最終得到的工作腳本資源:

Configuration Net452Install
{
    node "localhost"
    {

        LocalConfigurationManager
        {
            RebootNodeIfNeeded = $true
        }

        Script Install_Net_4.5.2
        {
            SetScript = {
                $SourceURI = "https://download.microsoft.com/download/B/4/1/B4119C11-0423-477B-80EE-7A474314B347/NDP452-KB2901954-Web.exe"
                $FileName = $SourceURI.Split('/')[-1]
                $BinPath = Join-Path $env:SystemRoot -ChildPath "Temp\$FileName"

                if (!(Test-Path $BinPath))
                {
                    Invoke-Webrequest -Uri $SourceURI -OutFile $BinPath
                }

                write-verbose "Installing .Net 4.5.2 from $BinPath"
                write-verbose "Executing $binpath /q /norestart"
                Sleep 5
                Start-Process -FilePath $BinPath -ArgumentList "/q /norestart" -Wait -NoNewWindow            
                Sleep 5
                Write-Verbose "Setting DSCMachineStatus to reboot server after DSC run is completed"
                $global:DSCMachineStatus = 1
            }

            TestScript = {
                [int]$NetBuildVersion = 379893

                if (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' | %{$_ -match 'Release'})
                {
                    [int]$CurrentRelease = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full').Release
                    if ($CurrentRelease -lt $NetBuildVersion)
                    {
                        Write-Verbose "Current .Net build version is less than 4.5.2 ($CurrentRelease)"
                        return $false
                    }
                    else
                    {
                        Write-Verbose "Current .Net build version is the same as or higher than 4.5.2 ($CurrentRelease)"
                        return $true
                    }
                }
                else
                {
                    Write-Verbose ".Net build version not recognised"
                    return $false
                }
            }

            GetScript = {
                if (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' | %{$_ -match 'Release'})
                {
                    $NetBuildVersion =  (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full').Release
                    return $NetBuildVersion
                }
                else
                {
                    Write-Verbose ".Net build version not recognised"
                    return ".Net 4.5.2 not found"
                }
            }
        }
    }
}

Net452Install -OutputPath $env:SystemDrive:\DSCconfig
Set-DscLocalConfigurationManager -ComputerName localhost -Path $env:SystemDrive\DSCconfig -Verbose
Start-DscConfiguration -ComputerName localhost -Path $env:SystemDrive:\DSCconfig -Verbose -Wait -Force

答案2

根據這篇 Microsoft Technet 文章,要安裝的功能的名稱應該是以下之一取得 Windows 功能命令的結果。因此,如果 .NET 4.5.2 未出現在清單中,則無法確保它是透過 DSC 安裝的。

姓名指示您要確保已新增或刪除的角色或功能的名稱。這與 Get-WindowsFeature cmdlet 中的 Name 屬性相同,而不是角色或功能的顯示名稱。

所以我猜你必須透過 DCS (4.5) 安裝主要版本,然後找出最佳解決方案將其更新到 4.5.2。

相關內容