Quick tip: reversed regex.

When working with regular expressions in PowerShell to check if certain string matches a pattern we can use it in both directions: to match or not match it (with –match and –notmatch operators). That’s very handy and it saves us a lot of time, because we don’t have to come up with regular expression that would do it for us. But there are certain situations where operators are not used.

Continue reading

Case-insensitive Select-Xml

Internet-xmlPowerShell is not case sensitive (most of the time). There are two situation when it is: you explicitly request it (e.g. using operators like –cmatch) or we depend on something where there is no easy way to turn it off. Select-Xml is using XPath and falls in the second category. Whenever I need to query large XML file I prefer to start with more general queries, and there is very few things more frustrating than being forced to remember case of e.g. attribute names. There are two options to go about it.

First of all we can do it easy way: change case of the whole string that defines XML, and be sure, that everything is either lower or upper case. Problem with this approach is the fact, that result we get back is different from original, and we are bit “stuck” with “broken” XML.

Second option is to use XPath own tools to get similar results, and has advantage of getting “proper” XML data back. Using Select-Xml also enables modifying XML and saving under different name.

Continue reading

Quick tip: human-readable LDAP filter

Whenever I work with Active Directory and I want to use pretty complex LDAP Filter to search for objects I’m after I’m tempted to use line break here and there to make whole thing easier on eyes of future reviewer (even if the only person to read the code is me “few weeks later”).

Continue reading

Quick tip: regex groups and ‘-split’

If you use –split as often as I do you probably had occasional issue with understanding why certain splits work as expected, and some produce results different from what you anticipated. Recently one of MVPs, Oisin Grehan, shed some light on the reason why this is the case. I thought it might be worth sharing in case you’ve had this issue and you are pulling your hair trying to figure out what’s going on.

Continue reading

Managing Linux via OMI: Packaging

PowerShell-Control-Linux-Packaging

This is sixth and final part of the series. You can find outline of the whole series here. We already have OMI server up, running and accessible from outside and we have test Lin_Process implemented on it, with several properties and single method: Kill. Today we will build our own CDXML-based module that will surround remote CIM calls in cmdlet-like functions. This package won’t be auto-generated, I had no luck finding procedure to do that (truth be told: I haven’t tried hard and wanted to learn CDXML concepts anyway). We move back to PowerShell than…

Continue reading

Managing Linux via OMI: Implementation (2)

PowerShell-Control-Linux-Implementation-2

This is fifth part of the series. You can find outline of the whole series here. We already have OMI server up, running and accessible from outside and we have Lin_Process class implemented, with two properties: PID and cmdline. Today we will extend our schema to few other properties and we will add a method to it. Unfortunately, it means more amateur C++ code, but also more fun and information retrieved remotely from our test, CentOS box.

Continue reading

Managing Linux via OMI: Implementation (1)

PowerShell-Control-Linux-Implementation-1

This is fourth part of the series. You can find outline of the whole series here. We already have OMI server up, running and accessible from outside and we have test class implemented on it. Time to create something useful. We start with processes because they are objects very convenient for demo purposes. Normally we would create class that is based on existing one, e.g. CIM_UnixProcess. But before we do it right, we will try to create something that at least works and does what we need. Why? Well, if you look at CIM_UnixProcess schema you will find plenty of keys there, not to mention other properties that would require values at certain point. We will come back to this later, when we know what those values should be.

Continue reading