powershell where-object specific number of numeric characters - parsing

I looked through some similar question but couldn't find an example of this sort of logic- what im looking for is a way to parse through a large return of usernames in powershell. User names that are not service accounts or kiosks have a specific naming convention of 6 grouped numeric characters: for example- username123456. I was thinking there has to be a way of using the where-object command but can't find anything on the syntax on technet or other PS resources. The one liner to get a return from the local domain is:
Search-ADAccount -PasswordNeverExpires | Select-Object Name, Enabled
This returns a list of domain accounts that have the password set to never expire. I want to only see the ones with the above naming convention ^^ I know you can achieve this with regex but my regex is quite rusty---if it can be done with where-object that would be optimal. I also looked into using pattern, but it seems to be for more complex returns than this... Thanks!

Are they under a specific OU? You could then use the -Searchbase parameter to limit the scope of the search to that OU.
Otherwise, Regex is probably going to be a good option to accomplish this.
Search-ADAccount -PasswordNeverExpires | Select-Object Name, Enabled | Where {
$_.name -match '\D\d{6}$'
}

Related

Rails ActiveRecord - find by substring of another string

id | name
1 | jack
2 | tomas
I want to find a row if the name is a substring of Ttomas.
So the result should be
id | name
2 | tomas
Is this possible?
Yes, this is possible in Ruby on Rails and SQL. Depends a bit on the database you use, but something like this should work:
Modelname.where("? LIKE CONCAT('%', name, '%')", 'Ttomas')
It's possible.
The brute-force way would be to iterate through all your users and check if name is a substring. But this horribly inefficient.
If you want to utilize SQL lookups, you need to look into gems like pg_search or elasticsearch for full search functionality.

In Sumo Logic, how to search for logs matching a regular expression?

I'm trying to do a Sumo Logic search for logs matching the following regular expression:
"Authorization \d+ for story is not voided. Story not removed"
That is, the \d+ consists of one or more digits, but it doesn't matter what they are exactly.
Based on the search examples cheat sheet (https://help.sumologic.com/05Search/Search-Cheat-Sheets/General-Search-Examples-Cheat-Sheet), I've tried to use a * | parse regex pattern for this, but that doesn't work:
I get a 'No capture group found in regex' error. I'm actually not really interested in capturing the digits, though, just in matching the regular expression in my search. How can I achieve this?
I managed to get it to work in two ways. Firstly, using the regular parse instead of parse regex:
* | parse "Authorization * for story is not voided. Story not removed" as id |
count by _sourceHost | sort by _count
or, when using a regular expression, it needs to be a named group:
* | parse regex "Authorization (?<id>\d+) for story is not voided. Story not removed" |
count by _sourceHost | sort by _count

Ruby variable in Postgresql Regex

I have a table named orders with a column named comment in it.
I have a list of order ids, and I want to search through all the comment to see which order has referenced them.
So far I have worked out the regex part of the sql
Order.where('comment ~ \'\s\d{5}\' OR comment ~ \'^\d{5}\'')
.where("comment LIKE '%?%'", order_id)
But I can't seem to work out a way to make the query more flexible to find order_ids that have different length other than 5
Is it possible to make {5} more dynamic, to something like {#{order_id.to_s.length}}?
UPDATE
Just ran into this post
Using a boundary match would work even better for this case.
Order.where('comment ~ \'\y?\y\'', order_id)
Would do the trick.
We can pass digit_count
digit_count = order_id.to_s.length
Order.where('comment ~ \'\s\d{?}\' OR comment ~ \'^\d{?}\'', digit_count, digit_count)
if you are looking for specific order_id
Order.where("comment ~ \'\s#{order_id}\s\'")

GREP create a list of words that contain a sting

I have a folder with a lot of text files and would like to get a list of all words in that folder that contain a certain string. So, e.g. there is words in the form of 'IND:abc', 'IND:cde', ... and I am looking for a way to get a list of all words starting with IND:, so something like:
[IND:abc, IND:cde, IND:...]
Can grep do that?
Cheers,
Chris
grep -ho 'IND:\w\+' * | sort | uniq
-h suppresses the filenames so that you will only get the text. -o prints only the matching path of the text. If you want to see the duplicates just remove the sort, and uniq.

Can't compare SQL powershell property to string

I first want to say that I am just learning powershell and this might be a very simple answer, but I have done some research and can't find the exact thing I am looking for.
My Goal:
I am using the SQL cmdlets to try and exclude some databases that we need to run scripts on. Below is my code to get the databases
$ExcludeDBs=Invoke-Sqlcmd -ServerInstance $EDDSServer -Database EDDS -InputFile $InactiveCaseSQLPath
This works fine, it is after this when I am trying to use a foreach loop and try to compare the properties so I can determine which SQL cluster it is on. Below is my code:
$str="__SQL Cluster 13 (ALL NEW CASES HERE)"
$ExcludeDbs | foreach{
if($ExcludeDBs | Where-Object SQLclusterName -eq $str){
$SQLClusterName="ClusterNameSQL"}
}#end foreach loop
I get the following error:
Cannot bind parameter 'FilterScript'. Cannot convert the "SQLclusterName" value of type "System.String" to type "System.Management.Autom
ation.ScriptBlock".
I think this has something to do with type casting, but I am not sure where to go from here. Any help would be very much appreciated.
SQLClustername is being handled as a string - is it a field being returned from your query?
Also, you shouldn't need the where-object inside the foreach
$str="__SQL Cluster 13 (ALL NEW CASES HERE)"
$ExcludeDbs | foreach{
if($_.SQLclusterName -eq $str){
$SQLClusterName="ClusterNameSQL"}
}#end foreach loop

Resources