Private/Get-UnityAuth.ps1

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
Function Get-UnityAuth {
  [CmdletBinding()]
  Param(
    [Parameter(Mandatory = $true,Position = 0,HelpMessage = 'EMC Unity FQDN or IP address',ParameterSetName='p1')]
    [ValidateNotNullorEmpty()]
    [String]$Server,
    [Parameter(Mandatory = $true,Position = 0,HelpMessage = 'EMC Unity Rest URI',ParameterSetName='p2')]
    [ValidateNotNullorEmpty()]
    [String]$URI,
    [Parameter(Mandatory = $true,Position = 1,HelpMessage = 'EMC Unity Encoded Password')]
    $EncodedPassword,
    [Parameter(Mandatory = $false,Position = 2,HelpMessage = 'EMC Unity Cookies')]
    [System.Net.CookieContainer]$Cookies
  )

  If ($PsCmdlet.ParameterSetName -eq 'p1') {
    Write-Verbose -Message 'Build the URI'
    $URI= 'https://'+$Server+'/api/types/system/instances'
  }

  Write-Verbose "URI: $URI"

  $Request = [System.Net.HttpWebRequest]::Create($URI)
  $Request.CookieContainer = $Cookies
  $Request.AllowAutoRedirect = $false
  $Request.Accept = "application/json"
  $Request.ContentType = "application/json"
  $Request.Headers.Add("X-EMC-REST-CLIENT","true")
  $Request.Headers.Add('Authorization',"Basic $($EncodedPassword)")

  Write-Verbose "Sending authentication request"

  Try {
    $NewResponse = $Request.GetResponse()
    $NewResponse.Close()
  }
  Catch {
    Show-RequestException -Exception $_
    throw
  }

  Write-Verbose "Processing cookies"
  Foreach ($cookie in $NewResponse.Cookies) {
    Write-Verbose "Adding cookie: $($cookie.Name)"
    $Cookies.Add($cookie)
  }

  Write-Verbose "Response Status Code: $($NewResponse.StatusCode)"

  If ($NewResponse.StatusCode -eq 'OK') {

    $headers = @{}
    $headers["Accept"] = "application/json"
    $headers["X-EMC-REST-CLIENT"] = "true"
    $headers["EMC-CSRF-TOKEN"] = $NewResponse.Headers.item('EMC-CSRF-TOKEN')

    $result = @{}
    $result['Headers'] = $headers
    $result['Cookies'] = $Cookies.GetCookies($uri)

    return $result

  } else {
    Get-UnityAuth -URI $NewResponse.Headers.item('Location') -EncodedPassword $EncodedPassword -Cookies $Cookies
  }
}