Milestone 5: 480.psm1 - squatchulator/Tech-Journal GitHub Wiki

Milestone 5: 480.psm1

Setup

  • The first thing that we need to do in order to get this milestone working is license our vCenter. Head to this URL and create/register for a VMWare IT academy account. Then, head to this link and request a lisence for VMWare vSphere 8.x Enterprise Plus.

  • Head over to xubuntu-wan, pop open a terminal, run pwsh and run the command sudo snap install code --classic to get Visual Studio Code installed.
  • While staying in powershell, create a new directory called 480. This will be our Git repo and where we will store the modules. Create it with a directory structure similar to the following:

image

  • Navigate into the 480-utils folder. You will also need to run this command there:
New-ModuleManifest -Path .\480-utils.psd1 -Author 'miles' -CompanyName 'SYS-480' -RootModule '480-utils.psm1' -Description 'vsphere automation module for SYS-480'
  • Also run touch 480-utils.psm1 after running that command above to create the empty file. Back out two directories (just getting to the folder called 480), and run code . to open VSCode in the current directory. Open up 480-utils.psm1 - this is where we are going to put all our functions. Put a basic one in there for now so we can just use it for testing:
function 480Banner()
{
    $banner=@"
 _  _    ___   ___              _   _ _     
| || |  ( _ ) / _ \       _   _| |_(_) |___ 
| || |_ / _ \| | | |_____| | | | __| | / __|
|__   _| (_) | |_| |_____| |_| | |_| | \__ \
   |_|  \___/ \___/       \__,_|\__|_|_|___/

      __    EEEK!
      /  \   ~~|~~
     (|00|)    |
      (==)  --/
    ___||___
   / _ .. _ \
  //  |  |  \\
 //   |  |   \\
 ||  / /\ \  ||
_|| _| || |_ ||_  
\|||___||___|||/

    "@
    Write-Host $banner
}
  • In the VSCode terminal at the bottom of the screen, cd to the modules directory and run code $profile - it will open a blank file. Put the following in:
$env:PSModulePath = $env:PSModulePath + ":/home/miles/Desktop/480/modules"
  • Save and close the file. If you run $env:PSModulePath, it should append what we wrote to the end of that variable similar to this:

image

  • Now in that VSCode terminal, run Import-Module '480-uils' -Force to import our new module. Calling the function should return a result like this:

image

  • Yay! Now to start adding more to our utils file. Add the following function below your original one:
function 480Connect([string] $server)
{
    $conn = $global:DefaultVIServer
    if ($conn) {
        $msg = "Already connected to {0}!" -f $conn
        Write-Host -ForegroundColor Green $msg
    } else {
        $conn = Connect-VIServer -Server $server
    }
}
  • Save the file and import the module again. (NOTE: You will have to do this each time you edit the .psm1 file!) Type 480Connect -server "vcenter.miles.local" to test our new function. Should prompt you with a login, just enter your -adm credentials. Test it again by running it once more after connecting to ensure the first part of the function works. Should get results like this:

image

  • Now we are going to figure out how to make use of JSON files to have a bit more automatic configuration when we make a connection to our server. In your 480 directory, go ahead and create a new file called 480driver.ps1, and create one more called 480.json. Open up the driver file first, and ad the following:
Import-Module '480-utils' -Force
480Banner
  • Save it, and now if we run ./480driver.ps1, it should just run our function for us.
  • Open up the new json file and add the following to it:
{
    "vcenter_server" : "vcenter.miles.local"
}
  • Make sure to save it - head back to the utils file where all our functions are, and add a new function at the bottom:
function Get-480Config([string] $config_path)
{
    Write-Host "Reading " $config_path
    $conf=$null
    if (Test-Path $config_path)
    {
        $conf = (Get-Content -Raw -Path $config_path | ConvertFrom-Json)
        $msg = "Using Configuration at {0}" -f $config_path
        Write-Host -ForegroundColor "Green" $msg
    } else {
        Write-Host -ForegroundColor "Yellow" "No Configuration"
    }
    return $conf
}
  • Save, import the module again, and run $conf=Get-480Config -config_path "./480.json". After this runs and gives a valid output, run $conf and it should output the vcenter server assigned to that variable.
  • In the driver file, add the following to the bottom:
$conf = Get-480Config -config_path = "/home/miles/Desktop/480/480.json"
480Connect -server $conf.vcenter_server
  • Save it and run the driver file again with ./480driver.ps1 and it should run everything smoothly!
  • Now, head back into the 480-utils file to add another function at the bottom:
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?"
        $selected_vm = $vms[$pick_index -1]
        Write-Host "You picked " $selected_vm.name
        return $selected_vm
    }
    catch
    {
        Write-Host "Invalid Folder: $folder" -ForegroundColor "Red"
    }
}
  • Now after saving and reimporting, you should be able to test it with Select-VM -folder "BASEVM"

  • Let's add some more to the JSON file. Add the line "vm_folder" : "BASEVM" beneath the only other line in there. (NOTE: Add a comma after the first line to indicate an end of line)

  • Head into the driver file and add the line Select-VM -folder "BASEVM" to the bottom. Make sure to save all these files! You can test that this all works by running your driver file again with ./480driver.ps1.

  • Modify everything to make a linked clone. Add JSON parameters like "esxi_host" : "192.168.7.15", "default_network" : "480-WAN", so on and so forth.