Skip to content
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

Fix: Use working directory when running PowerShell scripts #16837

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/Files.App/Actions/Content/Run/RunWithPowershellAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public RunWithPowershellAction()

public Task ExecuteAsync(object? parameter = null)
{
return Win32Helper.RunPowershellCommandAsync($"& '{context.ShellPage?.SlimContentPage?.SelectedItem?.ItemPath}'", PowerShellExecutionOptions.None);
return Win32Helper.RunPowershellCommandAsync(
$"& '{context.ShellPage?.SlimContentPage?.SelectedItem?.ItemPath}'",
PowerShellExecutionOptions.None,
context.Folder?.ItemPath
);
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down
7 changes: 7 additions & 0 deletions src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ async Task InvokeShellMenuItemAsync(ContextMenu contextMenu, object? tag)
await Win32Helper.OpenFormatDriveDialog(drivePath);
break;

case "Windows.PowerShell.Run":
await contextMenu.InvokeItem(
menuId,
contextMenu.ItemsPath[0].EndsWith(".ps1") ? Path.GetDirectoryName(contextMenu.ItemsPath[0]) : null
);
break;

default:
await contextMenu.InvokeItem(menuId);
break;
Expand Down
14 changes: 9 additions & 5 deletions src/Files.App/Helpers/Win32/Win32Helper.Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ public static string ExtractStringFromDLL(string file, int number)
}
}

public static async Task<bool> RunPowershellCommandAsync(string command, PowerShellExecutionOptions options)
public static async Task<bool> RunPowershellCommandAsync(string command, PowerShellExecutionOptions options, string? workingDirectory = null)
{
using Process process = CreatePowershellProcess(command, options);
using Process process = CreatePowershellProcess(command, options, workingDirectory);
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(30 * 1000));

try
Expand All @@ -432,11 +432,11 @@ public static async Task<bool> RunPowershellCommandAsync(string command, PowerSh
}
}

public static bool RunPowershellCommand(string command, PowerShellExecutionOptions options)
public static bool RunPowershellCommand(string command, PowerShellExecutionOptions options, string? workingDirectory = null)
{
try
{
using Process process = CreatePowershellProcess(command, options);
using Process process = CreatePowershellProcess(command, options, workingDirectory);

process.Start();

Expand Down Expand Up @@ -867,7 +867,7 @@ public static async Task InstallFontsAsync(string[] fontFilePaths, bool forAllUs
await RunPowershellCommandAsync(psCommand.Append("\"").ToString(), PowerShellExecutionOptions.Elevated | PowerShellExecutionOptions.Hidden);
}

private static Process CreatePowershellProcess(string command, PowerShellExecutionOptions options)
private static Process CreatePowershellProcess(string command, PowerShellExecutionOptions options, string? workingDirectory = null)
{
Process process = new();

Expand All @@ -883,6 +883,10 @@ private static Process CreatePowershellProcess(string command, PowerShellExecuti
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
}

if (workingDirectory is not null)
process.StartInfo.WorkingDirectory = workingDirectory;

process.StartInfo.Arguments = command;

return process;
Expand Down
4 changes: 3 additions & 1 deletion src/Files.App/Utils/Shell/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public async Task<bool> InvokeVerb(string? verb)
return false;
}

public async Task<bool> InvokeItem(int itemID)
public async Task<bool> InvokeItem(int itemID, string? workingDirectory = null)
{
if (itemID < 0)
return false;
Expand All @@ -100,6 +100,8 @@ public async Task<bool> InvokeItem(int itemID)
};

pici.cbSize = (uint)Marshal.SizeOf(pici);
if (workingDirectory is not null)
pici.lpDirectoryW = workingDirectory;

await _owningThread.PostMethod(() => _cMenu.InvokeCommand(pici));
Win32Helper.BringToForeground(currentWindows);
Expand Down
Loading