Skip to content

Admin privileges required for PackageManagement fixes #1721 #1717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 '[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 -AllowClobber -Repository PSGallery\"') -Verb RunAs");
}

await _executionService.ExecutePSCommandAsync(
command,
Expand Down