简体   繁体   English

Powershell的Start-Process命令不会在Powershell ise之外启动exe

[英]Powershell's Start-Process command doesn't start exe anywere outside Powershell ise

I'm writing simple script to unarchive (rar) a project from Teamcenter to temp directory, then run specific program (Mentor), then archive again. 我正在编写简单的脚本,以将Teamcenter的项目取消存档(rar)到temp目录,然后运行特定程序(Mentor),然后再次存档。

I've read a lot of examples about starting exe from PS, but they mostly relate to small exes like notepad, without dlls and other resources. 我已经阅读了很多有关从PS启动exe的示例,但它们大多与小型exe有关,例如记事本,没有dll和其他资源。

In Powershell Ise the script works perfectly. 在Powershell Ise中,脚本可以完美运行。 But when I call the script from teamcenter, Mentor is missing dlls. 但是,当我从teamcenter调用脚本时,Mentor缺少dll。

Before I run Mentor, in the script, I do: 在运行Mentor之前,请在脚本中执行以下操作:

Get-ChildItem Env:

to check environment variables and all variables exist. 检查环境变量和所有变量是否存在。 I tried to set environments manually, like this: 我尝试手动设置环境,如下所示:

$wf_classpath = Get-ChildItem Env:WF_CLASSPATH
[System.Environment]::SetEnvironmentVariable("WF_CLASSPATH", $wf_classpath.Value, "Process")

Does not work. 不起作用。

I tried to set homefolder: 我试图设置homefolder:

$mentor = Start-Process $file.FullName -Wait -WorkingDirectory $workdir

Does not work. 不起作用。

Then I tried to call a batch file from the script with environments, does not work. 然后,我尝试从具有环境的脚本中调用批处理文件,无法正常工作。

Try call cmd.exe /c ... does not work. 尝试调用cmd.exe /c ...不起作用。

Full script here, works perfect only in Powershell Ise, if I call the script from other programs, exe does not start. 此处的完整脚本只有在Powershell Ise中才能正常运行,如果我从其他程序调用该脚本,则exe无法启动。

$shell = new-object -com shell.application
$invocation = $MyInvocation.MyCommand.Definition
$rootpath = $PSScriptRoot
$outpath = "$($PSScriptRoot)\out"
$pathtorar = "c:\Siemens\menutils\Rar.exe"

Remove-Item -Recurse -Force $outpath
New-Item $outpath -ItemType directory

$archive = get-childitem $rootpath | where { $_.extension -eq ".rar" } | Select-Object -First 1
$arglist = "x $($archive.FullName) $($outpath)"
Start-Process -FilePath $pathtorar -ArgumentList $arglist -Wait

Remove-Item -Recurse -Force $archive.FullName

$file = get-childitem $outpath -Recurse | where { $_.extension -eq ".prj" } | Select-Object -First 1
Write-Host "$(get-date -Format yyyy-MM-dd-hh-ss)
Start process: $($file.FullName)"
$mentor = Start-Process $file.FullName -Wait

$arglist = "a -m0 -r -ep1 $($archive.FullName) $($outpath)"
Start-Process -FilePath $pathtorar -ArgumentList $arglist -Wait

Remove-Item -Recurse -Force $outpath

Read-Host -Prompt "Press Enter to exit"

What's the difference between running the script from Powershell Ise and other programs? 从Powershell Ise和其他程序运行脚本之间有什么区别?

How should I set environment variables to run the script from other scripts/programs? 如何设置环境变量以从其他脚本/程序运行脚本?

Its probably that your Current directory is not correct and WorkingDirectory in my experience is buggy. 根据我的经验,您的当前目录可能不正确, WorkingDirectory可能有问题。 The dll's will be obtained from the current directory if they are not at the regular system paths. 如果dll不在常规系统路径中,则将从当前目录中获取dll。

Use this function before Start-Process Start-Process之前使用此功能

    [IO.Directory]::SetCurrentDirectory($Dir)  

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

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