OutlookConnector.HelperFunctions.ps1

# Helper functions used within Outlook Connector module
# Functions are not exported out of module

function Get-ValidFileName {
    # refference https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
    #https://gallery.technet.microsoft.com/scriptcenter/Save-Email-from-Outlook-to-3abf1ff3#content
    
    param([Parameter(Mandatory=$true)][String]$FileName)

    # removing illegal characters
    foreach ($char in ([System.IO.Path]::GetInvalidFileNameChars())) {$FileName = $FileName -replace ('\'+$char),'_'}

    # trimming spaces and dots and removing extra long characters
    if (($FileName.Length) -gt 122) {$FileName = $FileName.Substring(0,123)} # 122 as we do not have extension yet
    while ($FileName -match '(^[\s\.])|([\s\.]$)') {$FileName = $FileName.Trim(' ').Trim(".")}

    # return value
    $FileName
    }

function New-Folder {
    param([Parameter(Mandatory=$true)][String]$TargetFolder)

    if (!(Test-Path -Path $TargetFolder)) {
        try {
            mkdir -Path $TargetFolder | Out-Null
        } catch {
            throw "Target folder $TargetFolder can't be created."
        }
    }
}