mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
[CmdNotFound]Support PowerShell Preview installation (#32034)
* [CommandNotFound] extending to detect PowerShellPreview installations * Re-implementing directory search, where the PowerShell Preview is installed * Re-implementing preview or normal powershell executable usage
This commit is contained in:
@@ -3,8 +3,10 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using global::PowerToys.GPOWrapper;
|
using global::PowerToys.GPOWrapper;
|
||||||
@@ -76,6 +78,9 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
|
|
||||||
private bool _isPowerShell7Detected;
|
private bool _isPowerShell7Detected;
|
||||||
|
|
||||||
|
private bool isPowerShellPreviewDetected;
|
||||||
|
private string powerShellPreviewPath;
|
||||||
|
|
||||||
public bool IsPowerShell7Detected
|
public bool IsPowerShell7Detected
|
||||||
{
|
{
|
||||||
get => _isPowerShell7Detected;
|
get => _isPowerShell7Detected;
|
||||||
@@ -129,6 +134,18 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
get => RuntimeInformation.OSArchitecture == System.Runtime.InteropServices.Architecture.Arm64;
|
get => RuntimeInformation.OSArchitecture == System.Runtime.InteropServices.Architecture.Arm64;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string RunPowerShellOrPreviewScript(string powershellExecutable, string powershellArguments, bool hidePowerShellWindow = false)
|
||||||
|
{
|
||||||
|
if (isPowerShellPreviewDetected)
|
||||||
|
{
|
||||||
|
return RunPowerShellScript(Path.Combine(powerShellPreviewPath, "pwsh-preview.cmd"), powershellArguments, hidePowerShellWindow);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return RunPowerShellScript(powershellExecutable, powershellArguments, hidePowerShellWindow);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string RunPowerShellScript(string powershellExecutable, string powershellArguments, bool hidePowerShellWindow = false)
|
public string RunPowerShellScript(string powershellExecutable, string powershellArguments, bool hidePowerShellWindow = false)
|
||||||
{
|
{
|
||||||
string outputLog = string.Empty;
|
string outputLog = string.Empty;
|
||||||
@@ -160,6 +177,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
|
|
||||||
public void CheckCommandNotFoundRequirements()
|
public void CheckCommandNotFoundRequirements()
|
||||||
{
|
{
|
||||||
|
isPowerShellPreviewDetected = false;
|
||||||
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\CheckCmdNotFoundRequirements.ps1";
|
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\CheckCmdNotFoundRequirements.ps1";
|
||||||
var arguments = $"-NoProfile -NonInteractive -ExecutionPolicy Unrestricted -File \"{ps1File}\"";
|
var arguments = $"-NoProfile -NonInteractive -ExecutionPolicy Unrestricted -File \"{ps1File}\"";
|
||||||
var result = RunPowerShellScript("pwsh.exe", arguments, true);
|
var result = RunPowerShellScript("pwsh.exe", arguments, true);
|
||||||
@@ -179,6 +197,33 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
IsPowerShell7Detected = false;
|
IsPowerShell7Detected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!IsPowerShell7Detected)
|
||||||
|
{
|
||||||
|
// powerShell Preview might be installed, check it.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// we have to search for the directory where the PowerShell preview command is located. It is added to the PATH environment variable, so we have to search for it there
|
||||||
|
foreach (string pathCandidate in Environment.GetEnvironmentVariable("PATH").Split(';'))
|
||||||
|
{
|
||||||
|
if (File.Exists(Path.Combine(pathCandidate, "pwsh-preview.cmd")))
|
||||||
|
{
|
||||||
|
result = RunPowerShellScript(Path.Combine(pathCandidate, "pwsh-preview.cmd"), arguments, true);
|
||||||
|
if (result.Contains("PowerShell 7.4 or greater detected."))
|
||||||
|
{
|
||||||
|
isPowerShellPreviewDetected = true;
|
||||||
|
IsPowerShell7Detected = true;
|
||||||
|
powerShellPreviewPath = pathCandidate;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// nothing to do. No additional PowerShell installation found
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (result.Contains("WinGet Client module detected."))
|
if (result.Contains("WinGet Client module detected."))
|
||||||
{
|
{
|
||||||
IsWinGetClientModuleDetected = true;
|
IsWinGetClientModuleDetected = true;
|
||||||
@@ -204,7 +249,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
{
|
{
|
||||||
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\InstallPowerShell7.ps1";
|
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\InstallPowerShell7.ps1";
|
||||||
var arguments = $"-NoProfile -ExecutionPolicy Unrestricted -File \"{ps1File}\"";
|
var arguments = $"-NoProfile -ExecutionPolicy Unrestricted -File \"{ps1File}\"";
|
||||||
var result = RunPowerShellScript("powershell.exe", arguments);
|
var result = RunPowerShellOrPreviewScript("powershell.exe", arguments);
|
||||||
if (result.Contains("Powershell 7 successfully installed."))
|
if (result.Contains("Powershell 7 successfully installed."))
|
||||||
{
|
{
|
||||||
IsPowerShell7Detected = true;
|
IsPowerShell7Detected = true;
|
||||||
@@ -220,7 +265,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
{
|
{
|
||||||
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\InstallWinGetClientModule.ps1";
|
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\InstallWinGetClientModule.ps1";
|
||||||
var arguments = $"-NoProfile -ExecutionPolicy Unrestricted -File \"{ps1File}\"";
|
var arguments = $"-NoProfile -ExecutionPolicy Unrestricted -File \"{ps1File}\"";
|
||||||
var result = RunPowerShellScript("pwsh.exe", arguments);
|
var result = RunPowerShellOrPreviewScript("pwsh.exe", arguments);
|
||||||
if (result.Contains("WinGet Client module detected."))
|
if (result.Contains("WinGet Client module detected."))
|
||||||
{
|
{
|
||||||
IsWinGetClientModuleDetected = true;
|
IsWinGetClientModuleDetected = true;
|
||||||
@@ -237,7 +282,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
{
|
{
|
||||||
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\EnableModule.ps1";
|
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\EnableModule.ps1";
|
||||||
var arguments = $"-NoProfile -ExecutionPolicy Unrestricted -File \"{ps1File}\" -scriptPath \"{AssemblyDirectory}\\..\"";
|
var arguments = $"-NoProfile -ExecutionPolicy Unrestricted -File \"{ps1File}\" -scriptPath \"{AssemblyDirectory}\\..\"";
|
||||||
var result = RunPowerShellScript("pwsh.exe", arguments);
|
var result = RunPowerShellOrPreviewScript("pwsh.exe", arguments);
|
||||||
|
|
||||||
if (result.Contains("Module is already registered in the profile file.") || result.Contains("Module was successfully registered in the profile file."))
|
if (result.Contains("Module is already registered in the profile file.") || result.Contains("Module was successfully registered in the profile file."))
|
||||||
{
|
{
|
||||||
@@ -252,7 +297,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
{
|
{
|
||||||
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\DisableModule.ps1";
|
var ps1File = AssemblyDirectory + "\\Assets\\Settings\\Scripts\\DisableModule.ps1";
|
||||||
var arguments = $"-NoProfile -ExecutionPolicy Unrestricted -File \"{ps1File}\"";
|
var arguments = $"-NoProfile -ExecutionPolicy Unrestricted -File \"{ps1File}\"";
|
||||||
var result = RunPowerShellScript("pwsh.exe", arguments);
|
var result = RunPowerShellOrPreviewScript("pwsh.exe", arguments);
|
||||||
|
|
||||||
if (result.Contains("Removed the Command Not Found reference from the profile file.") || result.Contains("No instance of Command Not Found was found in the profile file."))
|
if (result.Contains("Removed the Command Not Found reference from the profile file.") || result.Contains("No instance of Command Not Found was found in the profile file."))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user