Amici.IT.psm1

function startLogging {
    param (
        [string]$scriptName,
        [string]$logdirectory
    )
    $Global:scriptName = $scriptName
    $Global:dateTime = Get-Date -Format s | foreach { $_ -replace ":", "-" }
    $Global:logfile = "$logdirectory\$scriptName-$dateTime.log"

    if (-not (Test-Path $logdirectory)) {
        Write-Host "$($logdirectory) does not exist. Creating it now" -ForegroundColor Yellow
        New-Item -Path $logdirectory -ItemType "directory"
    }

    Start-Transcript -Path $logfile -Append
}

function handleError ($message, [bool]$stop) {
    Write-Host "Error: $message" -ForegroundColor Red
    Write-Host "Exception: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "Stack Trace: $($_.Exception.StackTrace)" -ForegroundColor Red
    
    if ($stop) {
        Stop-Transcript
        exit
    }
}

function stopLogging {
    Stop-Transcript
}

function checkforModule ($module) {

    function confirmInstallation ($module) {
        Write-Host "Confirming that $module is installed" -ForegroundColor Yellow
        if (Get-Module -ListAvailable -Name $module) {
            Write-Host "Module $module is installed!" -ForegroundColor Green
        }
        else {
            Write-Host "Module $module isn't installed" -ForegroundColor Red
        }
    }

    $trusted = Get-PSRepository -Name "PSGallery" | Select InstallationPolicy -ExpandProperty InstallationPolicy
    if ($trusted -eq "Untrusted") {
        Write-Host "PSGallery isn't trusted. Trusting it now." -ForegroundColor Yellow
        Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
    }

    if (!(Get-Module -ListAvailable -Name $module)) {
        Write-Host "You don't have the $module installed. `nDon't worry fam, I'll install it for you" -ForegroundColor Yellow
        Install-Module -Name $module
        confirmInstallation $module
    }
    else {
        confirmInstallation $module
    }
}

function checkIfAdmin ($elevate) {
    if ($elevate) {
        $currentUser = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
        $isAdmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

        if (!($isAdmin)) {
            Write-Host "Script must be run as Administrator." -ForegroundColor Red
            Stop-Transcript
            exit
        }
    }
}

function log {
    param (
        $log,
        $Colour = "Yellow"
    )
    Write-Host $log -ForegroundColor $Colour
}

function endScript ($skipDisconnect) {

    if ($skipDisconnect) {
        stopLogging
        return
    }

    Show-2Menu -Title "Do you want to disconnect ExchangeOnline and MSGraph?" -option1 "Yes" -option2 "No"
    $choice = Read-Host "Please make a selection"
    switch ($choice) {
        '1' {
            Write-Host "Disconnecting Exchange Online" -ForegroundColor Yellow
            Disconnect-ExchangeOnline -Confirm:$false
            Write-Host "Disconnecting MSGraph" -ForegroundColor Yellow
            Disconnect-Graph
            stopLogging
        } '2' {
            stopLogging
        }
    }
}

function getCoffee {
    Write-Host " ~" -ForegroundColor Yellow
    Write-Host " ,--." -ForegroundColor Yellow
    Write-Host "C| | This will take a while, you probably want to go get a coffee or something." -ForegroundColor Yellow
    Write-Host " '`=='" -ForegroundColor Yellow
    Start-Sleep -Seconds 3
}

function showStep {
    param(
        [Parameter(Mandatory = $true)]
        $currentStep,

        [Parameter(Mandatory = $true)]
        $description,

        [Parameter(Mandatory = $false)]
        $totalSteps
    )

    if ($totalSteps) {
        Write-Host "===================================" -ForegroundColor Cyan
        Write-Host " Step $currentStep of $totalSteps " -ForegroundColor Cyan
        Write-Host " $description" -ForegroundColor Cyan
        Write-Host "===================================" -ForegroundColor Cyan
    }
    else {
        Write-Host "============================" -ForegroundColor Cyan
        Write-Host " Step $currentStep " -ForegroundColor Cyan
        Write-Host " $description" -ForegroundColor Cyan
        Write-Host "============================" -ForegroundColor Cyan
    }

    Start-Sleep -Seconds 2
}

function getSerialNumber {
    $serial = (Get-WmiObject -Class Win32_BIOS).SerialNumber
    Write-Host "Serial Number: $serial" -ForegroundColor Green
}

function checkTranscript {
    try {
        Stop-Transcript -ErrorAction Stop | Out-Null
        log "Stopping previous transcript"
        return $true
    }
    catch {
        if ($_.Exception.Message -match 'not currently transcribing') {
            return $false
        }
        else {
            handleError "Unable to check if transcript is running" -stop $true
        }
    }
}

function Show-2Menu {
    param (
        [string]$Title = 'Menu',
        $option1,
        $option2,
        $clear
    )

    if ($clear) {
        Clear-Host
    }

    Write-Host "================ $Title ================"
    
    Write-Host "1: $($option1)."
    Write-Host "2: $($option2)."
    Write-Host "3: Press 'ctrl + c' to quit."
}