powershell - 无法在同一个 PowerShell session 中运行同一个脚本两次

标签 powershell sas enterprise-guide

我正在编写一个 PowerShell 脚本,它将打开一个应用程序,运行一个进程,然后关闭它,然后打开另一个应用程序,运行一个进程,然后关闭它。 (最终超过两个,但我从小开始。)

我可以运行以下脚本,没有任何问题:

# Project Config
$projectName = "c:\temp\test3.egp"
$projectName2= "c:\temp\test4.egp"

# Start Enterprise Guide
$app = New-Object -ComObject SASEGObjectModel.Application.7.1

#Open the Enterprise Guide Project
$projectObject = $app.Open($projectName, "")

# Save the Enterprise Guide Project
$projectObject.Save()

# Close the Enterprise Guide Project
$projectObject.Close()

# Open the second project
$projectObject = $app.Open($projectName2, "")

# Save the second project
$projectObject.Save()

#Close the second project
$projectObject.Close()

# Quit Enterprise Guide
$app.Quit()

PS C:\temp> ./test.ps1
PS C:\temp>

但是,如果我在没有退出 PowerShell 的情况下在上述提示下第二次运行它,则会出现错误。

PS C:\temp> ./test.ps1
Exception calling "Open" with "2" argument(s): "Path is not a directory 'C:': The filename, directory name, or volume label syntax is incorrect."
At C:\temp\test.ps1:9 char:27
    + $projectObject = $app.Open <<<< ($projectName, "")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation
... more errors related to $projectObject not having a value, and then a second copy of each of these for the second attempt ... 

PS C:\temp>

如果我退出 Powershell,然后重新打开 Powershell,然后再次运行它,它会正常工作。

记录的限制主要包括一次只能打开一个项目;但是,显然 $projectObject.Close() 工作正常,因为它确实成功地在一个脚本中运行了两个。只有当我离开脚本并再次运行它时,它才是一个问题。

现在,如果我删除 $app.Quit(),它然后允许我在同一 session 中再次运行它;但我确实想退出应用程序(以确保脚本的任何后续运行都不会受到我刚刚所做的任何事情的影响;这是一个编程环境,所以设置 session 宏变量等会产生不好的影响)。我也不完全理解为什么行 $app = New-Object ... 没有创建应用程序的新实例(以及为什么之前运行的 $app quit 是相关的)?

我在 PowerShell 中有什么地方做错了吗?或者这仅仅是我使用的 API 的问题,我必须与 SAS(供应商)交谈?

最佳答案

在您原来的 PS session 中为每个项目创建一个新的 PS session :New-PSSession

可以在 Microsoft 文档站点上找到更多详细信息和示例 here .

关于powershell - 无法在同一个 PowerShell session 中运行同一个脚本两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45171730/

相关文章:

c# - UI 自动化 cmdlet 找不到控件

sas - 终止整个流程而不是单个程序

Powershell import-csv - 自动识别定界符?

function - PowerShell 将多个参数传递给类方法失败

powershell - Installshield - 使用 powershell 检查注册表中的 key 失败

sas - 在 SAS Guide 中注释所选代码的快捷方式

sas - 如何停止出现在企业指南中的表格?

python-3.x - 读取具有固定列宽的 .dat 文件

sas - 解析表中的所有列名并将其放入宏变量中(SAS 9.4)

sql - 使用proc sql将多个不同结构的表堆叠到一张表中