【问题标题】:Calling a Powershell Runspace Scriptblock from a remote file从远程文件调用 Powershell 运行空间脚本块
【发布时间】:2019-05-30 12:57:24
【问题描述】:

我正在开发一个从 GUI 向导开始的项目,以从用户那里获取一些信息,例如端点 FQDN、用户名、密码等。

一旦用户提供了所有必需的信息并到达 GUI 中的最后一页,就会准备好开始按钮供他单击。单击此开始按钮后,该工具将调用位于不同文件中的 32 个不同的脚本。

为了使 GUI 更具响应性并克服挂起问题以及添加多线程和更好的性能,我使用了 Runspace 和 RunspcaePools。所以 GUI 本身在运行空间中运行,一旦用户单击启动,就会创建一个运行空间池,并且对于 32 个脚本中的每个脚本,都会创建和调用一个 Powershell 会话。

图形用户界面工作正常,一切都按预期工作,直到开始按钮。我的问题是,当我将脚本添加到 PowerShell 会话时,我无法调用这些脚本文件。这只是没有发生。如果我使用 Wait-Debugger,我看不到任何错误,就好像我传递给 Powershell 会话的 ScriptBlock(它具有我要执行的脚本的路径的值)只是一个刺痛而不是 ScriptBlock .

我的代码如下:

    #   This Section will add all the required Subscript Files into variables to be called by the main code of the tool.
    $SubScriptFolderPath = $CurrentWorkingDirectory + "SubCode\Scripts\"
    $SubscriptFilePath = @{}
    $SubscriptFilePath.BasicVspherePowerCliData = $SubScriptFolderPath + "BasicVspherePowerCliData.ps1"
    $SubscriptFilePath.NsxManagerPowerCliData = $SubScriptFolderPath + "NsxManagerPowerCliData.ps1"
    $SubscriptFilePath.NsxManagerPowerNsxData = $SubScriptFolderPath + "NsxManagerPowerNsxData.ps1"
    $SubscriptFilePath.NsxManagerNsxCliData = $SubScriptFolderPath + "NsxManagerNsxCliData.ps1"
    $SubscriptFilePath.ControllerPowerCliData = $SubScriptFolderPath + "ControllerPowerCliData.ps1"
    $SubscriptFilePath.ControllerPowerNsxData = $SubScriptFolderPath + "ControllerPowerNsxData.ps1"
    $SubscriptFilePath.ControllerNsxCliData = $SubScriptFolderPath + "ControllerNsxCliData.ps1"
    $SubscriptFilePath.ControllerSshData = $SubScriptFolderPath + "ControllerSshData.ps1"
    $SubscriptFilePath.ClusterPowerCliData = $SubScriptFolderPath + "ClusterPowerCliData.ps1"
    $SubscriptFilePath.ClusterPowerNsxData = $SubScriptFolderPath + "ClusterPowerNsxData.ps1"
    $SubscriptFilePath.ClusterNsxApiData = $SubScriptFolderPath + "ClusterNsxApiData.ps1"
    $SubscriptFilePath.HostPowerCliData = $SubScriptFolderPath + "HostPowerCliData.ps1"
    $SubscriptFilePath.HostPowerNsxData = $SubScriptFolderPath + "HostPowerNsxData.ps1"
    $SubscriptFilePath.HostEsxCliData = $SubScriptFolderPath + "HostEsxCliData.ps1"
    $SubscriptFilePath.HostNsxCliData = $SubScriptFolderPath + "HostNsxCliData.ps1"
    $SubscriptFilePath.DvsPowerCliData = $SubScriptFolderPath + "DvsPowerCliData.ps1"
    $SubscriptFilePath.DvsPowerNsxData = $SubScriptFolderPath + "DvsPowerNsxData.ps1"
    $SubscriptFilePath.LogicalSwitchPowerCliData = $SubScriptFolderPath + "LogicalSwitchPowerCliData.ps1"
    $SubscriptFilePath.LogicalSwitchPowerNsxData = $SubScriptFolderPath + "LogicalSwitchPowerNsxData.ps1"
    $SubscriptFilePath.TransportZonePowerCliData = $SubScriptFolderPath + "TransportZonePowerCliData.ps1"
    $SubscriptFilePath.TransportZonePowerNsxData = $SubScriptFolderPath + "TransportZonePowerNsxData.ps1"
    $SubscriptFilePath.DlrPowerCliData = $SubScriptFolderPath + "DlrPowerCliData.ps1"
    $SubscriptFilePath.DlrPowerNsxData = $SubScriptFolderPath + "DlrPowerNsxData.ps1"
    $SubscriptFilePath.DlrNsxCliData = $SubScriptFolderPath + "DlrNsxCliData.ps1"
    $SubscriptFilePath.EsgPowerCliData = $SubScriptFolderPath + "EsgPowerCliData.ps1"
    $SubscriptFilePath.EsgPowerNsxData = $SubScriptFolderPath + "EsgPowerNsxData.ps1"
    $SubscriptFilePath.EsgNsxCliData = $SubScriptFolderPath + "EsgNsxCliData.ps1"
    $SubscriptFilePath.DfwPowerCliData = $SubScriptFolderPath + "DfwPowerCliData.ps1"
    $SubscriptFilePath.DfwPowerNSXData = $SubScriptFolderPath + "DfwPowerNSXData.ps1"
    $SubscriptFilePath.DumpEndPoints = $SubScriptFolderPath + "DumpEndPoints.ps1"


    #   Create  Sync HashTable (GuiHash) to allow readability across different Runscpases and add required variables.
    $Global:GuiHash = [hashtable]::Synchronized(@{ })
    $Global:GuiHash.IPCehckPattern = $IPRegex   #   Add the IPCehckPattern Variable to be used within other runspaces
    $Global:GuiHash.SubscriptFilePath = $SubscriptFilePath  #   Add the SubCodePaths Variable to be used within other runspaces

    #   Crate the Runspace that will be used for the GUI and configure settings. After, Open the Runspace and import variables.
    #   You must import variables that will be used in other Runspaces, thus importing the required variables to this Runspace.
    $GuiRunspace =[runspacefactory]::CreateRunspace()
    $GuiRunspace.ApartmentState = "STA"
    $GuiRunspace.ThreadOptions = "ReuseThread"         
    $GuiRunspace.Open()
    $GuiRunspace.SessionStateProxy.SetVariable("GuiHash",$Global:GuiHash)  
    $GuiRunspace.SessionStateProxy.SetVariable("XamlFilesArray",$XamlFilesArray)

    #   Create a PowerShell Session and add it to the Runspace.
    $GuiPSSession = [PowerShell]::Create()
    $GuiPSSession.Runspace = $GuiRunspace

    #   Create the GUI PowerShell Session ScriptBlock. This will be the main code of the tool.
    [void]$GuiPSSession.AddScript({

          [Some code that is taking care of GUI]

          $GuiHash.WizardFinalPageStart.Add_Click({

               #    Create Session State Object and Add GuiHash Variable to it
               $InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
               $RunHash = New-object System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'RunHash',$Global:GuiHash,$Null
               $InitialSessionState.Variables.Add($RunHash)
               #  Create Runspace Pool
               $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, [int]$env:NUMBER_OF_PROCESSORS + 1, $InitialSessionState, $Host)
               $RunspacePool.ApartmentState = "MTA"  
               $RunspacePool.Open()
               ForEach ($ScriptBlock in $Global:GuiHash.SubscriptFilePath) {
                    $PowerShellSession = [PowerShell]::Create()
                    $PowerShellSession.RunspacePool = $RunspacePool 
                    $PowerShellSession.AddScript({$ScriptBlock})
                    $PowerShellSession.BeginInvoke()
               }
          })

        #   Show Wizard Window.
        $Global:GuiHash.WizardMainWindow.ShowDialog() | Out-Null
    })  

    $ShowGui = $GuiPSSession.BeginInvoke()

我尝试了很多方法,例如:

  • 将 & 添加到脚本路径的开头。

  • 添加 .到脚本路径的开头。

  • 使用获取内容

  • 使用 ScriptBlock 系统对象

    $Scriptblock = [scriptblock]::Create((Get-Content $ScriptPath))

似乎没有任何效果。 非常感谢您对此提供的帮助。

谢谢

【问题讨论】:

    标签: wpf powershell runspace


    【解决方案1】:

    我想我又犯了同样的错误,对不起,这个项目有时让我心烦意乱。调用脚本块时需要删除括号。所以你需要如下调用脚本块

    $PowerShellSession.AddScript($ScriptBlock)
    

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多