From 80073ee15033252f6557f23c6bb6369cb075770b Mon Sep 17 00:00:00 2001 From: Mavaddat Javid Date: Fri, 18 Feb 2022 14:36:35 -0800 Subject: [PATCH 1/3] Admin privileges required for PackageManagement PowerShell requires admin privileges for installing or updating PackageManagement modules. See https://github.com/PowerShell/PowerShell/issues/12777 --- .../Services/PowerShell/Handlers/GetVersionHandler.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs b/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs index f8a6d5521..feb965ec2 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs @@ -91,10 +91,12 @@ private async Task CheckPackageManagement() } _logger.LogDebug("Old version of PackageManagement detected."); + var isAdmin = var isAdmin = System.Security.Principal.WindowsIdentity.GetCurrent().IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator); if (_runspaceContext.CurrentRunspace.Runspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage) { - _languageServer.Window.ShowWarning("You have an older version of PackageManagement known to cause issues with the PowerShell extension. Please run the following command in a new Windows PowerShell session and then restart the PowerShell extension: `Install-Module PackageManagement -Force -AllowClobber -MinimumVersion 1.4.6`"); + var sessionRec = if(isAdmin) { "a new" } else { "an admin" }; + _languageServer.Window.ShowWarning($"You have an older version of PackageManagement known to cause issues with the PowerShell extension. Please run the following command in {sessionRec} Windows PowerShell session and then restart the PowerShell extension: `Install-Module PackageManagement -Force -AllowClobber -MinimumVersion 1.4.6`"); return; } @@ -120,7 +122,12 @@ private async Task CheckPackageManagement() // If the user chose "Not now" ignore it for the rest of the session. if (messageAction?.Title == takeActionText) { - var command = new PSCommand().AddScript("powershell.exe -NoLogo -NoProfile -Command 'Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber'"); + var command; + if(isAdmin) { + command = new PSCommand().AddScript("powershell.exe -NoLogo -NonInteractive -NoProfile -Command 'Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber'"); + } else { + command = new PSCommand().AddScript("Start-Process -FilePath powershell.exe -ArgumentList @('-NoLogo','-NoProfile','-NonInteractive','-Command \"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber -Repository PSGallery\"') -Verb RunAs"); + } await _executionService.ExecutePSCommandAsync( command, From 4109859e6feb549e55f02a6deeb7749fbf8abdd9 Mon Sep 17 00:00:00 2001 From: Mavaddat Javid Date: Sun, 20 Feb 2022 15:14:42 -0800 Subject: [PATCH 2/3] Pre-reqs install See https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget?view=powershell-5.1 --- .../Services/PowerShell/Handlers/GetVersionHandler.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs b/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs index feb965ec2..05f26f38f 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs @@ -124,9 +124,9 @@ private async Task CheckPackageManagement() { var command; if(isAdmin) { - command = new PSCommand().AddScript("powershell.exe -NoLogo -NonInteractive -NoProfile -Command 'Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber'"); + command = new PSCommand().AddScript("powershell.exe -NoLogo -NonInteractive -NoProfile -Command '[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Install-PackageProvider -Name NuGet -Force; Install-Module -Name PowerShellGet -Force; Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber -Repository PSGallery'"); } else { - command = new PSCommand().AddScript("Start-Process -FilePath powershell.exe -ArgumentList @('-NoLogo','-NoProfile','-NonInteractive','-Command \"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber -Repository PSGallery\"') -Verb RunAs"); + command = new PSCommand().AddScript("Start-Process -FilePath powershell.exe -ArgumentList @('-NoLogo','-NoProfile','-NonInteractive','-Command \"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Install-PackageProvider -Name NuGet -Force; Install-Module -Name PowerShellGet -Force; Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber -Repository PSGallery\"') -Verb RunAs"); } await _executionService.ExecutePSCommandAsync( From b6e72492e16a45b22103e89110bfc7ec88e1f432 Mon Sep 17 00:00:00 2001 From: Mavaddat Javid Date: Sun, 20 Feb 2022 15:21:04 -0800 Subject: [PATCH 3/3] testing shows must rid -scope currentuser --- .../Services/PowerShell/Handlers/GetVersionHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs b/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs index 05f26f38f..6915733e8 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetVersionHandler.cs @@ -126,7 +126,7 @@ private async Task CheckPackageManagement() if(isAdmin) { command = new PSCommand().AddScript("powershell.exe -NoLogo -NonInteractive -NoProfile -Command '[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Install-PackageProvider -Name NuGet -Force; Install-Module -Name PowerShellGet -Force; Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber -Repository PSGallery'"); } else { - command = new PSCommand().AddScript("Start-Process -FilePath powershell.exe -ArgumentList @('-NoLogo','-NoProfile','-NonInteractive','-Command \"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Install-PackageProvider -Name NuGet -Force; Install-Module -Name PowerShellGet -Force; Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -Scope CurrentUser -AllowClobber -Repository PSGallery\"') -Verb RunAs"); + command = new PSCommand().AddScript("Start-Process -FilePath powershell.exe -ArgumentList @('-NoLogo','-NoProfile','-NonInteractive','-Command \"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Install-PackageProvider -Name NuGet -Force; Install-Module -Name PowerShellGet -Force; Install-Module -Name PackageManagement -Force -MinimumVersion 1.4.6 -AllowClobber -Repository PSGallery\"') -Verb RunAs"); } await _executionService.ExecutePSCommandAsync(