Create a Hyper-V NAT Switch with PowerShell – the easy way

You can follow the original guide by Microsoft and manually edit all the details, or just use the variables from the script below and let PowerShell do the work for you.
# Variables
$InternalSwitchName = "Internal Virtual Switch"
$NATGatewayPrefixLength = "24"
$NATGatewayNetwork = "192.168.0.0/$NATGatewayPrefixLength"
$NATGatewayIP = "192.168.0.1"
$NATNetworkName = "NAT Network"

# Create the VM Switch and NAT Gateway
New-VMSwitch -SwitchName $InternalSwitchName -SwitchType Internal
New-NetIPAddress -IPAddress $NATGatewayIP -PrefixLength $NATGatewayPrefixLength -InterfaceIndex (Get-NetAdapter -Name $("vEthernet ($InternalSwitchName)")).InterfaceIndex
New-NetNat -Name $NATNetworkName -InternalIPInterfaceAddressPrefix $NATGatewayNetwork

PowerShell – How to Create an Array with PSObject

As I told you before in my previous blog post, I was asked to build an interactive PowerShell script for creating Virtual Machines in Azure. In this blog post, I want to show you how I’ve created a report (or array) within PowerShell that:

  • Visualize the to-be-created objects to the user
  • Allows PowerShell to get the data of that array to create Virtual Machines. This makes sure that you have a consistent view of what PowerShell will create for you. See it as an order overview before you buy something online.

Let’s imagine I have all the Virtual Machines that I want to create in $VMs. This can be an import of a CSV, or maybe I’ve asked the user for details with “Read-Host” or Out-GridView. When you import a CSV, the content will already be organized in an array. But with this code, you can easily add more content to it and combine both data from a CSV and from the script. For example, a randomized password or the first available IP address in a subnet in Azure. My end goal is to have a nice overview of all the needed Virtual Machines in $Report, which I can later use to make those VMs. With the code below, you will create a PSObject for every VM in the $VMs variable. After the PSObject has been created, it will append to the $Report variable. With “$Report = @()” you ask PowerShell to create an empty array. See it as an empty table that you could later use to add content to it. After the deployment has succeeded or failed, you can add the status to $VM.DeploymentStatus.

# Set report variable
$Report = @()

Foreach ($VM in $VMs) {
    $PSObject = New-Object PSObject -Property @{
        DeploymentName          = $VM.resourceGroupName + "-" + (Get-Date -Format "yyyyMMdd-hh-mm-ss")
        VMName                  = $VM.vmName
        Location                = $VM.resourceGroupLocation
        ResourceGroupName       = $VM.resourceGroupName
        AdminPassword           = $VM.adminPassword
        VMSize                  = $VM.vmSize
        VirtualNetwork          = $VM.virtualNetwork
        VirtualNetworkRG        = $VM.virtualNetwork.ResourceGroupName
        SubnetName              = $VM.subnetName
        IPAddress               = $VM.ipAddress
        OperatingSystem         = $VM.operatingSystem
        DeploymentStatus        = $Null
    }
    $Report += $PSObject
}

# Show the report
$Report

# Or show it in Table Format
# $Report | Format-Table

The above example is by far the easiest way to create a nice array for me.

Thanks for reading. Hope you find it useful too.

Azure – A parameter cannot be found that matches parameter name

Some error outputs are not always useful. Especially when they make no sense for the issue you have.

Error message:

New-AzureRmResourceGroupDeployment : A parameter cannot be found that matches parameter name ‘YOURPARAMETER’

Solution:

This error occurs because of at least the 2 following issues:

  • You didn’t specify a parameter for ‘YOURPARAMETER’ in your JSON template. That’s what the error says. If you forget to specify a parameter with the New-AzureRmResourceGroupDeployment cmdlet, you’ll see a prompt to insert a value for that parameter. But if you add a paremeter like -Name “VM01” to the command while it’s not specified in the JSON template, you’ll see this error.
  • The JSON code you provided isn’t valid. Always validate your JSON code. You can use http://www.jsoneditoronline.org/, paste your code and look for the red “X” buttons after a line number.

Did you find another issue where this error occurs? Please let me know in the comments section.

Cheers!

PowerShell Profiles – The structure of your _PSH_BASE.ps1 file

In my previous post PowerShell Profiles – The profile.ps1 file I showed you my profile.ps1 file. In this post, I’ll show you a way to structure your base file, so that you can use it for your functions and aliases. Make sure that you always use max 2 files. 1The first file is your profile.ps1 file and the other file is this _PSH_BASE.ps1 file. If you use like 3 or 4 files, it can take a couple of seconds to load your PowerShell session.

Continue reading

PowerShell Profiles – The profile.ps1 file

Welcome to this blog series about PowerShell profiles. I’m using PowerShell profiles for a couple of months now to make life a lot easier. To start this blog series, I would like to show you my Profile.ps1 file. It’s located in “C:\Users\<Your username>\Documents\WindowsPowerShell”. Because I use my PowerShell profiles at multiple locations such as my work notebook, home computer and sometimes at projects, I need to make sure that my PowerShell script home is always right so that the rest of the PowerShell profile is able to load successfully. That’s why I have these commands at the beginning of my profile:

Continue reading

SCCM 2012 R2 SP1 – Make Windows 8.1 drivers supported on Windows 10 with PowerShell

I had an issue within my lab with deploying Windows 8.1 drivers to Windows 10 with SCCM 2012 R2 SP1. It isn’t possible to make all Windows 8.1 drivers compatible with Windows 10 within the SCCM 2012 R2 SP1 console with just one click. Because I was running within a lab environment and I only had 2 driver packages for Windows 8.1 x64, I was able to make the drivers available for deployment to all platforms. You can do this with the magic of PowerShell:

Continue reading

Azure – Deploy and automatically domain join a VM with Azure Automation Runbooks

I was looking for a way to deploy and automatically domain join a VM in Azure. The solution was quite simple: Azure Automation. I found the blog post of DexterPOSH very useful, but the script doesn’t work for me. Follow the steps on his blog and use this script below. I’ll update this post if I find some improvements. Don’t forget to update the domain in the Add-Computer part.

Continue reading