SBTools


24 May 2017

Taking advantage of PowerShell 5’s PowerShellGet, I’m publishing the SB-Tools PowerShell module to the PowerShell Gallery. This provides a much easier way to use the module functions without the need for manual installation. It also has built-in version control.

To see if you have PS5, type in $PSVersionTable

To find a module in the PowerShell Gallery, you can use the Find-Module cmdlet

The Find-Module cmdlet is from the PowerShellGet module that ships with Windows 10 and Server 2016. When using it for the first time, you may get a message similar to the the image above to upgrade your NuGet provider.

I’m re-writing the SB-Tools module, re-using and updating some of the functions, and adding new ones. Some are removed. The legacy SBTools modules remains in the Microsoft Script Center Repository.

To install the SB-Tools module you can use the Install-Module cmdlet. You may see a message similar to:

To see a list of the cmdlets/functions in the SB-Tools module you can use the Get-Command cmdlet

The module contains supporting functions such as Write-Log, ConvertTo-EnahncedHTML, and ConvertTo-EnahancedHTMLFragment. The ConvertTo-EnahncedHTML, and ConvertTo-EnahancedHTMLFragment functions are courtesy of Don Jones.

To see the built-in help for a function you can use the Get-Help cmdlet

You will see help information similar to:

Once the SB-Tools module is installed, you can use its cmdlets/functions such as Get-SBDisk

If you wish to have the SB-Tools updated when you start a new PowerShell instance, you can add it to your PowerShell ISE profile using the following code

New-Item -Path (Split-Path $profile) -ItemType Directory -Force | Out-Null
'if (Get-Module SB-Tools) { Remove-Module SB-Tools -Force }' | 
    Out-File -FilePath $profile -Append
'Install-Module SB-Tools -Force -AllowClobber -Confirm:$false' | 
    Out-File -FilePath $profile -Append

SBTools Powershell module includes a collection of functions that I find useful in different network administration tasks. Most have been written by me. Each function has credit to the authors under the .notes section. This module requires Powershell version 3, and is designed to work on Windows 2012/Windows 8 and above.

The SBTools module can be downloaded from the Microsoft Script Center Repository here.

To install it or upgrade to the latest version, extract the .rar file, and run install-SBTools.ps1 in the folder where you extracted the .rar file.

To see what version you have, type in:

Get-Module

SBTools02

If you don’t see the SBTools module, and you have installed it, you can import it by using the command:

Import-Module SBTools

To see the available functions/commands in SBTools module, use the command:

Get-Command -Module SBTools

SBTools03

To see help and examples for a function use the Get-Help cmdlet. For example, to get help on the New-SBVM function use:

help New-SBVM -all

The help output will look like:

NAME
New-SBVM

SYNOPSIS
Function to create Hyper-V virtual machines

SYNTAX
New-SBVM [-VMName] <String> [-VMFolder] <String> [-vSwitch] <String> [-GoldenImageDiskPath] <String> [[-VMG] <Int32>] [[-VMMemoryType] <String>] [[-VMStartupRAM] <Int64>]
[[-VMMinRam] <Int64>] [[-VMMaxRAM] <Int64>] [[-VMCores] <Int32>] [[-VLAN] <Int32>] [[-AdditionalDisksTotal] <Int32>] [[-AdditionalDisksSize] <Int64>] [[-CSV] <String>] [-WhatIf]
[-Confirm] [<CommonParameters>]

DESCRIPTION
This function automates the process of creating Hyper-V Virtual Machines.

PARAMETERS
-VMName <String>
VM Name: Mandatory. This is positional parameter 1
2-15 characters long. Observe NetBIOS naming limitations.
Must contain alphanumeric or these ! @ # $ % ^ & ( ) – _ ‘ { } . ~ characters only (http://support.microsoft.com/kb/188997)
Example: V-2012R2-Lab01

Required? true
Position? 1
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-VMFolder <String>
VM Folder: Mandatory. This is positional parameter 2
This is the path to put all VM files under
Example: g:\VMs\V-2012R2-Lab01 or \\SOFS\VMs\VM05

Required? true
Position? 2
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-vSwitch <String>
vSwitch name: Mandatory. This is positional parameter 3
It must exist on current Hyper-V host, example “My_vSwitch”

Required? true
Position? 3
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-GoldenImageDiskPath <String>
Path to VHDX file: Mandatory. This is positional parameter 4
This can be local or UNC path for a sys-prepped guest OS image.

Required? true
Position? 4
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-VMG <Int32>
VM Generation: Optional. This is positional parameter 5
Can be either 1 or 2. Assumed to be 1 if not provided.

Required? false
Position? 5
Default value 1
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-VMMemoryType <String>
VM memory type: Optional. This is positional parameter 6
Can be either “Static” or “Dynamic”. Assumed to be Dynamic if not provided.

Required? false
Position? 6
Default value Dynamic
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-VMStartupRAM <Int64>
VM startup memory: Optional. This is positional parameter 7
Assumed to be 1GB if not provided.
Examples: 536870912 or 512MB or 2GB

