0

I have this PSSession which returns the Date from a Remote System. Start-Session is a function which collects Host,User and Password and starts a PSSession. $return is the returnvalue

# Start Session
$Session = Start-Session

# Start Commands in Session
$return = Invoke-Command -Session $Session -Scriptblock {

    Get-Date

    }

$return

# Close Session
Remove-PSSession $Session   

If I put the snippet above into a runspace, $Data is empty.

$Runspace = [runspacefactory]::CreateRunspace()
$PowerShell =[Powershell]::Create()
$PowerShell.runspace = $Runspace
$Runspace.Open()

[void]$PowerShell.AddScript({

        # Start Session
        $Session = Start-Session

        # Start Commands in Session
        $return = Invoke-Command -Session $Session -Scriptblock {

            Get-Date

            }

        $return

        # Close Session
        Remove-PSSession $Session                                       
})

$AsyncObject = $PowerShell.BeginInvoke()

$Data = $PowerShell.EndInvoke($AsyncObject)

$Data

$PowerShell.Dispose()

Is this construction possible ? If yes were do I have to look for the returnvalues? At the PSSession or at the runspaceobject ?

thanks in advance.

regards

Dirk

Dirk.R.
  • 1
  • 1
  • Why do you want to reinvent the wheel? There are lots of ways to do that, PoshRSJob, Invoke-Parallel.ps1, PS Jobs, Invoke-Command can do queries in parallel – 4c74356b41 Feb 22 '17 at 13:30
  • Good advice, I forgot to clarify. I'm not allowed to install modules.... Currently my problem is that the GUI freezes if I press a button which invokes a command on a PSsession. So I tried this way putting things in a runspaces to have functional GUI while waiting for results. – Dirk.R. Feb 22 '17 at 13:49

0 Answers0