1

I would like to come up with a mechanism by which I can share 'data' between different Powershell processes. This would be in order to implement a kind of job system, whereby a function can be run in one Powershell process, complete and then someone communicate its status to a function run from another (distinct) Powershell process...

I guess what I'd ideally like psjob results to be shareable between sessions, but this does not seem to be possible.

I can think of a few dirty ways of achieving this (like O/S environment variables), but am I missing an semi-elegant way?

For example:

Function giveMeNumber
{
  $return_vlaue =  Get-Random -Minimum -100 -Maximum 100
  Return $return_vlaue
}

What are some ways i could get this function to store it's return somewhere and then grab it from another Powershell session (without using a database).

Cheers.

hobgadling
  • 131
  • 2
  • 10
  • See if this helps http://stackoverflow.com/questions/13891325/whats-the-best-way-to-pass-values-to-a-running-background-script-block – Keith Hill Jun 10 '14 at 14:25

1 Answers1

1

The QA mentioned by Keith refers to using MSMQ, a message queueing feature optionally available on desktop, mobile & server OS's from Microsoft.

It doesn't run by default on desktop OS's so you would have to ensure that the appropriate service was started. Seems like serious overkill to me unless you wanted something pretty beefy.

Of course, the most common choice for this type of task would be a simple shared file.

Alternatively, you could create a TCP listener in each of the jobs that you want to have accept external info. Not done this myself in PowerShell though I know it is possible. Node.JS would be a more familiar environment or Python. Seems like overkill if a shared file would do the job!

Another way would be to use the registry. Though you might consider that cheating since it is actually a database (of a very broken and simplistic sort).

I'm actually not sure that environment variables would work since I know that they can be picky about the parent environment scope (for example setting an env variable in a cmd doesn't make it available outside of the cmd scope by default.

UPDATE: Doh, missed a few! Some of them very obvious. Microsoft have a list:

  • Clipboard
  • COM
  • Data Copy
  • DDE
  • File Mapping
  • Mailslots
  • Pipes
  • RPC
  • Windows Sockets

Pipes was the one I was trying to remember. Windows sockets would be similar to a TCP listener.

Julian Knight
  • 4,716
  • 2
  • 29
  • 42