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.
If info from a text appears once, twice or 3 times
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.
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.
Re: If info from a text appears once, twice or 3 times
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
}
if($count -ge 1 -and $count -le 3){
... send the email
}
Re: If info from a text appears once, twice or 3 times
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
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
Re: If info from a text appears once, twice or 3 times
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.
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 }
Re: If info from a text appears once, twice or 3 times
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.