We help IT Professionals succeed at work.

Access a currently running PowerShell script from another program

Aludaan asked
1 Endorsement
Last Modified: 2018-12-05
I am not sure how to make this clear so hopefully I ask this correctly. I have programmed in a few different languages but I am new to PowerShell. I am trying to find out if there is a way to have a PowerShell script running in the background waiting for some variables from another application which it will process. Nothing needs to be returned to the application calling the PowerShell script.

So what I am doing right now is I have a program that when a certain event occurs it will trigger a script. The program will pass the same 5 fields to the script every time. The script then runs a SQL query and loads every row from a database which contains a list of possible matches for the event that occurred based off all 5 fields. It then processes each row eliminating the rows that don't match the criteria.

So for example say an event occurs, it will trigger a script as follows:
script1.ps1 data1 data2 data3 data4 data5

The important part of why I am asking is script1 starts by accessing our database and loading 1,000 rows of data with 5 fields for each row. This is important because this is the same data almost every time and it seems like it would be more efficient if I could just keep it in memory.

The script then begins to step through each row discarding each row that does not match.

So if data1=field1 then keep it else discard it. At the end of this we may have 100 rows left and then it will process the next set of data the same way. If data2=field2 then keep it else discard it. And I do the same for all 5 fields further narrowing down the results each time. At the end there is stuff I do based off the remaining results which aren't important.

Occasionally the 1,000 rows of data on the SQL server which we load at the start of the script will be changed (and to be clear it is not always exactly 1,000 rows, I was using that for my example) so it is not the same EVERY time but it changes rarely. Perhaps once per day or less.

So rather than load the 1,000 rows at the start of the script every time and then discarding it again just to reload the same data I was thinking I could just have the program run in the background with the 1,000 rows pre-loaded and access it directly. If that is the case then instead of using a database I would switch it to a CSV file which would be more convenient for us and just use a hash to identify if the file has changed and if not continue to use the rows already in memory. If it has changed then dump the existing array and reload the data accordingly.

I can't show you code as I have not programmed this yet because I wanted to find out if it was possible before coding it but please feel free to ask any questions you need to try and help clarify my endless ramblings above.
Watch Question

Dustin SaundersCo-Founder and Chief Architect
Top Expert 2016
This one is on us!
(Get your first solution completely free - no credit card required)


Because we use pattern matching which SQL does not support.

So for a really short example the database, which doesn't change too much, could look like this:
RowNum  DeviceName    KeyField     GroupField  EmailList
======  ==========    ========     ==========  =========
1       device1.*     .*           .*          user
2       device2.*     /mount/.*    .*          os team
3       device2.*     .*           .*          user
4       device[34].*  /u[01][0-9]  .*          database team
5       device[34].*  .*           CPU         os team
6       device[34].*  .*           .*          user

Open in new window

Then the following events could come in and the script would load the whole table from above and then compare it to the event that came in.

This event would match row 6:
DeviceName    KeyField     GroupField
==========    ========     ==========
device4       /usr         null

Open in new window

This event would match row 4:
DeviceName    KeyField     GroupField
==========    ========     ==========
device3       /u12         null

Open in new window

We do have more complicated patterns as well but this is a good example.
This one is on us!
(Get your first solution completely free - no credit card required)
Dustin SaundersCo-Founder and Chief Architect
Top Expert 2016

Glad you were able to get it working-- yes MySQL has native regex functions and SQL Server can implement PATINDEX.  Shouldn't feel ashamed, it's all part of the process, that's why resources exist where you can bounce a problem off others in the industry.



Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.