如何通过 PowerShell DSC 安装 .NET 4.5.2?

Mar*_*ark 15 powershell

我可以使用 PowerShell DSC 通过 WindowsFeature 资源和 NET-Framework-45-Core 功能在 Windows Server 2012 R2 上安装 .NET Framework 4.5。我的问题是,如何使用 PowerShell DSC 来确保安装了 .NET 4.5.2?

小智 14

不确定 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
Run Code Online (Sandbox Code Playgroud)


Ob1*_*lan 2

根据这篇 Microsoft Technet 文章,要安装的功能的名称应该是Get-WindowsFeature命令的结果之一。因此,如果 .NET 4.5.2 未出现在列表中,则无法确保它是通过 DSC 安装的。

名称指示您要确保添加或删除的角色或功能的名称。这与 Get-WindowsFeature cmdlet 中的 Name 属性相同,而不是角色或功能的显示名称。

所以我猜你必须通过 DCS (4.5) 安装主要版本,然后找出最佳解决方案将其更新到 4.5.2。