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 setRepoAsTrusted() {
    if (!(Get-PackageProvider -ListAvailable -Name "NuGet")) {
        Install-PackageProvider -Name NuGet -Confirm:$false
    }
    Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted -ErrorAction SilentlyContinue
}

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

function sendSMS {
    <#
    .SYNOPSIS
        This function will send an SMS message with a custom error to whatever number is in the $number parameter.
        It is primarily designed to be a callback function for the errorHandler function.
        There's nothing stopping you from calling it directly, just use the body parameter to be whatever text you want
    .PARAMETER Number
        This is the mobile number the function will send an SMS message to. This must be a string and be the complete number starting with +44
    .PARAMETER Body
        This is the main body of the text. You'll most likely want the error in here
    .EXAMPLE
        When used as a callback function by errorHelper:
        errorHelper -customError "custom error goes here"
 
        When called directly
        sendSMS -number "+447888888888" -body "message goes here"
    #>
    

    param (
        $number,
        $body
    )

$date = Get-Date -Format "dddd dd/mm/yyyy HH:MM"
$flowuri = "https://hooks.zapier.com/hooks/catch/12326021/bvfpntr/"
$head = @{
    "Content-type"  = "application/json"
    }

$json = @"
    {
        "recipient":"$($number)",
        "body":"$($body)",
        "date":"$($date)"
    }
"@


beVerbose "Sending SMS message to $number"
Invoke-RestMethod -Method POST -Uri $flowuri -Headers $head -Body $json
}

function errorHelper($customError, $dontKill) {
    Write-Host $customError
    sendTeamsMessage -TeamsURI $TeamsURI -Title $scriptname -Text $customError
    sendSMS -number $asimNumber -body $customError
    sendSMS -number $aslamNumber -body $customError
    $Error[0]

    if (!($dontKill)) {
        Write-Host "Terminating" -ForegroundColor Red
        Stop-Transcript
        exit
    } else {
        Write-Host "Not terminating" -ForegroundColor Yellow
    }

}

# 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
}