从远程文件调用Powershell Runspace脚本块

时间:2019-05-30 12:57:24

标签: wpf powershell runspace

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

一旦用户提供了所有必需的信息并到达GUI的最后一页,即可开始单击启动按钮。单击此“开始”按钮后,该工具将调用位于单独文件中的32个不同脚本。

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

GUI正常运行,并且一切正常,直到启动按钮为止。我的问题是,当我将脚本添加到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()

我尝试了很多事情,例如:

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

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

  • 使用Get-Content

  • 使用ScriptBlock系统对象

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

似乎没有任何效果。 感谢您在此方面的帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

我想我又犯了同样的错误,对不起,这个项目使我的头脑有时挂起。调用脚本块时需要删除括号。因此您需要按如下所示调用脚本块

$PowerShellSession.AddScript($ScriptBlock)