BIGCommonPS7.psm1

function PS7Check {
    if ($PSVersionTable.PSVersion -notlike "7.*") {
        throw "You need to be running PowerShell 7 to use this function"
    }
}

function sleepforone {
    Start-Sleep -s 1
}

function sleepfortwo {
    Start-Sleep -s 2
}

function installChoco {
    Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
}

# ExchangeOnlineManagement
function checkIfExchangeModuleExists {
    if (!(Get-Module -ListAvailable -Name ExchangeOnlineManagement)) {
        Write-Host "You don't have the Exchange Online Management module installed.`nDon't worry though, I'll install it for you"`
         -ForegroundColor Yellow
        Install-Module -Name ExchangeOnlineManagement
    } elseif (Get-Module -ListAvailable -Name ExchangeOnlineManagement) {
        Write-Host "Exchange Online Management module is installed!" -ForegroundColor Green
        Write-Host "Importing Module" -ForegroundColor Green
        Import-Module -Name ExchangeOnlineManagement
        Write-Host "If the authentication box doesn't appear, check behind the VSCode/ISE window" -ForegroundColor Black -BackgroundColor White
    }
}
function connect-eom {
    param (
        $credential
    )
    checkIfExchangeModuleExists
    Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue
    Connect-ExchangeOnline -ShowProgress $true -Credential $credential
}

# Az
function checkifAzModuleExists {
    PS7Check
    if (!(Get-Module -ListAvailable -Name Az)) {
        Write-Host "You don't have the Az module installed. `nDon't worry though, I'll install it for you" -ForegroundColor Yellow
        Install-Module -Name Az
    } elseif (Get-Module -ListAvailable -Name Az) {
        Write-Host "Az module is installed" -ForegroundColor Green
        Write-Host "Importing Module" -ForegroundColor Green
        Import-Module -Name Az
        #Import-Module -Name Az.Accounts
        Write-Host "If the authentication box doesn't appear, check behind the VSCode window" -ForegroundColor Black -BackgroundColor White
    }
}
function connect-az {
    param (
        $credential
    )
    PS7Check
    checkifAzModuleExists
    Connect-AzAccount -credential $credential
}

function checekifTeamsModuleExists {
    if (!(Get-Module -ListAvailable -Name MicrosoftTeams)) {
        Write-Host "You don't have the Microsoft Teams module installed. `nDon't worry though, I'll install it for you" -ForegroundColor Yellow
        Install-Module -Name MicrosoftTeams -Force
    } elseif (Get-Module -ListAvailable -Name MicrosoftTeams) {
        Write-Host "Microsoft Teams module is installed" -ForegroundColor Green
        Write-Host "Importing Module" -ForegroundColor Green
        Import-Module -Name MicrosoftTeams
        Write-Host "If the authentication box doesn't appear, check behind the VSCode/ISE window" -ForegroundColor Black -BackgroundColor White
    }
}
function connect-msteams {
    param (
        $credential
    )
    checekifTeamsModuleExists
    Connect-MicrosoftTeams -credential $credential
}

function checkifPnPExists {
    if (!(Get-Module -ListAvailable -Name PnP.Powershell)) {
        Write-Host "You don't have the PnP module installed. `nDon't worry though, I'll install it for you" -ForegroundColor Yellow
        Install-Module -Name PnP.Powershell -Force
    } elseif (Get-Module -ListAvailable -Name PnP.Powershell) {
        Write-Host "PnP module is installed" -ForegroundColor Green
        Write-Host "Importing Module" -ForegroundColor Green
        Import-Module -Name PnP.Powershell
    }
}

function Show-2Menu
{
    param (
        [string]$Title = 'Menu',
        $option1,
        $option2
    )
    Clear-Host
    Write-Host "================ $Title ================"
    
    Write-Host "1: $($option1)."
    Write-Host "2: $($option2)."
    Write-Host "3: Press 'ctrl + c' to quit."
}

function Show-2Menu-NoClear
{
    param (
        [string]$Title = 'Menu',
        $option1,
        $option2
    )
    Write-Host "================ $Title ================"
    
    Write-Host "1: $($option1)."
    Write-Host "2: $($option2)."
    Write-Host "3: Press 'ctrl + c' to quit."
}

function Show-4Menu {
    param (
        [string]$Title = 'Menu',
        $option1,
        $option2,
        $option3,
        $option4
    )
    Write-Host "================ $Title ================"
    
    Write-Host "1: $($option1)."
    Write-Host "2: $($option2)."
    Write-Host "3: $($option3)."
    Write-Host "4: $($option4)."
    Write-Host "Press 'ctrl + c' to quit."
}

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

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

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

        Start-Transcript -Path $logfile -Append
}

function sendTeamsMessage {
<#
    .SYNOPSIS
        This function will send a message in the "leaver feed" channel of the IT Staff Team
    .PARAMETER Title
        This is the title of the message in teams
    .PARAMETER Text
        This is the main body of the text. You'll most likely want the error in here
    .PARAMETER JSONBody
        Do not use this parameter, the title and text parameters will be used in this JSON message. It just consolidates the message.
    .EXAMPLE
        sendTeamsMessage -Title "Users PLM" -Text "Remember to check if the user had a PLM account and to disable it"
#>
    
    param (
        $TeamsURI,
        $Title,
        $Text,
        $JSONBody = [PSCustomObject][Ordered]@{
            "@type"      = "MessageCard"
            "@context"   = "http://schema.org/extensions"
            "summary"    = "Incoming Alert Message!"
            "themeColor" = '0078D7'
            "title"      = "$($Title)"
            "text"       = "$($Text)"
            "sections"   = @(
                @{
                    "activityTitle"     = "Alert Subsection"
                    "facts"             = @(
                        @{
                            "name"      = "Hostname"
                            "value"     = "$($runnerhostname)"
                        },
                        @{
                            "name"      = "Log file"
                            "value"     = "$($logfile)"
                        }
                    )
                    "markdown" = $true
                }
            )
        }
    )
    $TeamMessageBody = ConvertTo-Json $JSONBody -Depth 100
    $parameters = @{
        "URI"         = $TeamsURI
        "Method"      = 'POST'
        "Body"        = $TeamMessageBody
        "ContentType" = 'application/json'
    }
    Invoke-RestMethod @parameters | Out-Null
}