none
PowerShell Runspaces RRS feed

  • Question

  • Hi,

    I have a WPF GUI that I want to be responsive, this means using runspaces for commands. I have the following test runspace:

    $RHIWindow.ShowDialog() | Out-Null
    
    
    $computerHostName = "PCNAME"
            
    $searchRunspace = [runspacefactory]::CreateRunspace()
    $searchRunspace.ApartmentState = "STA"
    $searchRunspace.ThreadOptions = "ReuseThread"
    $searchRunspace.Open()
    $searchRunspace.SessionStateProxy.SetVariable("computerHostName",$computerHostName)
    
    $searchPowerShell = [powershell]::Create().AddScript({
    
        
           $getadcom = Get-ADComputer -Identity $computerHostName
           $testwsman = Test-WSMan -ComputerName $computerHostName -ErrorAction SilentlyContinue
         
     
    })
    
    $searchPowerShell.Runspace = $searchRunspace
    $SearchObject = $searchPowerShell.BeginInvoke()
    
    do{
        Start-Sleep -Milliseconds 100
    }while(!$SearchObject.IsCompleted)
    
    
    $searchData = $searchPowerShell.EndInvoke($SearchObject)
    Write-Host $searchData
    
    $searchPowerShell.Dispose()

    Is there a way for me to return both of these values so that I can check if they are null or not? Both are being returned at the same time so I can't seem to check them.

    Any help is much appreciated :) 


    Friday, March 8, 2019 5:23 PM

All replies

  • WPF does not require a runspace.  A runspace is only required if you need to run multiple independent windows or forms.

    WPF as a script works just like a regular Windows Form once the controls are all bound to variables.


    \_(ツ)_/

    Friday, March 8, 2019 8:52 PM
  • To return multiple variables you need to use a "synchash".  Search for PowerShell synchash" for examples of various ways to set this up and use it.

    You can also just use multiple "SetVariable statements but variables don't necessarily sync correctly.


    \_(ツ)_/


    • Edited by jrv Friday, March 8, 2019 8:55 PM
    Friday, March 8, 2019 8:54 PM