As you all Microsoft-sysadmins now know, PowerShell is t3H sh1T everybody is supposed to use, more and more.
Windows guys now have the ever-wanted ability to pipe stuff, we can execute commands in one line (also called “one-liners”), how wonderful (you hear the sarcastic tone, I hope..).
Sometimes these commands are very easy, sometimes they’re a bit trickier, anyway I will record here some of the cool ones I met.
Last example in mind: a colleague asks me to provide him “a list in an usable text file, of the DNS info (static/dynamic record, record type) regarding Linux machines in our domain”.
We have a pretty complex naming convention here. All servers have a prefix of three letters followed by an hyphen, and then some random word to describe the server functionnality. Linux machines have prefixes all beginning with “L” (how strange). Problem is, laptops also begin with “L” (prefix “LP-“). Oh and we also have a few Linux servers with some specific random name as prefix (let’s call it “randomname” here).
Here’s the one-liner:
1 |
PS C:\> Get-DnsServerResourceRecord -ZoneName mydomain.be | ?{$_.Hostname -match '^L.{2}-.*|randomname[a-zA-Z0-9_]*'} |sort Hostname | out-file c:\temp\dns.txt |
I love regexp’s as much as I suck at them, that’s why I’m so proud when I manage to get one to work for what I want! (one is happy with what one can..). So yeah in our example here, “randomname_2” is also taken into account.
This outputs a nice text file looking like the following:
where Timestamp=0 means static record, and the other fields are self-explanatory.
This must be executed on a server with the DNS Powershell cmdlets (easiest way : a DNS server..) and PowerShell version 4 or later (might work with v3, haven’t tested, but certainly doesn’t work with v2).