有什么方法可以知道使用 start-job -filename 启动时正在运行什么脚本?

发布于 2024-10-03 13:42:41 字数 448 浏览 4 评论 0原文

我正在使用类似以下命令的命令启动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事↓在人 2024-10-10 13:42:41

这似乎类似于远程处理情况,其中文件作为脚本块传递到作业中,因此脚本来自哪个文件的概念丢失了。您也可以传递路径(尽管这似乎不太理想):

PS> gc .\job.ps1
param($scriptPath)
"Running script $scriptPath"
PS> $job = Start-Job -FilePath .\job.ps1 -ArgumentList $pwd\job.ps1
PS> Wait-Job $job

Id  Name            State      HasMoreData     Location  Command
--  ----            -----      -----------     --------  -------
13  Job13           Completed  True            localhost param($scriptPath)...


PS> Receive-Job $job.id
Running script C:\Users\hillr\job.ps1

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):

PS> gc .\job.ps1
param($scriptPath)
"Running script $scriptPath"
PS> $job = Start-Job -FilePath .\job.ps1 -ArgumentList $pwd\job.ps1
PS> Wait-Job $job

Id  Name            State      HasMoreData     Location  Command
--  ----            -----      -----------     --------  -------
13  Job13           Completed  True            localhost param($scriptPath)...


PS> Receive-Job $job.id
Running script C:\Users\hillr\job.ps1
蓝梦月影 2024-10-10 13:42:41

更新:

我遇到了这个代码示例:

http://blog.brianhartsock.com/2010/05/22/a-better-start-job-cmdlet/

为了使用 FilePath 参数,我将其修改为这样,而不是 ScriptBlock :

param([string]$file)

$filePath = split-path $file -parent
Start-Job -Init ([ScriptBlock]::Create("Set-Location $filePath")) -FilePath $file -ArgumentList $args

这将被称为:

Start-JobAt c:\full_path\to\file\my_script.ps1 ($v1, $v2, $v3)

使用 -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:

param([string]$file)

$filePath = split-path $file -parent
Start-Job -Init ([ScriptBlock]::Create("Set-Location $filePath")) -FilePath $file -ArgumentList $args

This would be called as:

Start-JobAt c:\full_path\to\file\my_script.ps1 ($v1, $v2, $v3)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文