简体   繁体   中英

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.

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.

In Powershell Ise the script works perfectly. But when I call the script from teamcenter, Mentor is missing dlls.

Before I run Mentor, in the script, I do:

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:

$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.

Full script here, works perfect only in Powershell Ise, if I call the script from other programs, exe does not start.

$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?

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. The dll's will be obtained from the current directory if they are not at the regular system paths.

Use this function before Start-Process

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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