Showing posts with label Select-String. Show all posts
Showing posts with label Select-String. Show all posts

Monday, October 15, 2012

Let’s select the String using Select-String in Powershell.

Hi,

In my previous post i have write a little article on how to view to see the contents of files.

As you know that i like Text files and my most of the day spent on PowerShell console so i love to do different different examples with cmdlets and command.

On my laptop i have a simple test file name as “Seen Movies.txt”, i this i mainly put the name of all movies which i have seen on my laptop and whose video files i do have stored on my NAS drive. SO when i need to get any movie i check this file to know that if i already have that movie or not. In case i have a movies entry in the file then i don’t get or purchase that Movies and if i don’t have entry of that movie then i can think of buying it if i am interested to watch that movie.

So what’s special ? “we can do the same by open the file in notepad and then we can search for that movie”.

Yes agreed but remember there are lots of way to do one thing, Smile

To check that i have that movie or not i just run a simple command.

Get-Content FILE_NAME | Select-String “Movie Name”

Using Get-content we are getting the content of the files and the we are piping the output to Select-String Cmdlet and then selecting to show the string “Avenger”

12-10-2012 00-16-01

The above command showed me that i have “The Avengers” movies in my database.

ok but what is the real use?

The real use is that you can search for specific words in log files.

Get-Content C:\Windows\Logs\CBS\CBS.log | Select-String "error"

In above command i am getting the content of C:\Windows\Logs\CBS\CBS.log  and piped them to the Select-String cmdlet and asking it match everything which contain the word “Error”

12-10-2012 00-35-39

If you want to do a search on case sensitive words then you can use -CaseSensitive parameter with Select-String cmdlet

12-10-2012 00-39-39

Simple isn’t Smile  and this is very handy and useful cmdlet Smile .

 

Thanks

Aman Dhally

join aman on facebook Join aman on Linkedin follow aman on Twitter

Friday, May 11, 2012

Powershell and Active Directory: Find the group memberships of a Domain User using PowerShell.

 

Hi,

I hope that you guys are enjoying our "Powershell and Active Directory" series. Today for some reason i need to find the group membership of a domain user and send the list of all of that detail to my IT Manager.

Normally we open "DSA.MSC" or "Active Directory user and Computers" , search the user, go to his user account properties , find the membership tab and note/write down the list of his all group membership to a paper then put it to a Excel. If a user is subscribed to 100 of groups then? Obviously we "IT peoples are know for our laziness"  we are not going to do it ,, I am right isnt? .

Then i thought let's do it again with PowerShell. :)

we are using Get-ADUser cmdlet to get the membership of the group of a particular user.

Lets Start.

 Make sure you have "RSAT" installed on you laptop.

Now Import the Active Directory module.

Import-Module ActiveDirectory

30-04-2012 23-21-03 

..

ok, Module is imported,,

ok now run Get-ADUser cmdlet with username of the user whose Group Membership you want to see.

Get-ADUser Aman.Dhally

11-05-2012 13-34-37

Ok..it's not showing the Group member list.. it wont until in -Properties we select the MemberOf property of Get-ADUser

Run the below command it will show you the list of all properties which domain user "aman.dhally" have.

Get-ADUser Aman.Dhally -Properties *

You will notice that it also have the MemberOf property.

11-05-2012 13-41-02

lets access only MemBerOf property of domain user using Dot(.) notation and grouping.

The below command show us only the Domain user "MemberOf" property.

(Get-ADUser Aman.Dhally -Properties *).MemberOf

this will give you the detailed list of Group membership in LDAP like pattern, like

CN=Singers,OU=Demo4,OU=Groups,DC=XYX,DC=com
CN=Songs,OU=Demo3,OU=Groups,DC=XYZ,DC=com

If you are happy with this that is OK,, but then you have to remove all clutter manually ...   "CN" is contains our group name .. let's filter it more using Powershell.

11-05-2012 13-51-06

You can see  above the output of MemberOf is separated by Comma (,) let's split the output using -Split  parameter.

(Get-ADUser Aman.Dhally -Properties *).MemberOf -split ","

11-05-2012 14-34-49 

Ok.. now what???, now we need to select only CN names,,, Simple.. we can use Select-String cmdlet to select only CN Names...

(Get-ADUser Aman.Dhally -Properties *).MemberOf -split (",")  | Select-String -SimpleMatch "CN="

11-05-2012 14-38-30 

Great.. Now it showing only Name of the our Groups. but it have "CN=" in the from of it...

Do you want to filter more ???

Yes, Ok..

Lets replace "CN=", with nothing,, we can use -Replace parameter .. To user -Replace paramter we need to put above command in to Subexpression and after SubExpression we can use -Replace Parameter.

$((Get-ADUser Aman.Dhally -Properties *).MemberOf -split (",")  | Select-String -SimpleMatch "CN=") -replace "CN=",""

Wow , finally i have the clean, filtered list of my domain user group memberships.

11-05-2012 14-44-15

Hope you like it..

Happy Weekends.

Thanks

Aman Dhally

Aman Dhally