diff --git a/images/win/post-generation/Dotnet.ps1 b/images/win/post-generation/Dotnet.ps1 deleted file mode 100644 index 56d2f875d..000000000 --- a/images/win/post-generation/Dotnet.ps1 +++ /dev/null @@ -1,11 +0,0 @@ -$latestPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::Machine) -$dotnetPath = "$env:USERPROFILE\.dotnet\tools" - -if (-not $latestPath.Contains($dotnetPath)) -{ - $latestPath = "$dotnetPath;$latestPath" - [System.Environment]::SetEnvironmentVariable('PATH', $latestPath, [System.EnvironmentVariableTarget]::Machine) -} - -# Recreate the config using the 'dotnet nuget list source command' -dotnet nuget list source diff --git a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 index 09e4962a6..bf7438f76 100644 --- a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 +++ b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 @@ -8,12 +8,19 @@ param() . $PSScriptRoot\VisualStudioHelpers.ps1 Export-ModuleMember -Function @( + 'Connect-Hive' + 'Disconnect-Hive' 'Test-MachinePath' 'Get-MachinePath' + 'Get-DefaultPath' 'Set-MachinePath' + 'Set-DefaultPath' 'Add-MachinePathItem' + 'Add-DefaultPathItem' 'Get-SystemVariable' + 'Get-DefaultVariable' 'Set-SystemVariable' + 'Set-DefaultVariable' 'Install-Binary' 'Install-VisualStudio' 'Get-ToolsetContent' diff --git a/images/win/scripts/ImageHelpers/PathHelpers.ps1 b/images/win/scripts/ImageHelpers/PathHelpers.ps1 index 1c98dcb65..af4bd5ef5 100644 --- a/images/win/scripts/ImageHelpers/PathHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/PathHelpers.ps1 @@ -1,3 +1,38 @@ +function Connect-Hive { + param( + [string]$FileName = "C:\Users\Default\NTUSER.DAT", + [string]$SubKey = "HKLM\DEFAULT" + ) + + Write-Host "Loading the file $FileName to the Key $SubKey" + if (Test-Path $SubKey.Replace("\",":")) { + return + } + + $result = reg load $SubKey $FileName *>&1 + if ($LASTEXITCODE -ne 0) { + Write-Host "Failed to load hive: $result" + exit 1 + } +} + +function Disconnect-Hive { + param( + [string]$SubKey = "HKLM\DEFAULT" + ) + + Write-Host "Unloading the hive $SubKey" + if (-not (Test-Path $SubKey.Replace("\",":"))) { + return + } + + $result = reg unload $SubKey *>&1 + if ($LASTEXITCODE -ne 0) { + Write-Host "Failed to unload hive: $result" + exit 1 + } +} + function Get-SystemVariable { param( [string]$SystemVariable @@ -6,6 +41,19 @@ function Get-SystemVariable { [System.Environment]::GetEnvironmentVariable($SystemVariable, "Machine") } +function Get-DefaultVariable { + param( + [string]$DefaultVariable, + [string]$Name = "DEFAULT\Environment", + [bool]$Writable = $false + ) + + $key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($Name, $Writable) + $key.GetValue($DefaultVariable, "", "DoNotExpandEnvironmentNames") + $key.Handle.Close() + [System.GC]::Collect() +} + function Set-SystemVariable { param( [string]$SystemVariable, @@ -16,10 +64,30 @@ function Set-SystemVariable { Get-SystemVariable $SystemVariable } +function Set-DefaultVariable { + param( + [string]$DefaultVariable, + [string]$Value, + [string]$Name = "DEFAULT\Environment", + [string]$Kind = "ExpandString", + [bool]$Writable = $true + ) + + $key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($Name, $Writable) + $key.SetValue($DefaultVariable, $Value, $Kind) + Get-DefaultVariable $DefaultVariable + $key.Handle.Close() + [System.GC]::Collect() +} + function Get-MachinePath { Get-SystemVariable PATH } +function Get-DefaultPath { + Get-DefaultVariable Path +} + function Set-MachinePath { param( [string]$NewPath @@ -28,6 +96,14 @@ function Set-MachinePath { Set-SystemVariable PATH $NewPath } +function Set-DefaultPath { + param( + [string]$NewPath + ) + + Set-DefaultVariable PATH $NewPath +} + function Test-MachinePath { param( [string]$PathItem @@ -46,3 +122,15 @@ function Add-MachinePathItem { $newPath = $PathItem + ';' + $currentPath Set-MachinePath -NewPath $newPath } + +function Add-DefaultPathItem { + param( + [string]$PathItem + ) + + Connect-Hive + $currentPath = Get-DefaultPath + $newPath = $PathItem + ';' + $currentPath + Set-DefaultPath -NewPath $newPath + Disconnect-Hive +} diff --git a/images/win/scripts/Installers/Install-DotnetSDK.ps1 b/images/win/scripts/Installers/Install-DotnetSDK.ps1 index b5cd09732..1b802e24f 100644 --- a/images/win/scripts/Installers/Install-DotnetSDK.ps1 +++ b/images/win/scripts/Installers/Install-DotnetSDK.ps1 @@ -110,12 +110,21 @@ function InstallAllValidSdks() function RunPostInstallationSteps() { + # Add dotnet to PATH Add-MachinePathItem "C:\Program Files\dotnet" - # Run script at startup for all users - $cmdDotNet = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "[System.Environment]::SetEnvironmentVariable(''PATH'',"""$env:USERPROFILE\.dotnet\tools;$env:PATH""", ''USER'')"' - # Update Run key to run a script at logon - Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "DOTNETUSERPATH" -Value $cmdDotNet + # Remove NuGet Folder + $nugetPath = "$env:APPDATA\NuGet" + if (Test-Path $nugetPath) { + Remove-Item -Path $nugetPath -Force -Recurse + } + + # Generate and copy new NuGet.Config config + dotnet nuget list source | Out-Null + Copy-Item -Path $nugetPath -Destination C:\Users\Default\AppData\Roaming -Force -Recurse + + # Add %USERPROFILE%\.dotnet\tools to USER PATH + Add-DefaultPathItem "%USERPROFILE%\.dotnet\tools" } InstallAllValidSdks