Milestone 5 ‐ 480‐utils.psm1 - Jacob-Mayotte/SYS480 GitHub Wiki

💡In a previous milestone, we installed PowerShell and powercli and accessed vsphere both interactively through commands such as Get-VM as well as through a .ps1 script of your own making.  In this module, we will make a powershell module that we will build upon over the semester.Powershell/PowerCLI is the mechanism we use to deploy and configure the virtual machine and its hardware.  We can also do some interesting things through the VMWare Tools guest integration.

License vCenter & vSphere:

  • Followed: Licensing Inst. Provided by Ryan in this lab.
  • Received the keys via email then applied through the GUI on xubuntu-mgmt.

Install vscode on xubuntu-mgmt as well as the PowerShell extension:

image

Setup GitHub Connection: Source

  • I followed GitHubs documentation to create a new SSH key.

  • ssh-keygen -t ed25519 // Creates a new key-pair

  • ssh-add ~/.ssh/id_ed25519

image

  • To add the key to my GitHub I followed Source.
  • Then I logged into my github and cloned the repo:

image

image

Build a Module Skeleton:

  • Followed Devins video.

  • VSCode was installed in the last lab but for reference: sudo snap install code --classic

Things done throughout the video:

image

  1. Set up directories:

mkdir modules

mkdir 480-utils

  1. Module-Manifest Command + Files Created:

New-ModuleManifest -Path .\480-utils.psd1 -Author 'Jacob-Mayotte' -CompanyName 'SYS480' -RootModule '480-utils.psm1' -Description 'vsphere automation module for DevOps-480'

touch 480-utils.psm1

  1. code . // opens VSCode within standing directory.

  2. VSCode Open

image

  1. First Function in our .psm file:
function 480Banner() 
{
    Write-Host "Hello SYS480-Devops"
}
  1. Changed the VSCode $profile with: code $profile

image

This command was run in the terminal in VS, when it was initially executed VSCode asked if it could open a file, and allow it. I then added the = $env:PSModulePath + ":/home/jacob/SYS480/modules to the line, seen in the screenshot above.

NOTE!! This can be checked at any time with: $env:PSModulePath

image

  1. Imported the 480-utils mod: Import-Module '480-utils' -Force & executed the function 480Banner:

image

  1. Added new function 480Connect and executed it:
User
function 480Connect([string] $server)
{
    $connection = $global:DefaultVIServer
    # Questions if we are connected or not: 
    if ($connection){
        $msg = "Already Connected to: {0}" -f $connection

        Write-Host -ForegroundColor Green $msg
    }else {
        $connection = Connect-VIServer -Server $server 
    }
}
  • After adding this function to the 480-utils.psm1 file I made sure to press CTRL + S to save the file locally, then I ran in the terminal: Import-Module '480-Utils' -Force:

image

  1. 480driver.ps1 creation via terminal:
  • Create a new powershell file: touch 480driver.ps1

image

  • Add the above, ctrl + s and then test it in the terminal:

image

  1. json file creation: touch 480.json via terminal
  • Similiar to last step but with 480.json:

image

  • CTRL = S // save that thang locally.
  1. New function in 480-utils.psm1
function Get-480Config([string]$config_path) 
{
    Write-Host "Reading " $config_path
    $conf=$null
    if(Test-Path $config_path)
    {
        $conf= (Get-Content -Path $config_path -Raw | ConvertFrom-Json)
        $msg = "Using Configuration at {0}" -f $config_path
        Write-Host -ForegroundColor Green $msg
    } else
    {
        Write-Host "No configuration found at $config_path" -ForegroundColor Yellow
    }
    return $conf
}
  • CTRL + S
  • Perform the following:

image

  • Fortunately, this worked. Copy that path that is seen under the most recent pwd, this will assist in the next step.
  1. Navigate to 480drivers.ps1 to add recent changes:

image

  • Test this after ripping a good ol' local save : ./480drivers.ps1

image

SelectVM Function:

  • In 480-utils.psm1 add the following function:
Function Select-VM([string] $folder)
{
    $selected_vm=$null
    try 
    {
        $vms = Get-VM -Location $folder
        $index = 1
        foreach($vm in $vms)
        {
            Write-Host [$index] $vm.name
            $index+=1
        }
        $pick_index = Read-Host "Which index number [x] do you wish to pick?"
        # 480-TODO Need to deal with an invalid index (consider making this check a function)
        $selected_vm = $vms[$pick_index -1]
        Write-Host "You picked " $selected_vm.Name
        # note this is a full on vm object that we can interract with!
        return $selected_vm    
    }
    catch 
    {
        Write-Host "Invalid Folder: $folder" -ForegroundColor Red    <#Do this if a terminating exception happens#>
    }
        
}
  • Okay once finished adding the function rip a local save. Now in the terminal import the module and test the function:

image

  • This fortunately works so we added a variable to 480.json:

image

  • Then added the command into 480driver.ps1:

image

Chugged away on the remaining requirements/functions:

  • Scripts:
  1. https://github.com/Jacob-Mayotte/SYS480/tree/main/modules/480-utils Sources:
  2. Bible
  3. Error AHndling Expl.
  4. User Guide kinda like the Bible
  5. Comparison Operators Explained:
  6. snapshots expl.
  7. Explanation of crazy RegEx string ChatGPT provided me for input validation in functions: '^\d+$'

image

^\d+$ is a regular expression pattern.
^ asserts the start of the string.
\d+ matches one or more digits (0-9).
$ asserts the end of the string.
So, this part checks if $pick_index is composed entirely of one or more digits.
  1. Example PowerCLI Scripts
  2. Comparison Operators