If info from a text appears once, twice or 3 times

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 10 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
juneb1022
Posts: 34
Last visit: Fri Sep 30, 2016 8:08 am

If info from a text appears once, twice or 3 times

Post by juneb1022 »

Hi..

I am writing a script that is creating a csv file. Im at the end of the script where I need to send an email if a username appears only once, twice or 3 times. Any guidance is appreciated.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: If info from a text appears once, twice or 3 times

Post by jvierra »

Why can't you count the incidents of the username and use that to conditionally send an email. Just check the count is between 1 and 3.

if($count -ge 1 -and $count -le 3){
... send the email
}
User avatar
juneb1022
Posts: 34
Last visit: Fri Sep 30, 2016 8:08 am

Re: If info from a text appears once, twice or 3 times

Post by juneb1022 »

Thank you, yes. I have copied the script below so I can word my question more appropriately. The contents of the $WindowsOutFile2 has 5 columns: ("CSName","Name","SessionId","Owner","Logon"). If the owner column contains any username once, twice, or three times then send the email. The part I am stuck on is how do I do this particualr search?

get-wmiobject -class Win32_Process -ComputerName (Get-Content $Servers.txt) | Select CSName,Name,SessionId,@{name="Owner";expression={$_.GetOwner().user}},@{name="Logon";Expression={$_.ConvertToDateTime($_.creationdate)}} | Export-Csv $WindowsOutFile -Notypeinformation

get-content $WindowsOutFile | where {$_ -notmatch "Network Service" -and $_ -notmatch "Local Service" -and $_ -notmatch "System"} | Set-Content $WindowsOutFile2
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: If info from a text appears once, twice or 3 times

Post by jvierra »

Why are you writing the file out and then reading it right back again. That wont help you count things.

Start with this until you understand how to capture the info and how to process it conditionally.
PowerShell Code
Double-click the code block to select all.
$processes=get-wmiobject -class Win32_Process | 
     Select CSName,Name,SessionId,
     @{name="Owner";expression={$_.GetOwner().user}},
     @{name="Logon";Expression={$_.ConvertToDateTime($_.creationdate)}}

$processes |
     Group-Object Owner|
     Where-Object{
          $_.Count -ge 1 -and $_.count -le 3
     }
User avatar
juneb1022
Posts: 34
Last visit: Fri Sep 30, 2016 8:08 am

Re: If info from a text appears once, twice or 3 times

Post by juneb1022 »

So simple, thank you very much for clearing this up for me. I think my syntax was fomratted incrrorectly, as I used the group-object. Very cool.
This topic is 10 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked