PowerShell Start-Job 工作目录 [英] PowerShell Start-Job Working Directory

查看:56
本文介绍了PowerShell Start-Job 工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法为 Start-Job 命令指定工作目录?

Is there a way to specify a working directory to the Start-Job command?

用例:

我在一个目录中,我想使用 Emacs 打开一个文件进行编辑.如果我直接执行此操作,它将阻止 PowerShell,直到我关闭 Emacs.但是使用 Start-Job 会尝试从我的主目录运行 Emacs,从而让 Emacs 打开一个新文件而不是我想要的文件.

I'm in a directory, and I want to open a file using Emacs for editing. If I do this directly, it will block PowerShell until I close Emacs. But using Start-Job attempts to run Emacs from my home directory, thus having Emacs open a new file instead of the one I wanted.

我尝试使用 $pwd 指定完整路径,但脚本块中的变量直到它们在 Start-Job 上下文中执行时才会解析.因此,在 shell 上下文中强制解析变量的某种方法也是可以接受的答案.

I tried to specify the full path using $pwd, but variables in the script block are not resolved until they're executing in the Start-Job context. So some way to force resolving the variables in the shell context would also be an acceptable answer to this.

所以,这是我尝试过的,只是为了完整性:

So, here's what I've tried, just for completeness:

Start-Job { emacs RandomFile.txt }
Start-Job { emacs "$pwd/RandomFile.txt" }

推荐答案

一个可能的解决方案是创建一个踢球脚本":

A possible solution would be to create a "kicker-script":

Start-Job -filepath .\emacs.ps1 -ArgumentList $workingdir, "RandomFile.txt"

您的脚本如下所示:

Set-Location $args[0]
emacs $args[1]

希望这会有所帮助.

这篇关于PowerShell Start-Job 工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