Search notes:

Powershell module: bypassEmailProxyRules

The PowerShell module bypassEmailProxyRules provides some functions that should outsmart corporate email proxies so that files can be attached to emails that otherwise couldn't.

Functions

format-saveEmailProxyExtension Turns the extension of a filename into an extension that (hopefully) is not rejected by a corporate email proxy (for example C:\foo\bar.ps1 turns into C:\foo\bar.p-s-1-)
format-unsaveEmailProxyExtension Returns the original filename from one that was changed using format-saveEmailProxyExtension
format-bypassEmailProxyRulesFile Replace numbers with non-numbers (and vice-versa). This method was necessary to send a file through a proxy that marked attachments with sequences of digits which it mistook for account numbers (which were not permitted to be sent).
new-bypassEmailProxyRulesZipArchive Create a ZIP archive whose entries have file extensions that were formatted with format-saveEmailProxyExtension, see creating and extracting ZIP Archives with bypassEmailProxyRulesZipArchive
expand-bypassEmailProxyRulesZipArchive Extract the entries of a ZIP archive that was created with new-bypassEmailProxyRulesZipArchive, see creating and extracting ZIP Archives with bypassEmailProxyRulesZipArchive

bypassEmailProxyRules.psm1

# V0.3

set-strictMode -version latest

function format-saveEmailProxyExtension {

   param (
      [parameter (mandatory=$true)]
      [string[]]   $fileNames
   )
   return ($fileNames -replace '(?!.*\.)(.)', '$1-')
}

function format-unsaveEmailProxyExtension {
   param (
      [parameter (mandatory=$true)]
      [string[]]    $fileNames
   )

   return ($fileNames -replace '(?!.*\.)(?:(.)-)', '$1')
}

function new-bypassEmailProxyRulesZipArchive {

   param (
      [parameter (mandatory=$true)]
      [string]    $dirName,
      [parameter (mandatory=$true)]
      [string]    $zipName
   )

   $zip = new-zipArchive $zipName

   get-childItem  -path $dirName -recurse -name -attributes !directory | foreach-object {

      $entry_name = format-saveEmailProxyExtension $_

      add-zipEntry "$dirName/$_"   $entry_name   $zip
   }

   close-zipArchive $zip
}

function expand-bypassEmailProxyRulesZipArchive {

   param (
      [parameter (mandatory=$true)]
      [string]    $zipName,
      [parameter (mandatory=$true)]
      [string]    $destDirName
   )

   $zip = open-zipArchive $zipName

   foreach ($entry in $zip.entries) {

      $orig_name = "$destDirName/" + (format-unsaveEmailProxyExtension ($entry.FullName))

      $abs_file = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($orig_name)
      $abs_path = $ExecutionContext.SessionState.Path.ParseParent($abs_file, $null);

      if (-not (test-path $abs_path)) {
         $null = new-item $abs_path -type directory
      }

     [System.IO.Compression.ZipFileExtensions]::ExtractToFile(
        $entry,
        $abs_file,
        $true  # overwrite
      )
   }

   close-zipArchive $zip
}

function format-bypassEmailProxyRulesFile {
   param (
      [parameter(valueFromPipeline = $true, mandatory = $true)]
      [string]               $filename,
      [parameter()]
      [System.Text.Encoding] $encoding = (new-object System.Text.UTF8Encoding $false)
   )

   process {

      $text       = [System.IO.File]::ReadAllText($ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($filename), $encoding)
      [string] $r = ''

      foreach ($c in $text.ToCharArray()) {

          [int] $n = $c

          if ($encoding.IsSingleByte) {

             $start = 0xb0 # 176

             if     ( $n -ge      48 -and  $n -le      57   ) { $r += [char] ($n + $start - 48) }
             elseif ( $n -ge  $start -and  $n -le  $start+9 ) { $r += [char] ($n - $start + 48) }
             else                                             { $r += $c                        }

          }
          else {

            if     ( $n -ge   48 -and  $n -le   57 ) { $r += [char] ($n + 8256) }
            elseif ( $n -ge 8304 -and  $n -le 8313 ) { $r += [char] ($n - 8256) }
            else                                     { $r += $c                 }
          }

      }

      [System.IO.File]::WriteAllText($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$filename.out"), $r, $encoding)
   }
}
Github repository ps-modules-bypassEmailProxyRules, path: /bypassEmailProxyRules.psm1

bypassEmailProxyRules.psd1

@{
   RootModule        = 'bypassEmailProxyRules.psm1'
   ModuleVersion     = '0.3'
   FunctionsToExport = @(
     'format-saveEmailProxyExtension'         ,
     'format-unsaveEmailProxyExtension'       ,
     'new-bypassEmailProxyRulesZipArchive'    ,
     'expand-bypassEmailProxyRulesZipArchive' ,
     'format-bypassEmailProxyRulesFile'
   )
}
Github repository ps-modules-bypassEmailProxyRules, path: /bypassEmailProxyRules.psd1

History

V0.1 format-saveEmailProxyExtension and format-unsaveEmailProxyExtension
V0.2 Add new-bypassEmailProxyRulesZipArchive and expand-bypassEmailProxyRulesZipArchive (2021-06-14)
V0.3 Add format-bypassEmailProxyRulesFile

See also

René's simple PowerShell modules

Index