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:
- https://github.com/Jacob-Mayotte/SYS480/wiki/Milestone-4-%E2%80%90-VCenter-AD-Integration,-PowerCLI-and-Linked-Clones-%E2%80%90-JM#milestone-42 Installed vscode here in this lab
- Install the PS extension via VSCode's GUI, enter the Extensions tab, and search
PowerShell
.
Source
Setup GitHub Connection:-
I followed GitHubs documentation to create a new SSH key.
-
ssh-keygen -t ed25519
// Creates a new key-pair -
ssh-add ~/.ssh/id_ed25519
- To add the key to my GitHub I followed Source.
- Then I logged into my github and cloned the repo:
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:
- Set up directories:
mkdir modules
mkdir 480-utils
- 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
-
code .
// opens VSCode within standing directory. -
VSCode Open
- First Function in our
.psm
file:
function 480Banner()
{
Write-Host "Hello SYS480-Devops"
}
- Changed the VSCode
$profile
with:code $profile
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
- Imported the 480-utils mod:
Import-Module '480-utils' -Force
& executed the function 480Banner:
- 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 pressCTRL + S
to save the file locally, then I ran in the terminal:Import-Module '480-Utils' -Force
:
- 480driver.ps1 creation via terminal:
- Create a new powershell file:
touch 480driver.ps1
- Add the above, ctrl + s and then test it in the terminal:
- json file creation: touch
480.json
via terminal
- Similiar to last step but with
480.json
:
CTRL = S
// save that thang locally.
- 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:
- Fortunately, this worked. Copy that path that is seen under the most recent pwd, this will assist in the next step.
- Navigate to
480drivers.ps1
to add recent changes:
- Test this after ripping a good ol' local save :
./480drivers.ps1
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:
- This fortunately works so we added a variable to 480.json:
- Then added the command into 480driver.ps1:
Chugged away on the remaining requirements/functions:
- Scripts:
- https://github.com/Jacob-Mayotte/SYS480/tree/main/modules/480-utils Sources:
- Bible
- Error AHndling Expl.
- User Guide kinda like the Bible
- Comparison Operators Explained:
- snapshots expl.
- Explanation of crazy RegEx string ChatGPT provided me for input validation in functions:
'^\d+$'
- FreeCodeCamp Expl. Metacharacters - /d specifically
- RegEx Tester This source broke down each character in the string:
^\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.