Avatar of Aludaan
AludaanFlag for United States of America asked on

Access a currently running PowerShell script from another program

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.
Powershell

Avatar of undefined
Last Comment
Dustin Saunders

8/22/2022 - Mon
SOLUTION
Dustin Saunders

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Aludaan

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.
ASKER CERTIFIED SOLUTION
Aludaan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Dustin Saunders

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.

Kindly,

Dustin
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck