【问题标题】:Powershell's Start-Process command doesn't start exe anywere outside Powershell isePowershell 的 Start-Process 命令不会在 Powershell 之外启动 exe
【发布时间】:2015-04-15 13:26:41
【问题描述】:

我正在编写简单的脚本来将 Teamcenter 中的项目解压缩 (rar) 到临时目录,然后运行特定程序 (Mentor),然后再次存档。

我已经阅读了很多关于从 PS 启动 exe 的示例,但它们大多与记事本等小型 exe 相关,没有 dll 和其他资源。

在 Powershell Ise 中,脚本运行良好。但是当我从 teamcenter 调用脚本时,Mentor 缺少 dll。

在我运行 Mentor 之前,我会在脚本中执行以下操作:

Get-ChildItem Env:

检查环境变量和所有变量是否存在。我尝试手动设置环境,如下所示:

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

不起作用。

我尝试设置主文件夹:

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

不起作用。

然后我尝试从脚本中调用带有环境的批处理文件,不起作用。

尝试调用cmd.exe /c ... 不起作用。

这里的完整脚本,仅在 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"

从 Powershell Ise 运行脚本和其他程序有什么区别?

我应该如何设置环境变量以从其他脚本/程序运行脚本?

【问题讨论】:

    标签: windows powershell batch-file environment-variables


    【解决方案1】:

    这可能是您的当前目录不正确,而根据我的经验,WorkingDirectory 是错误的。如果 dll 不在常规系统路径中,则将从当前目录获取 dll。

    Start-Process之前使用这个函数

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

    【讨论】:

    • 谢谢,明天试试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2012-01-14
    • 2016-01-18
    相关资源
    最近更新 更多