diff --git a/src/Aiplugs.PoshApp/Services/Powershell/PowershellWorker.cs b/src/Aiplugs.PoshApp/Services/Powershell/PowershellWorker.cs index 632abe6..10a224a 100644 --- a/src/Aiplugs.PoshApp/Services/Powershell/PowershellWorker.cs +++ b/src/Aiplugs.PoshApp/Services/Powershell/PowershellWorker.cs @@ -67,6 +67,8 @@ private async Task TryInvokeCommand(Runspace runspace) if (content == null) return; + var workingDir = await _service.GetScriptDir(invokeCommand.ScriptId); + runspace.SessionStateProxy.Path.SetLocation(workingDir); using var ps = PowerShell.Create(runspace); ps.Streams.Error.DataAdding += (sender, args) => { diff --git a/src/Aiplugs.PoshApp/Services/ScriptsService.cs b/src/Aiplugs.PoshApp/Services/ScriptsService.cs index adf9cdd..504f81c 100644 --- a/src/Aiplugs.PoshApp/Services/ScriptsService.cs +++ b/src/Aiplugs.PoshApp/Services/ScriptsService.cs @@ -251,5 +251,36 @@ public async Task RemoveScript(Repository repository, string id) _semaphore.Release(); } } + + public async Task GetScriptDir(string scriptId) + { + var splited = scriptId.Split(':'); + + if (splited.Length != 2) + throw new ArgumentException(nameof(scriptId)); + + var repositoryName = splited[0]; + + var scriptName = splited[1]; + + var repository = await GetRepository(repositoryName); + + if (repository == null) + return null; + + var script = await GetScript(repository, scriptName); + + if (script == null) + return null; + + return GetScriptDir(repository, script); + } + + public string GetScriptDir(Repository repository, Script script) + { + var path = GetScriptPath(repository, script.Path); + + return Path.GetDirectoryName(path); + } } } \ No newline at end of file