Press "Enter" to skip to content

Category: PowerShell

Formatting Strings in Powershell

Formatting strings in PowerShell is easy, just include the variable inside the string and it does it automatically, right? What about when you are using variable values (i.e. $ds.count)?
Then you have to write an ugly line of code like:

I’m sure your thinking, ah that’s not too bad. But what if you had four or five value strings to put together? There is a better way to concatenate strings with variables values, it’s called Formatting a String. This is accomplished through .Net, and looks like:

And it works with more than one value:

The basics of the String.Format Method is to provide an indexed set of overloads into the string. Each overload is represented by the {0} notation, where the number inside the curly bracket is the indexed location of the overload. To read more about the method, check out the MSDN Article.

Creating XML Documents from Powershell

We all know I have OCD when it comes to formatting. Using the built in PowerShell cmdlets (Export-XmlCli) to export XML left me with a mess of an XML Schema that I couldn’t apply a XSL Transform to. I needed something that would allow me to control the schema, and insert the XSL declaration. After doing quite a bit of online searching and research, I decided to try using the .NET class System.XML.XmlTextWriter. The code sample below is the basic snippet using the class and creating a document.

A couple of notes:

  • $filepath will need to be set to the full file path, using “.\” will not work.
  • If you are creating a complex file, you will want to note which tag you are closing with the WriteEndElement.

PowerShell Logging Module

The SysAdmin PowerShell Module consists of a diverse set of functions, but there is one set of functions that is leveraged in every other function…Logging! The logging functions provide visual feedback to the user about the progress of a script. It can also provide valuable debugging information when a script goes sideways.

The logging functions consist of a core function ‘New-Log’. The function takes two parameters: (1) the log text, and (2) the type of log. The second parameter is not required. The remaining logging functions in the module call ‘New-Log’ with the second parameter. The functions are shown below:

Systems Admin PowerShell Module

Over the past year, I have passed from PowerShell n00b to being able to script/code anything that has been thrown at me. One of the main reasons I have been able to progress this far with my PowerShell abilities are all the great blogs and resources on the internet. I hope over time, that this blog may be a resource for those starting with PowerShell and those who want to learn more.

While I’ve been learning PowerShell, I have been compiling a PowerShell Module for Sys Admins. This module consists of a core library that handles reporting, connecting to remote servers, etc. In addition, it contains scripts for adding everything from DNS records creating, pulling system configuration information and a lot in between. As I go through the existing module, I’ve decided to rewrite and update the module. As I go through this process, I will share the various cmdlets under the PSModule category.

If you have any suggestions, please let me know!