BIGCommonFunctions.psm1

function sleepforone {
    Start-Sleep -s 1
}

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 "If the authentication box doesn't appear, check behind the VSCode/ISE window" -ForegroundColor Black -BackgroundColor White
    }
}

function checkifMsolModuleExists {
    if (!(Get-Module -ListAvailable -Name MSOnline)) {
        Write-Host "You don't have the MSOnline module installed. `nDon't worry though, I'll install it for you" -ForegroundColor Yellow
        Install-Module -Name MSOnline
    } elseif (Get-Module -ListAvailable -Name MSOnline) {
        Write-Host "MSOnline is installed" -ForegroundColor Green
        Write-Host "If the authentication box doesn't appear, check behind the VSCode/ISE window" -ForegroundColor Black -BackgroundColor White
    }
}

function checkifAzureADModuleExists {
    if (!(Get-Module -ListAvailable -Name AzureAD)) {
        Write-Host "You don't have the AzureAD module installed. `nDon't worry though, I'll install if for you" -ForegroundColor Yellow
        Install-Module -Name AzureAD
    } elseif (Get-Module -ListAvailable -Name AzureAD) {
        Write-Host "AzureAD module is installed" -ForegroundColor Green
        Write-Host "If the authentication box doesn't appear, check behind the VSCode/ISE window" -ForegroundColor Black -BackgroundColor White
    }
}

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 "If the authentication box doesn't appear, check behind the VSCode/ISE window" -ForegroundColor Black -BackgroundColor White
    }
}

function checkifSharepointPnPExists {
    if (!(Get-Module -ListAvailable -Name SharePointPnPPowerShellOnline)) {
        Write-Host "You don't have the Sharepoint PnP module installed. `nDon't worry though, I'll install it for you" -ForegroundColor Yellow
        Install-Module -Name SharePointPnPPowerShellOnline -Force
    } elseif (Get-Module -ListAvailable -Name SharePointPnPPowerShellOnline) {
        Write-Host "Sharepoint PnP module is installed" -ForegroundColor Green
    }
}
function checekifPrtgAPIModuleExists {
    if (!(Get-Module -ListAvailable -Name PrtgAPI)) {
        Write-Host "You don't have the Prtg API module installed. `nDon't worry though, I'll install it for you" -ForegroundColor Yellow
        Install-Module -Name PrtgAPI -Force
    } elseif (Get-Module -ListAvailable -Name PrtgAPI) {
        Write-Host "PRTG API module is installed" -ForegroundColor Green
    }
}

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

function connect-365 {
    checkIfExchangeModuleExists
    Connect-ExchangeOnline -ShowProgress $true
}

function connect-msol {
    checkifMsolModuleExists
    Connect-MsolService
}

function connect-AzAD {
    checkifAzureADModuleExists
    Connect-AzureAD
}

function connect-msteams {
    checekifTeamsModuleExists
    Connect-MicrosoftTeams
}

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 beVerbose {
    param (
        $beVerbose,
        $Colour = "Green"
    )
    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
}