Showing posts with label RegEx. Show all posts
Showing posts with label RegEx. Show all posts

2015/08/30

PowerShell - Remove special characters from a string using Regular Expression (Regex)

Some more string manipulations! Today I'd like to remove the special characters and only keep alphanumeric characters using Regular Expression (Regex).

You might be interested to check a previous article where I showed how to remove diacritics (accents) from some strings, see here: http://www.lazywinadmin.com/2015/05/powershell-remove-diacritics-accents.html


My goal is to be able to keep only any characters considered as letters and any numbers.
If you are familiar with Regex, you could do something simple as using the metacharacter \w or [a-z] type of things. It's great when you only work with english language but does not work when you have accents or diacritics with Latin languages for example.

Preview of the final solution

2014/09/28

PowerShell Tip - Escape Regex MetaCharacters


Last week I worked on a Scorch PowerShell script that is looking for duplicate Incident Requests inside SCSM by checking new incoming request and existing ticket already in the system.

One of the script step is to look for a match between two strings, something similar to the following:

$String1 = "Title:[PowerShell Rocks!]"
$String2 = "Title:[PowerShell Rocks!]"

$String1 -match $String2

Straight forward you would think! And at first I was surprised to see the result $FALSE ... yep...to "ass-u-me"...

2014/09/06

PowerShell - ConvertFrom-String and the TemplateFile parameter

I'm continuing to play with the new ConvertFrom-String cmdlet (available in the last WMF 5.0 September preview released yesterday) which make the parsing job really easy for simple or complex output.

This cmdlets supports two types of modes: Basic Delimited Parsing (See yesterday's post) and the Auto-Generated Example-Driven Parsing which I will cover in this post.

This Auto-Generated Example-Driven Parsing mode is based on the FlashExtract research work in Microsoft Research...

Important: This post is based on the September 2014 preview release of WMF 5.0. This is pre-release software, so this information may change.

2014/09/05

PowerShell - Playing with the new ConvertFrom-String cmdlet

In a previous post I talked about parsing NetStat.exe using PowerShell and some regex, It is a fun exercice but require some knowledge to figure out how the parsing should happen.

Today, It got way easier !! The PowerShell Team just released a new version of the WMF : v5 September preview ! And One of the coolest feature is the new ConvertFrom-String cmdlet.

EDIT (2014/10/02): See also my post about using ConvertFrom-String and the param -TemplateFile against Netstat.exe

Using the same example from my previous post, I will perform a simple parsing of netstat.exe -n and send the output to ConvertFrom-String.

Important: This post is based on the September 2014 preview release of WMF 5.0. This is pre-release software, so this information may change.

2014/01/01

Top Five 2013 LazyWinAdmin Blog Posts

The year 2013 has been a pretty good year for the LazyWinAdmin blog.
Thank you guys for following my blog and participating to the comments discussions. Thanks also for those who give me advices, critics and ideas to improve my scripts, I think It helps everyone (and myself) to improve our knowledge everyday, Really appreciated :-)

I get more and more page views, comments,... every month  :-) and I thought It would be nice to give a small Top Five 2013 of the LazyWinAdmin blog posts.



Top Five 2013 Blog posts:


WS2012 Storage - NFS Server - Configure NFS for VMware vSphere 5.1 Home Lab
This article focus on how to configure NFS on Windows Server 2012 with PowerShell for a VMware vSphere 5.1 host.


PowerShell Studio 2012 - WinForms - Creating a basic GUI (Video) 
This is a quick tutorial (with a video) on how to create a really basic Winform/Graphical User Interface (GUI) using SAPIEN PowerShell Studio 2012.


PowerShell - Monitor and Report Active Directory Group Membership Change
This is probably one of my most popular script. This PowerShell script let you monitor membership of one or multiple groups. You can specify the names of the group(s) directly, from a file or from an OU path.

PowerShell - Get a SubString out of a String using RegEx 
In 2013 I started playing a bit more with Regex, this post talked about getting text from a string. I'll post more about this subject soon.

PowerShell 4.0 is now available
The team of Jeffery Snover, the PowerShell Team,  realeased a new version of this awesome tool ! more cmdlets!! more fun!!



Thanks for reading! If you have any questions, leave a comment or send me an email at fxcat@lazywinadmin.com. I invite you to follow me on Twitter @lazywinadm / Google+ / LinkedIn. You can also follow the LazyWinAdmin Blog on Facebook Page and Google+ Page.

2013/10/21

PowerShell - Report the AD Missing Subnets from the NETLOGON.log

Today I will share with you a script that report the Missing Subnets detected in the NetLogon file(s) of your Active Directory Domain Controller(s).

Update: See my Github repository for the most recent version

Missing Subnets

When a computer is joined to a domain It knows for sure of which AD domain it is a member. However once the computer is joined to the domain, It may or may not know which AD site it belongs to. Even if it thinks it knows the AD site, it may not even be in the correct AD site (e.g. because it was moved, AD site was renamed, Subnet not declared, Subnet was removed from a site and add to another...etc.).

2013/10/19

PowerShell - Get a SubString out of a String using RegEx

Last week one of my colleague asked me if I could help him with some Regular Expression (Regex) to select some text inside a String.

I don't work a lot with RegEx but when I do, I use tools like PowerRegex from Sapien, RegExr, the technet help for about_Regular_Expressions or RegExlib.com. And to be honest, most of the time I'm trying to avoid it...trying to find a solution the "PowerShell Way"  before trying with Regex...


Problem

So here is what he asked me
Out of the following string "OU=MTL1,OU=CORP,DC=FX,DC=LAB" (Which is a Distinguished Name), he wanted to get the name "MTL1", (SiteCode for Montreal).