有什么方法可以知道使用 start-job -filename 启动时正在运行什么脚本?
我正在使用类似以下命令的命令启动 PowerShell 作业:
start-job -filename my_script.ps1 -argumentlist ($v1, $v2, $v3)
但是,此脚本需要知道它所在的位置,因为它根据相对于它的位置运行其他命令。当直接从提示符运行时,诸如以下的构造可以工作:
join-path (split-path (& { $myinvocation.scriptname })) "relative path\filename"
join-path (split-path $myinvocation.mycommand.definition) "relative path\filename"
但是,当像第一个示例中那样作为作业启动时,这根本不起作用。当我开始工作时,如何确定我要从哪里开始?
I'm starting a PowerShell job with something like the following command:
start-job -filename my_script.ps1 -argumentlist ($v1, $v2, $v3)
This script, however needs to know where it's located, because it runs other commands based on their location relative to it. When run directly from the prompt, constructs such as these work:
join-path (split-path (& { $myinvocation.scriptname })) "relative path\filename"
join-path (split-path $myinvocation.mycommand.definition) "relative path\filename"
This doesn't work at all when started as a job as in the first example, however. How can I determine where I'm running from when I'm started as a job?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎类似于远程处理情况,其中文件作为脚本块传递到作业中,因此脚本来自哪个文件的概念丢失了。您也可以传递路径(尽管这似乎不太理想):
It appears to be like the remoting case where the file is passed into the job as a script block so the notion of which file the script came from gets lost. You could pass in the path as well (although that seems less than ideal):
更新:
我遇到了这个代码示例:
http://blog.brianhartsock.com/2010/05/22/a-better-start-job-cmdlet/
为了使用 FilePath 参数,我将其修改为这样,而不是 ScriptBlock :
这将被称为:
使用 -Init (-InitializationScript) 并触发 Set-Location 将当前执行进程移动到脚本的目录,因此,从那里您可以确定相对位置。
正如博客文章提到的,您可以将其作为外部脚本(我将其测试为 Start-JobAt.ps1)或将其作为您的用户/服务帐户的配置文件的一部分。
UPDATED:
I came across this code example:
http://blog.brianhartsock.com/2010/05/22/a-better-start-job-cmdlet/
Rather than a ScriptBlock, to use the FilePath parameter, I modified it as such:
This would be called as:
Using -Init (-InitializationScript) and firing off the Set-Location moves the current executing process to the directory of the script, so, from there you can determine relative location.
As the blog post mentions, you could have this as an external script (I tested mine as Start-JobAt.ps1) or make it part of your user/service account's profile.