Convert text files to PDF then merge PDFs in bulk with PowerShell and iTextSharp

To demonstrate this process, I will show you how I export all the PowerShell help and convert it to PDF files that I use instead of looking at the help files in the PowerShell Console. I realize you can view the PowerShell help files in the console and online. The purpose of this script is to demonstrate how I solved what I viewed as a problem for me. You may be just fine looking through the help files in the console or online, as with any PowerShell script anyone hacks together, your mileage may vary.

I have split the code into 3 functions; 1 to export the help files to text files, 1 to convert those text files to PDFs, and 1 to merge all the PDFs in a given folder into 1 PDF. This process requires the iTextSharp .net library which you can download here (https://github.com/itext/itextsharp). This is probably one of the more basic implementations of the iTextSharp library, it has a lot of functionality that I didn’t need to accomplish my mission. You can find more technical information on iTextSharp here (https://afterlogic.com/mailbee-net/docs-itextsharp/)

The Problem

For me, I hate looking through the help files in the console… for a few reasons.

  1. By default, the font is too small for me to see well. Old people problems I know, it is what it is. I find reading a pdf file much easier on the eye. I dump all the help files on a network accessible share that I can quickly access on any machine in my network.
  2. I can highlight, comment and bookmark the pdf files as needed. This may require the full version of Acrobat Pro or an equivalent, I’m not sure. My organization has Acrobat Pro so this isn’t an issue for me. Using highlighting, comments and bookmarks, I can make the help files more of a quick reference, it just works for me.
  3. I frequently work on Servers that are not accessible to the larger internet. Meaning, I can’t update help files for those machines unless I do it manually. Instead, I keep these pdf files on an external hard drive that I carry around anyway.

I know the help files are updated occasionally but it’s not often enough to make much of a difference. I usually update my help files a couple times a year. All that said, below is the code I use to make all the txt files and convert them to pdfs. I have also combined the help for each cmdlet in a module into a single pdf that I have made available here on signalwarrant.com. Hit the downloads link and use if you think you can get some benefit from them.

This script exports all the help for each cmdlet to the specified file path.

 

The script below will convert files to PDF.

 

This script will merge all PDFs in a given folder into 1 PDF. You would think it would take a long time for many PDF files but I created a 20,000 page PDF file in a second or so.

 

This script is not part of any of the functions above but I figured I would include it. It loops through each folder recursively and merges all the PDF files in each folder.

 

Automate Creating Lab Virtual Machines in Azure with PowerShell

As you may or may not know, I recently decommissioned my old Dell PowerEdge 1950 server that I used for a few Lab virtual machines. While experimenting with PowerShell on these Virtual Machines, I have found myself in the situation where it would be easier to delete the Virtual Machines and re-create them instead of troubleshooting something I fouled up in the Registry. After the 2nd time rebuilding the lab VMs using the Azure website, I decided to script the process.

The script below will take input from a CSV file and create a virtual machine in your Azure subscription for each row in the CSV file. My example creates 2 virtual machines but you can obviously add as many as you need.

For a production environment in Azure, I would suggest Snapshotting the Virtual Machines. There is a good write-up of the process here: http://www.coreazure.com/snapshot-vms-in-azure-resource-manager/. In my case, for a Lab, snapshots use more storage which costs more $$.

 

 

Hey PowerShell… Text me if my Domain Admins Group changes

This is why I Love PowerShell… It’s simple, yet functional.

From an Administrative perspective, I think we can all agree that any changes in your Domain Admins group without your knowledge would be of interest to you. If you’re in a large organization with access to enterprise management tools you probably have some widget that fires off a message to you or a group of people in the event a change is detected… or maybe you don’t.

If you’re an admin at a small business and maybe even some medium sized businesses, you may not have access to those enterprise management tools and widgets. Turns out, we can use PowerShell to monitor any group for us and notify us when a change occurs. It’s actually pretty simple.

You can even have PowerShell send you a text message… which is pretty cool.

I’m using the script to keep an eye on my Domain Admins Group but you could easily adapt it to monitor services or processes. You might want to monitor your Exchange Servers Transport service, if it stops for whatever reason send me an email and text message.

First, we have to get all the members of the Domain Admins Group and export to an xml file.

This is the script we’ll run on a schedule.

These are the Action arguments for the scheduled task.
-NoLogo -NonInteractive -WindowStyle Hidden -NoProfile -Executionpolicy bypass -file “C:\scripts\AD_Audit.ps1”

 

Visual Studio Code and Visual Studio Team Services Integration

For whatever reason, I seem to be migrating most of my coding activity to Visual Studio Code from the PowerShell ISE. I’m not really sure why other than the look and feel seems to be more pleasing than the ISE… to me anyway. Anywho, I have wanted to hack out some sort of code backup and version control solution for all of my code for a while now. Moving everything to Visual Studio Code seems like the prime time to get that done as well.

I know nothing about Git or Visual Studio Team services, I have zero experience with either. So, In order to get this setup, I found myself searching the Youtube for a tutorial… I didn’t find much. After a couple of hours googling and youtube video watching I finally got it to work. In order to save you the time and effort, I thought it would be good to make a video myself.

Enable PowerShell Remoting to an Azure Virtual Machine, without Domain Membership

I recently started an Azure subscription in order to move all the servers I use to test PowerShell code to the cloud. Right now I have only a couple Virtual Machines, one running Windows Server 2016, that’s my Domain Controller. I also have a Windows Server 2012 R2 Virtual Machine with Exchange 2013 installed. Obviously, both of these VMs are in the same domain.

For the purposes of testing, I wanted to be able to remote to the cloud VMs using PowerShell. The problem is since my local machine is not in the same domain as the VMs I couldn’t get authenticated. Now, you can stand up an Azure Active Directory and put the local machine in that domain and you’re good to go. I’m trying to keep costs as low as possible so I wasn’t willing to pay that extra expense for the Azure AD. I think you can also use a certificate in an Azure Keystore but again, extra expense plus I would have to figure out how to make it work… I’m an Azure n00b.

After some quality time consulting Professor Google, I came to the conclusion to create a certificate in each VM, then importing that certificate on my local laptop was the easiest way to make this work. Obviously, this is not a good enterprise solution although I guess you could probably do it a little more efficiently on a larger scale using Certificate Services. Anywho… this is how I did it.

If you have a better method, please let me know in the comments.