简体   繁体   English

Powershell Start-Process cmdlet 无法在 TFS Powershell 脚本中工作

[英]Powershell Start-Process cmdlet not working from within a TFS Powershell script

VS / TFS 2017 This post was updated to reflect new information VS / TFS 2017此帖子已更新以反映新信息

There are 2 PowerShell scripts:有 2 个 PowerShell 脚本:

p1.ps1
p2.ps1 

p1 is executed as a PowerShell task in a TFS build process. p1 在 TFS 构建过程中作为 PowerShell 任务执行。 p2 is used twice within p1. p2 在 p1 中使用了两次。

Use 1: p2 is called from p1 , meaning p2 is executed inline使用 1: called from p1 p2 ,意味着 p2 是inline执行的

Use 2: p2 is launched from p1 , meaning p1 does a fire and forget execution of p2, ie as a background task.使用 2: p2 launched from p1 ,这意味着 p1 会fire and forget p2 的执行,即作为后台任务。

Within the TFS build process, use 1 works exactly as expected, use 2 does not.在 TFS 构建过程中,使用 1完全按预期工作,使用 2没有。

Near the end of p1 are the following 2 different sets of code that have been attempted for the fire and forget execution of p2:接近 p1 的末尾是以下 2 组不同的代码,它们已尝试用于fire and forget执行 p2:

$powershellArguments = "-file C:\conf\p2.ps1" , "refresh" , "C:\agent\_work\4\a\for-deploy\website\"
Start-Process powershell.exe -Argument $powershellArguments

And the 2nd methodology:

Start-Job -FilePath $baseFolder"p2.ps1" -ArgumentList "refresh", $sourceRoot

If I manually execute p1 in a Command Prompt PowerShell Window , the fire and forget p2 execution happens as expected with either sets of code above.如果我在Command Prompt PowerShell Window手动执行 p1 ,则上面的任一组代码都会按预期执行fire and forget p2 执行。

But when I run the build and p1 is executed as a task within the build , p2 does not fire and forget .但是当我运行构建并且 p1 作为a task within the build执行时,p2 不会fire and forget Again, to be clear, within the TFS build, the use 1 inline process does execute properly.同样,要明确的是,在 TFS 构建中, use 1 inline进程确实正确执行。

At the head of the script I embedded some code to write a small text file to confirm the script is at least being started and the code also writes out the arguments received to ensure the argument syntax has been used correctly.在脚本的开头,我嵌入了一些代码来编写一个小文本文件,以确认脚本至少正在启动,并且代码还写出收到的参数以确保正确使用了参数语法。 The arguments are passed to p2 properly when p1 is executed outside of the build environment .当 p1 outside of the build environment执行时,参数会正确传递给 p2。 But when p1 is executed as a PowerShell Script within the build , the small text file does not reflect that p2 even started in the fire and forget mode.但是,当P1为PowerShell脚本执行within the build ,小的文本文件并不反映P2甚至在开始fire and forget模式。

It turns out that p1 and p2 are the same script, they just use switches to execute slightly differently.事实证明 p1 和 p2 是同一个脚本,它们只是使用开关来执行略有不同。 But I copied p1 and named it p2 just to separate the 2 scripts and the result is the same.但是我复制了 p1 并将其命名为 p2 只是为了将 2 个脚本分开,结果是相同的。 I can't get p2 to start using the Start-Process cmdlet or the Start-Job cmdlet when p1 is executing within the build .当 p1 within the build执行时,我无法让 p2 开始使用Start-Process cmdletStart-Job cmdlet

I think I've been pretty diligent to narrow this down and it seems there is something about launching a separate script from within a script inside the build process.我想我一直很努力地缩小范围,似乎有一些关于从构建过程中的脚本中启动单独的脚本的事情。

Why might this be and is there a way around this?为什么会这样,有没有办法解决这个问题?

I did wonder if it relates to the basic script policies such as in a batch file that attempts to run a PowerShell script for which we include -executionpolicy remotesigned .我确实想知道它是否与基本脚本策略有关,例如在尝试运行我们包含-executionpolicy remotesigned的 PowerShell 脚本的批处理文件中。 Is that possible?那可能吗?

@Shayik-Abramczyk in the comments was correct.评论中的@Shayik-Abramczyk 是正确的。 When you use Start-Job , if the PowerShell session terminates, any executing jobs started via Start-Job also terminate.当您使用Start-Job ,如果 PowerShell 会话终止,任何通过Start-Job执行的作业也会终止。

There are a few ways you could accomplish what you want.有几种方法可以完成你想要的。 It depends on your particular scenario.这取决于您的特定场景。 You could use Register-ScheduledJob or use Start-Process .您可以使用Register-ScheduledJob或使用Start-Process I'm thinking you probably want Start-Process :我想你可能想要Start-Process

Start-Process PowerShell.exe -ArgumentList "-NonInteractive", "-NoLogo", "-File", ".\$(Join-Path $baseFolder p2.ps1)", 
 "refresh", "$sourceRoot" -NoNewWindow

HTH.哈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM