Featured image of post T-SQL Tuesday #143: Short code examples

T-SQL Tuesday #143: Short code examples

A quick code snippet to help find members of the local admin group.

T-SQL Tuesday Logo

Well folks, it’s Wednesday here in the UK, which means I’m a day late to get my blog post in for T-SQL Tuesday. However, if I was in Hawaii it would be still Tuesday so let’s go for it…

I used a handy short script this morning and I figured it was worth a quick, late entry! Hopefully John Mccormack (b|t), will forgive me for stretching the deadline!

First of all, shout out to John for hosting the monthly blog party, he has got a great prompt and I’m really excited to see the wrap-up post as I’m sure it’ll be full of great little code snippets.

T-SQL Tuesday this month is going back to basics and its all about code. I’d like to know “What are your go to handy short scripts”?

This morning I was working on pulling together some information which included whether certain accounts were in the local administrator’s group on some remote servers. I had the perfect snippet saved in my code repo so I was quickly able to answer that question – and then I realised I should share that with you all.

The following PowerShell snippet uses the net localgroup command line tool to retrieve the results and parse them so we just get the account names.  The final line includes the -ComputerName parameter so you can easily run it against remote machines.

1
2
3
4
Invoke-Command -ScriptBlock { net localgroup administrators |
    Where-Object { $_ -AND $_ -notmatch "command completed successfully" } |
    Select -skip 4
} -ComputerName mssql1

Hope this comes in handy, and sorry again John for sneaking in late.

Built with Hugo
Theme Stack designed by Jimmy