I have made a PowerShell script to report hourly/daily current running processes via email excluding known processes.
I now want to make this easier to update with new processes added to a list.
Below is an example of the current script:
$Yday = (Get-Date).AddDays(-1)
$pros = Get-Process | Where {($_.StartTime -GT $Yday -and $_.ProcessName -notmatch "chrome|outlook|powershell")}
Output of $pros will contain the results of processes started in the last 24 hours minus the processes chrome, outlook and powershell.
I would like to achieve:
A file called "Known_Processes.txt" containing a list of processes like
chrome outlook powershell
Then using the following script to create the same string of text used to pass through as a filter in the where statement.
$Yday = (Get-Date).AddDays(-1)
[string]$Known_Processes = (Get-Content -Path C:\PS\known_processes.txt | Out-String).Replace("`n", "|").TrimEnd("|")
$pros = Get-Process | Where {($_.StartTime -GT $Yday -and $_.ProcessName -notmatch $Known_Processes)}
The output of this will show all processes including the processes I am trying to filter out even though the variable $known_processes is the same value as "chrome|outlook|powershell".
I have tried searching and the only alternative to getting this to work is to use regex. Whilst I could do this my fear is other admins that do not have PowerShell knowledge could make mistakes when attempting to update the where statement. As where it would be easier for them to insert process names into a text file in a list.