MHA-IT.CommonFunctions.psm1
# Common scripts for Maryhill Housing Association. Initialised by Ewan McLean 2021-04-19 # Functions for emailing out function SendMail ($to, $subject, $body, $attachment) { #Meta function in case we change mail system in future SendMailO365 $to $subject $body $attachment } function SendMailO365 ($to, $subject, $body, $attachment) { $hostname = hostname $SmtpServer = "maryhill-org-uk.mail.protection.outlook.com" $from = $hostname + "@maryhill.org.uk" if ($attachment) { Send-MailMessage -From $from -to $to -SmtpServer $SmtpServer -Body $body -Subject $subject -Attachments $attachment } else { Send-MailMessage -From $from -to $to -SmtpServer $SmtpServer -Body $body -Subject $subject } } function Send-MailgunEmail($to, $subject, $body, $attachment) { #This function does not work properly (struggling with sending the attachment) $emaildomain = "maryhill.org.uk" $hostname = hostname $from = $hostname + "@" + $emaildomain $apikey = "key-XXX" #$attachment = [IO.File]::ReadAllText($attachment); $idpass = "api:$($apikey)" $basicauth = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($idpass)) $headers = @{ Authorization = "Basic $basicauth" } $url = "https://api.mailgun.net/v3/$($emaildomain)/messages" $body = @{ from = $from; to = $to; subject = $subject; text = $body; attachment = Get-Item -Path $attachment } Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body -ContentType "multipart/form-data;" } function Connect-O365 { param( $PrivAccCredential ) $session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $PrivAccCredential -Authentication Basic -AllowRedirection Import-Module (Import-PSSession $session365 -AllowClobber) -Global Connect-MsolService -Credential $o365cred } function Get-RandomPassphrase([bool] $addDigit) { # https://www.hanselman.com/blog/DictionaryPasswordGeneratorInPowershell.aspx $words = import-csv "$PSScriptRoot\passphrasedictionary.csv" $conjunction = "the", "my", "we", "our", "and", "but", "or" $rand = new-object System.Random $word1 = ($words[$rand.Next(0, $words.Count)]).Word $con = ($conjunction[$rand.Next(0, $conjunction.Count)]) $word2 = ($words[$rand.Next(0, $words.Count)]).Word if ($addDigit) { $digit = $rand.Next(2, 9) # don't want 0 or 1 - they look like O and l } else { $digit = "" } return $word1 + "-" + $con + "-" + $word2 + $digit } function Set-PrivAccCredential { $PrivAccCredential = Get-Credential -Message "Enter the appropriate PrivAcc credential" return $PrivAccCredential } |