Required? false
Position? 7
Default value 1073741824
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-VMMinRam <Int64>
VM minimum memory: Optional. This is positional parameter 8
Relevent if VMMemoryType is “Dynamic”. Ignored if VMMemoryType is “Static”.
Assumed to be 512MB if not provided. Minimal accepted value is 512MB.
Examples: 536870912 or 512MB or 2GB

Required? false
Position? 8
Default value 536870912
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-VMMaxRAM <Int64>
VM maximum memory: Optional. This is positional parameter 9
Relevent if VMMemoryType is “Dynamic”. Ignored if VMMemoryType is “Static”.
Assumed to be 4GB if not provided. Minimal accepted value is 512MB.
Examples: 1024MB or 2GB or 1TB

Required? false
Position? 9
Default value 4294967296
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-VMCores <Int32>
Number of CPU cores to assign to the VM: Optional. This is positional parameter 10
Example: 4

Required? false
Position? 10
Default value 1
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-VLAN <Int32>
VLAN ID: Optional. This is positional parameter 11
Example: 193

Required? false
Position? 11
Default value 0
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-AdditionalDisksTotal <Int32>
Number of additional disks to create and attach to VM.
Optional. This is positional parameter 12
Valid range: 1-256. They will be dynamic disks.

Required? false
Position? 12
Default value 0
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-AdditionalDisksSize <Int64>
Size of each of the additional disks.
Optional. This is positional parameter 13
If AdditinalDisksTotal is provided and AdditionalDisksSize is not, size is assumed to be 1GB.
Examples: 536870912 or 512MB or 2GB or 10TB

Required? false
Position? 13
Default value 1073741824
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-CSV <String>
Path to CSV file used to store disk copy statistics.
Optional. This is positional parameter 14
Script appends to existing CSV file

Required? false
Position? 14
Default value “.\IOPS_$($VMName)_$(Get-Date -format yyyyMMdd_hhmmsstt).csv”
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-WhatIf [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Confirm [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

NOTES

Function by Sam Boutros
v1.0 – 08/14/2014

————————– EXAMPLE 1 ————————–

C:\PS>New-SBVM “VM01” “k:\VMs\VM01” “My_vSwitch” “E:\Golden\V-2012R2-3-C.VHDX”

————————– EXAMPLE 2 ————————–

C:\PS>New-SBVM “VM02” “i:\VMs\VM02” “My_vSwitch” “E:\Golden\V-2012R2-3-C.VHDX” -VMMemoryType Static

————————– EXAMPLE 3 ————————–

C:\PS>$VMName = “VM01”
$VMFolder = “k:\VMs\VM01”
$VMG = 2
$VMStartupRAM = 1GB
$VMminRAM = 512MB
$VMmaxRAM = 1GB
$vSwitch = “My_vSwitch”
$VMCores = 2
$GoldenImageDiskPath = “E:\Golden\V-2012R2-3-C.VHDX”
New-SBVM -VMName $VMName -VMFolder $VMFolder -vSwitch $vSwitch -GoldenImageDiskPath $GoldenImageDiskPath -VMG $VMG -VMStartupRAM $VMStartupRAM -VMminRAM $VMminRAM -VMmaxRAM
$VMmaxRAM -VMCores $VMCores -verbose

This example creates a single VM

————————– EXAMPLE 4 ————————–

C:\PS>$NumberofVMs = 5
$VMrootPath = “G:\VMs”
$VMPrefix = “V-2012R2-LAB”
For ($j=1; $j -lt $NumberofVMs+1; $j++) {
$Params = @{ VMName = $VMPrefix + $j;
VMFolder = $VMRootPath + “\” + $VMPrefix + $j;
VMG = 2;
VMMemoryType = “Dynamic”;
VMStartupRAM = 1GB;
VMminRAM = 512MB;
VMmaxRAM = 2GB;
vSwitch = “My_vSwitch”;
VMCores = 2;
VLAN = 19;
AdditionalDisksTotal = 3;
AdditionalDisksSize = 2TB;
GoldenImageDiskPath = “E:\Golden\V-2012R2-3-C.VHDX”
}
New-SBVM @Params
Start-VM -Name ($VMPrefix + $j)
}

This example creates 5 VMs and starts them.
VM names will be V-2012R2-Lab1, V-2012R2-Lab2,..

RELATED LINKS
https://superwidgets.wordpress.com/category/powershell/

Do you have a question about a function in the SBTools module? Do you like to have a function enhanced, or a new one added?

Let me know..

 

4 responses

  1. Pingback: Using Powershell to report on files containing PII (Personally Identifiable Information) | Sam's Corner

  2. Pingback: You don’t need to hunt for Powershell modules anymore | Sam's Corner

  3. Ramkumar D

    Hi,

    Thanks for sharing this wonderful script. Is it possible to customize the regex pattern?

    Thanks
    Ram

    May 24, 2016 at 10:09 am

  4. Todd MdFarland

    Has anyone been able to get this to work on Windows 2008 R2?

    July 1, 2016 at 9:27 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s