PowerShell for Admins

Abstraction and Configuration Data

pscookiemonster
2 min read
Share:

Modularity and abstraction are a huge benefit in scripting and coding. Which of the following blocks of code are easier to understand?

$SQLConnection = New-Object System.Data.SqlClient.SQLConnection $SQLConnection.ConnectionString = 'Server=SqlServer1;Database=MyDB;Integrated Security=True;Connect Timeout=15' $cmd = New-Object system.Data.SqlClient.SqlCommand("SELECT * FROM Table1",$SQLConnection) $ds = New-Object system.Data.DataSet $da = New-Object system.Data.SqlClient.SqlDataAdapter($cmd) [void]$da.fill($ds) $SQLConnection.Close() $ds.Tables[0] Or…

# Invoke-Sqlcmd2 -ServerInstance SQLServer1 -Database MyDB -Query 'SELECT * FROM Table1' If you aren’t a masochist, the latter probably looks a bit nicer. Oh, and it offers other parameters, error handling, parameterized SQL queries, built in help, and other benefits the .NET code block misses.

The takeaway? You should be writing or using Advanced Functions and Modules, not monolithic scripts and snippets. Do it for yourself. Do it for anyone who might have to read your code down the line.

Some modules can benefit from persistent configurations. If you have a module that wraps a REST API, you might want to allow the end user to specify a default URL, rather than specify it every time they run a command.

This begs the question: what data format should you use? XML? JSON? YAML? INI?

This is a quick hit on options for storing configuration data in PowerShell.

Don’t be ashamed. Many of us sysadmins pride ourselves on learning through experience. That doesn’t mean you need to re-invent all the wheels. It can be a great learning experience to write your own code and functions, but at the end of the day, there’s nothing wrong with finding the best tool for the job, and sticking with it. Developers make a living writing code, yet they all borrow existing libraries.

Once you start writing modules and advanced functions, be sure to share them with the community!

Related Articles

Sep 15, 2023

PowerShell Escape Room

PowerShell Escape Room by Michiel Hamers by Michiel Hamers https://about.me/michielhamers/ Why on earth you want to create an Escape Room with PowerShell as backend? I’ve always been a fan of escape rooms, so I decided to create my own for my kids. I wanted to make it something that would be challenging and fun for them, but also educational. I decided to use PowerShell as the backend for the escape room, as I’m a PowerShell developer and I thought it would be a great way to learn more about the language.

Sep 15, 2023

Microsoft Graph PowerShell Module: Getting Started Guide

Microsoft Graph PowerShell Module: Getting Started Guide by Jeff Brown Microsoft is retiring the Azure AD Graph API sometime after June 30, 2023 (announcement). This retirement includes the Azure AD PowerShell module. In its place, Microsoft has released the Microsoft Graph PowerShell module. The Microsoft Graph PowerShell module is the next-generation way of managing Microsoft cloud services using PowerShell. If you have used MSOnline or Azure AD PowerShell in the past, you’ll need to read on to learn about this new module.

Oct 8, 2021

ICYMI: PowerShell Week of 08-October-2021

Topics include VMWare, Windows 11, Web Reports and more… Special thanks to Robin Dadswell, Prasoon Karunan V, Kiran Patnayakuni and Kevin Laux How to gather your vCenter inventory data with this VMware PowerShell script by Scott Matteson on 7th October Inventory reports are a common request when administering a VMware vCenter environment. Learn how this VMware PowerShell script can make such requests quick and easy Building a Web Report in PowerShell, use the -Force Luke by Chris Noring on 8th October