SEC 480 Milestone 5 Powershell Module - DefiantCoder/Tech-Journals GitHub Wiki

vCenter Licensing

  • First register an account with VMware IT Academy using your school email & request the vSphere 8.x Enterprise Plus and vCenter Server 8.x Standard licenses

  • To apply your license navigate to licenses on the xubuntu-wan vSphere and add your two license codes

image

  • Next to apply your new licenses navigate to the assets tab and assign the two new license to vcenter.yourname.local through Assign license and select your license

  • Repeat this in hosts to assign the enterprise license

image

VSCode Install

  • On xubuntu-wan enter the powershell cli and run the following install
pwsh
sudo snap install code --classic

Install git and create your repo

/home/user/Tech-Journals/480/480-utils
New-ModuleManifest -Path .\480-utils.psd1 -Author 'adam' -CompanyName 'Adam Cyber' -RootModule '480-utils.psm1' -Description 'vsphere automation module for 480-DevSecOps'

touch 480-utils.psm1

code .

image

cd modules
$profile
code $profile
  • add the following code
$env:PSModulePath = $env:PSModulePath + ":/home/user/Tech-Journals/480/modules"

image

  • Add the following to 480-utils.psm1
function 480Connect([string] $server){
    $conn = $global:DefaultVIServer
    #are we already connected
    if ($conn){
        $msg = 'Already Connected to: {0}' -f $conn

        Write-Host -ForegroundColor Green $msg
    }
    else{
        $conn = Connect-VIServer -Server $server
        #if this fails, let Connect-VIServer handle the encryption
    }
}
  • Run the following to check for success
Import-Module '480-utils' -Force
480Connect -server 'vcenter.adam.local'

480Connect -server 'vcenter.adam.local'
480Banner

image

  • Make the following files
touch 480driver.ps1
touch 480.json
  • In 480driver.ps1 input the following
Import-Module '480-utils' -Force
#Call the banner function
480Banner
  • In the 480.json file add the following
{
    "vcenter_server" : "vcenter.adam.local"
}
  • Next in the 480-utils.psm1 add the new function bellow the existing code
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 Configurtion"
    }
    return $conf
}
  • Execute the following to check for success
Import-Module '480-utils' -Force
$conf=Get-480Config -config_path "./480.json
$conf
  • Add the following to the 480driver.ps1 file and execute as shown below
$conf = Get-480Config -config_path "/home/user/Tech-Journals/480/480.json"
480Connect -server $conf.vcenter_server

image

  • Under the existing 480-utils.psm1 script add the following
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 interact with
        return $selected_vm
    }

    catch{
        Write-Host "Invalid Folder: $folder" -ForegroundColor "Red"
    }
}
  • Run the following to check for success the output should look similar to the example below Import-Module '480-utils' -Force select-vm -folder "BASEVM" image

  • add this line to the 480.json file

"vm_folder: "BASEVM"
  • add to the 480driver.ps1 file
Write-Host "Selecting your VM"
Select-VM -folder "BASEVM"
  • json file should look as shown
{
    "vcenter_server" : "vcenter.adam.local",
    "vm_folder" : "BASEVM",
    "esxi_host" : "192.168.7.29",
    "default_network" : "480-WAN",
    "default_datastore" : "datstore1-super1",
    "snapshot" : "Base"

}