###################################
# DESCRIPTION
# 

###################################
#REQUIREMENTS
#--Script must be digitally signed
# https://www.hanselman.com/blog/signing-powershell-scripts

###################################
#REVISION HISTORY
#2021.04.22 - kel.mankenberg - Base version

###################################
#RESOURCES
# 

###################################
#VARIABLES
$ScriptName = $MyInvocation.MyCommand.Name
$ComputerName = $env:COMPUTERNAME
$LogPath = $PSScriptRoot + "\Logs\"
$LogFilename = $ScriptName + "_Log.txt"

###################################
# Windows PowerShell: must load assembly System.IO.Compression manually.
# Uncomment next line if using compression
# Add-Type -AssemblyName System.IO.Compression


function EndLogText(){
    #Add blank line at end as a seperator
    Log ("===========================================================================" + "`n`n")
}

function Log ( [String] $Message ) {
    $date = Get-Date -Format "yyyyMMdd-HHmm"
    $Message = $date + ": " + $Message
    
    # Check for Logs folder
    if (!(Test-Path $LogsPath)) {
        New-Item -Path $LogsPath -ItemType Directory
    }

    # Check for log file
    if (!(Test-Path ($LogPath + $LogFilename))) {
        New-Item -Path $LogsPath -Name $LogFilename -ItemType File
        Add-Content ($LogPath + $LogFilename) -Value $Message
    }
    else {
        Add-Content ($LogPath + $LogFilename) -Value $Message
    }
}

function Main {
    StartLogText
    EndLogText
}

function Show-Notification {
    [cmdletbinding()]
    Param (
        [string]
        $ToastTitle,
        [string]
        [parameter(ValueFromPipeline)]
        $ToastText
    )

    [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
    $Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)

    $RawXml = [xml] $Template.GetXml()
    ($RawXml.toast.visual.binding.text|where {$_.id -eq "1"}).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null
    ($RawXml.toast.visual.binding.text|where {$_.id -eq "2"}).AppendChild($RawXml.CreateTextNode($ToastText)) > $null

    $SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument
    $SerializedXml.LoadXml($RawXml.OuterXml)

    $Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml)
    $Toast.Tag = "PowerShell"
    $Toast.Group = "PowerShell"
    $Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1)

    $Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PowerShell")
    $Notifier.Show($Toast);
}

function StartLogText(){
    # Add begining text to log
Log "========================= from $ComputerName (InfoDev $ServerEnv server)"
}

Main