设置每个运行空间的环境

时间:2017-08-09 13:04:48

标签: vb.net multithreading powershell

我目前正在开发一个从SQL表中提取任务并执行PowerShell脚本的应用程序。我想在开头运行SetUP.ps1来设置像工作目录等变量,最后只需要tearDown.ps1进行清理。

目前我正在使用:

Using myRunSpace = RunspaceFactory.CreateRunspace
  myRunSpace.Open()

  Using ps = PowerShell.Create
    ps.Runspace = myRunSpace
    ps.AddScript("Set-ExecutionPolicy -Scope Process RemoteSigned")
    ps.Invoke()
  End Using


  Using ps = PowerShell.Create
    ps.Runspace = myRunSpace
    ps.AddScript(Application.StartupPath & "\SetUp.ps1")
    ps.Invoke()
  End Using

  Using ps = PowerShell.Create
    ps.Runspace = myRunSpace
    ps.AddScript(task.Script)
    ReturnValue = PSSerializer.Serialize(ps.Invoke())
  End Using

  Using ps = PowerShell.Create
    ps.Runspace = myRunSpace
    ps.AddScript(Application.StartupPath & "\TearDown.ps1")
    ps.Invoke()
  End Using
End Using

Setup.ps1我正在做的事情:

$env:test = Get-Random

当我现在使用代码片段运行多线程上面的代码作为代码时:

Start-Sleep 10; $env:test

然后所有跑步给我相同的价值。 $env:test在每次运行中都是相同的。有没有办法如何将$env:test的范围限制为这一个空间?

0 个答案:

没有答案
相关问题