Starting with Terraform, Windows and Azure Part 2

Reading Time: 3 minutes
Installation

In the previous post I explained how to setup the Windows environmental variable PATH so you can run the terraform executable from any path. In this post I will explain how I set up my favorite text file editor. In the last year or so I came to fall in love with Microsoft Visual Studio Code. It runs on Windows, macOS and Linux, is very versatile and got al kind of neat features. At least try it and decide for yourself.

You can download the installation media here.

When you are done installing you will be greeted with a welcome screen that looks something like this:

image

Settings

Now first lets add some nice settings. To access the settings you can press the following key combination:

Ctrl+, (Control and Comma)

This will open up the User Settings json file:

image

On the left side are the Default Settings. You can mess with those, but it’s better to put your custom settings in the user settings.

The following is an example of my settings:

{
"material-icon-theme.showWelcomeMessage": false,
"workbench.iconTheme": "material-icon-theme",
// 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
"window.zoomLevel": 0,
"git.confirmSync": false,
"workbench.panel.location": "bottom",
"editor.minimap.enabled": true
}

I can recommend Minimap.enabled. This Enables a map in the right side of the editor that shows a small version of the file you are editing. This is usefull navigating large text files.

Extensions

Pressing the Ctrl+Shift+X will open up the Extension Tab on the left side of the window:

image

When you first start out it should be empty. I can recommend the following Extensions:

  • 1337 Theme
  • Advanced Terraform
  • Azure Resource Manager Tools
  • Azure Resource Manager Snippets
  • Material Icon Theme
  • PowerShell
  • Simple Terraform Snippets
  • Terraform
  • Terraform Autocomplete

You can type in these values at the ‘Search in Extensions Marketplace‘ field. Visual Studio Code will also recommend Extensions based on the files you are working with. But the ones above I find great for editing Terraform files.

Console

You van start the console by typing Ctrl+~. In windows this should default to PowerShell. This can also be set to Bash for instance. We’ll keep it on PowerShell for now. Visuals Studio Code also supports the running of selected code by pressing F8. The entire code can be executed by pressing F5. Keep in mind the code execution works when running PowerShell cmdlets. More keyboard shortcuts are found here.

Explorer

Ctrl+Shift+E opens the explorer tab. In this tab you can either open files or entire folders. In the case of the later the contents of the entire folder will be displayed. This is useful when editing multiple files simultaneously. In fact, Visual Studio Code even adds an ‘Open with Code’ feature to the context menu (right-click). This will work for folders and Files.

TLDR;

Download Visual Studio Code, it’s great for Terraform and great for scripting in general.

Starting with Terraform, Windows and Azure Part 1

Reading Time: 2 minutes

This is a series of blog posts going in to the setup of Terraform and building your first Azure deployment. First we are going to the local installation. Terraform is able to run on a variety of operating systems:

  • MacOS
  • FreeBSD
  • Linux
  • OpenBSD
  • Solaris
  • Windows

Since this blog Is mostly about Microsoft, Windows and Azure related stuff I’m going to cover the Windows version. Fist of all, download the Terraform executable for your Windows installation (32 of 64 bit) right here.

Extract the zip package to a location on your computer, for instance:

c:\Terraform

I would also recommend to add this location to the ‘PATH’ environmental variable in Windows so you can actually run this from any location so you don’t have to type extensive paths every time you are doing deployments.

To make it easy I’ve devised a PowerShell script:

# Terraform Executable location, modify for a different location
$TerraformPath='c:\Terraform'
# Get the PATH environmental Variable
$Path=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
# Create New PATH environmental Variable
$NewPath=$Path+";"+$TerraformPath
# Set the New PATH environmental Variable
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $NewPath
# Verify the Path
(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

This should allow you to run terraform from any path on your machine. You can try this opening a PowerShell session and running Terraform:

image

So the local terraform is all set up. That wasn’t too hard. In the next post I will visit the tools I use to write Terraform templates (and a lot of other scripts/things).

Uninstall PowerShell from Windows Server 2012; And get it back

Reading Time: 4 minutes

A couple of days ago I was doing a deployment. It was one of those days where nothing seemed to work. Everyone is familiar with them and probably detest them as much as I do: the type of non-productive days where nothing productive gets done.

I actually had a lot of setbacks. Firewalls being too restrictive. Network traffic not routed correctly. Group Policy Objects (GPOs) magically breaking my clients Active Directory connections. And then, instead of throwing my laptop, I wanted to start from scratch with the server I was working on at that particular moment.

I got a little more than I bargained for. So in my infuriated state I decided it was a good idea to remove all features and roles from the server. I opened server manager, choose to remove roles and features, and deselected all options, not really paying attention to what I was doing.

Knipsel2

The Server Manager gave me a summary of the jobs at hand, but of course I did not read it. I clicked the close button and went for a cup of coffee.

Knipsel6

When I came back the server innocently asked me to reboot it. Suggesting that was needed to complete the task I had assigned. Giving it no secondary thought I gave the command to shutdown forced and reboot immediately.

When it came back up, I logged in and was happily greeted by a command prompt shell. Thinking I accidentally uninstalled the graphical user interface from Windows Server 2012, I wasn’t worried for a second. So I gave command to start PowerShell and was presented with the following error message.

Knipsel7

Oops…

What did I just do? Uninstall everything. And I really mean everything. Turns out that unchecking everything includes .NET 4.5 and thus removing everything, leaves your server quite useless. Something like a Windows 2008 Server Core installation.

Then I remembered “Deployment Image Servicing and Management”. Usually referred to as DISM. First, I checked what features where available. I put the output to a text file because otherwise it wouldn’t be really readable.

DISM /Online /Get-Features > features.txt

Knipsel10

As you can see, almost everything is disabled. This gives new meaning to "build from scratch".

PowerShell Engage

then I used the following command to install PowerShell again:

DISM /Online /Enable-Feature:MicrosoftWindowsPowerShell /all

Notice the /all switch at the end of the command. This tells DISM to install the prerequisites as well.

Knipsel14

The PowerShell command now works again.

To the GUI

Still there is no Graphical User Interface. And for the sake of this blog I used DISM to install that as well:

DISM /Online /Enable-Feature:Server-GUI-Shell /all

This will ask for a reboot.

Then the server will install the GUI again and will be manageable by IT Pros who don’t like the command line that much.

Knipsel19

And after this operation completes and you log in, the GUI is back.

Knipsel20

Then I went on with the deployment as planned and this server is now running the Remote Access Role and configured to manage DirectAccess for a couple of Windows 8 clients. It's running this without any problems.

To Sum up

You can remove PowerShell from Windows Server 2012 by using the Server Manager and selecting the Remove Roles and Features option.

Afterwards you will boot into a Command Prompt where PowerShell will not work because it is not enabled.

I used the following DISM commands to enable the feature again:

DISM /Online /Enable-Feature:MicrosoftWindowsPowerShell /all

With PowerShell enabled, it should be a breeze to fix the server some more.

Keep in mind that this server still had its source files. There are methods to add or remove the source files.

Concluding

Windows Server 2012 is very modular. Almost everything is removable. There might be scenarios where this can be useful. It might just want to be something to keep in mind.