From 7bce1653f3ef8aa33f56fe8d7700eee23fa9755e Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 7 Jul 2017 09:43:37 -0700 Subject: [PATCH] Add Windows PowerShell PSModulePath by default on Windows (#4132) This change is only for Windows and appends the Windows PowerShell PSModulePath on startup via a default profile. Depending on the data/feedback we get, we can decide what to do (opt-in vs opt-out) as we get closer to a release candidate. --- build.psm1 | 12 +++++++++--- .../Microsoft.PowerShell_profile.ps1 | 10 ++++++++++ test/powershell/Host/ConsoleHost.Tests.ps1 | 6 +++--- .../Out-Default.Tests.ps1 | 8 ++++---- .../powershell/engine/Module/ModulePath.Tests.ps1 | 15 +++++++++++++++ 5 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 src/powershell-win-core/Microsoft.PowerShell_profile.ps1 diff --git a/build.psm1 b/build.psm1 index 22523e138c..de972a2238 100644 --- a/build.psm1 +++ b/build.psm1 @@ -191,12 +191,12 @@ function Start-PSBuild { # verify we have all tools in place to do the build $precheck = precheck 'dotnet' "Build dependency 'dotnet' not found in PATH. Run Start-PSBootstrap. Also see: https://dotnet.github.io/getting-started/" - $dotnetCLIIntalledVersion = (dotnet --version) - If ( $dotnetCLIIntalledVersion -ne $dotnetCLIRequiredVersion ) { + $dotnetCLIInstalledVersion = (dotnet --version) + If ( $dotnetCLIInstalledVersion -ne $dotnetCLIRequiredVersion ) { Write-Warning @" The currently installed .NET Command Line Tools is not the required version. -Installed version: $dotnetCLIIntalledVersion +Installed version: $dotnetCLIInstalledVersion Required version: $dotnetCLIRequiredVersion Fix steps: @@ -472,6 +472,12 @@ cmd.exe /C cd /d "$location" "&" "$($vcVarsPath)\vcvarsall.bat" "$NativeHostArch Pop-Location } + # copy PowerShell host profile if Windows + if ($IsWindows) + { + Copy-Item -Path "$PSScriptRoot/src/powershell-win-core/Microsoft.PowerShell_profile.ps1" -Destination $publishPath -Force + } + # download modules from powershell gallery. # - PowerShellGet, PackageManagement, Microsoft.PowerShell.Archive if($PSModuleRestore) diff --git a/src/powershell-win-core/Microsoft.PowerShell_profile.ps1 b/src/powershell-win-core/Microsoft.PowerShell_profile.ps1 new file mode 100644 index 0000000000..df6ea82ee8 --- /dev/null +++ b/src/powershell-win-core/Microsoft.PowerShell_profile.ps1 @@ -0,0 +1,10 @@ +# Default profile for all users for PowerShell host + +if ($IsWindows) +{ + # Add Windows PowerShell PSModulePath to make it easier to discover potentially compatible PowerShell modules + # If a Windows PowerShell module works or not, please provide feedback at https://github.com/PowerShell/PowerShell/issues/4062 + + Write-Warning "Appended Windows PowerShell PSModulePath" + $env:psmodulepath += ";${env:userprofile}\Documents\WindowsPowerShell\Modules;${env:programfiles}\WindowsPowerShell\Modules;${env:windir}\system32\WindowsPowerShell\v1.0\Modules\" +} diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 7fb582a75d..1182d4a03e 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -179,7 +179,7 @@ Describe "ConsoleHost unit tests" -tags "Feature" { It "-File should be default parameter" { Set-Content -Path $testdrive/test -Value "'hello'" - $observed = & $powershell $testdrive/test + $observed = & $powershell -NoProfile $testdrive/test $observed | Should Be "hello" } @@ -189,13 +189,13 @@ Describe "ConsoleHost unit tests" -tags "Feature" { ) { param($Filename) Set-Content -Path $testdrive/$Filename -Value "'hello'" - $observed = & $powershell -File $testdrive/$Filename + $observed = & $powershell -NoProfile -File $testdrive/$Filename $observed | Should Be "hello" } It "-File should pass additional arguments to script" { Set-Content -Path $testdrive/script.ps1 -Value 'foreach($arg in $args){$arg}' - $observed = & $powershell $testdrive/script.ps1 foo bar + $observed = & $powershell -NoProfile $testdrive/script.ps1 foo bar $observed.Count | Should Be 2 $observed[0] | Should Be "foo" $observed[1] | Should Be "bar" diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Default.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Default.Tests.ps1 index fa8ddc3dd0..9da136ea0b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Default.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Default.Tests.ps1 @@ -14,16 +14,16 @@ Describe "Out-Default Tests" -tag CI { `$null = Stop-Transcript "@ - & $powershell -c $script | Should BeExactly 'bye' + & $powershell -noprofile -command $script | Should BeExactly 'bye' "TestDrive:\transcript.txt" | Should Contain 'hello' } It "Out-Default reverts transcription state when used more than once in a pipeline" { - & $powershell -c "Out-Default -Transcript | Out-Default -Transcript; 'Hello'" | Should BeExactly "Hello" + & $powershell -noprofile -command "Out-Default -Transcript | Out-Default -Transcript; 'Hello'" | Should BeExactly "Hello" } It "Out-Default reverts transcription state when exception occurs in pipeline" { - & $powershell -c "try { & { throw } | Out-Default -Transcript } catch {}; 'Hello'" | Should BeExactly "Hello" + & $powershell -noprofile -command "try { & { throw } | Out-Default -Transcript } catch {}; 'Hello'" | Should BeExactly "Hello" } It "Out-Default reverts transcription state even if Dispose() isn't called" { @@ -35,6 +35,6 @@ Describe "Out-Default Tests" -tag CI { [GC]::WaitForPendingFinalizers(); 'hello' "@ - & $powershell -c $script | Should BeExactly 'hello' + & $powershell -noprofile -command $script | Should BeExactly 'hello' } } diff --git a/test/powershell/engine/Module/ModulePath.Tests.ps1 b/test/powershell/engine/Module/ModulePath.Tests.ps1 index 0fe8b80597..728d5355dd 100644 --- a/test/powershell/engine/Module/ModulePath.Tests.ps1 +++ b/test/powershell/engine/Module/ModulePath.Tests.ps1 @@ -93,4 +93,19 @@ Describe "SxS Module Path Basic Tests" -tags "CI" { $paths -contains $fakePSHomeModuleDir | Should Be $true $paths -contains $customeModules | Should Be $true } + + It "Default PowerShell profile appends Windows PowerShell PSModulePath only on Windows" { + + $psmodulepath = & $powershell -nologo -c '$env:PSModulePath' + + if ($IsWindows) + { + $psmodulepath[0] | Should Match "Warning" # for Windows, there is a warning that path being appended + $psmodulepath[1] | Should Match "WindowsPowerShell" + } + else + { + $psmodulepath[0] | Should Not Match "WindowsPowerShell" + } + } }