Ephesoft.Transact.Common.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# This internal function returns the formatted URI for the Transact instance
Function Get-TransactURI {
  [CmdLetBinding()]
  [OutputType([string])]
  param (
    [Parameter (Mandatory = $true)]
    [string] $Hostname,
    [Parameter (Mandatory = $true)]
    [string] $Endpoint,
    [ValidateRange(1,65535)]
    [int] $Port,
    [switch] $UseHTTP,
    [switch] $AcknowledgeInsecure
  )
    # Prepare URL to use
    $Hostname += ":$Port"
    $protocol = 'https://'
    if ($UseHTTP) {
      if ($AcknowledgeInsecure) {
        $protocol = 'http://'
      }
      else {
        throw 'UseHTTP specified which will send credentials insecurely to target server. To force the insecure connection add the AcknowledgeInsecure switch. WARNING: This should only be done in on-box development/testing scenarios.'
      }
    }
    return "$protocol$Hostname/dcma/rest/$Endpoint"
}

# This internal function returns correctly formatted multipart file content
Function Get-MultiPartFileContent {
  [CMDLetBinding()]
  param (
    [Parameter (Mandatory = $true, ValueFromPipeline = $true)]
    [string] $FilePath
  )
  begin {
    # Create Multipart form data content object
    $multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
  }

  process {
    # Create file stream
    $fileStream = [System.IO.FileStream]::new($FilePath, [System.IO.FileMode]::Open)
    [array]$fileStreams += $fileStream

    # Create file header
    $fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
    $fileHeader.Name = (Get-ChildItem $FilePath).name
    $fileHeader.FileName = "$FilePath"

    # Create file content
    $fileContent = [System.Net.Http.StreamContent]::new($fileStream)
    $fileContent.Headers.ContentDisposition = $fileHeader

    # Add this file's content to the multipart form data
    $multipartContent.Add($fileContent)
  }
  end {

    # Note that the multipart content type is passed as an array unless it is wrapped in another object
    return [pscustomobject] @{
      Content = $multipartContent
      FileStreams = $fileStreams
    }
  }
}