Public/Connect-Unity.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Function Connect-Unity {

  <#
      .SYNOPSIS
      Connects to an EMC Unity Array
      .DESCRIPTION
      Connects to an EMC Unity Array. This cmdlet starts a new session with an EMC Unity Array using the specified parameters.
      When you attempt to connect to an array, the array checks for valid certificates. To avoid this use the -TrusAllCerts param.
      You can have more than one connection to the same array. To disconnect from an array, you need to close all active connections to this server using the Disconnect-Unity cmdlet.
      Every new connection is stored in the $global:DefaultUnitySession array.
      .NOTES
      Written by Erwan Quelin under Apache licence
      .LINK
      https://github.com/equelin/Unity-Powershell
      .EXAMPLE
      Connect-Unity -Server 192.168.0.1
 
      Connects to the array with the IP 192.168.0.1
      .EXAMPLE
      Connect-Unity -Server 192.168.0.1,192.168.0.2
 
      Connects to the array with the IP 192.168.0.1 and 192.168.0.2
  #>


  [CmdletBinding(DefaultParameterSetName="ByServer")]
  Param(
      [Parameter(Mandatory = $true,Position = 0,ParameterSetName="ByServer",ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True,HelpMessage = 'EMC Unity FQDN or IP address')]
      [String]$Server,
      [Parameter(Mandatory = $true,Position = 0,ParameterSetName="BySession",ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True,HelpMessage = 'EMC Unity Session object')]
      [UnitySession]$Session,
      [Parameter(Mandatory = $false,HelpMessage = 'EMC Unity username')]
      [String]$Username,
      [Parameter(Mandatory = $false,HelpMessage = 'EMC Unity password')]
      [SecureString]$Password,
      [Parameter(Mandatory = $false,HelpMessage = 'EMC Unity credentials')]
      [System.Management.Automation.CredentialAttribute()]$Credentials,
      [Parameter(Mandatory = $false,HelpMessage = 'Trust all certs ?')]
      [Switch]$TrustAllCerts
  )

  Begin {
    Write-Verbose "Executing function: $($MyInvocation.MyCommand)"
    if ($TrustAllCerts.IsPresent) {
      Unblock-UnityCerts
    }
  }

  Process {

    If ($PsCmdlet.ParameterSetName -eq 'BySession') {
      Write-Verbose -Message "Disconnect or delete previous session"
      Disconnect-Unity -Session $Session -Confirm:$False

      Write-Verbose -Message "Server: $($Session.Server)"
      $Server = $Session.Server
    }

    Write-Verbose -Message 'Validating that login details were passed into username/password or credentials'
    if ($Password -eq $null -and $Credentials -eq $null)
    {
        Write-Verbose -Message 'Missing username, password, or credentials.'
        $Credentials = Get-Credential -Message 'Please enter administrative credentials for your EMC Unity Array'
    }

    if ($Credentials -eq $null)
    {
        $Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password
    }

    #Encoding username and password for HTTP Basic authentication
    $EncodedAuthorization = [System.Text.Encoding]::UTF8.GetBytes($Credentials.username + ':' + $Credentials.GetNetworkCredential().Password)
    $EncodedPassword = [System.Convert]::ToBase64String($EncodedAuthorization)

    #Initialazing Cookies Container
    $Cookies = New-Object -TypeName System.Net.CookieContainer

    $result = Get-UnityAuth -Server $server -EncodedPassword $EncodedPassword -Cookies $Cookies

    #Building the UnitySession Object
    $Sess = New-Object -TypeName UnitySession
    $Sess.IsConnected = $true
    $Sess.Server = $Server
    $Sess.Headers = $result['Headers']
    $Sess.Cookies = $result['Cookies']
    $Sess.SessionId = ([guid]::NewGuid())
    $Sess.User = $Credentials.username

    $System = Get-UnitySystem -Session $Sess

    $Sess.Name = $System.Name
    $Sess.Model = $System.model
    $Sess.SerialNumber = $System.SerialNumber

    #Add the UnitySession Object to the $global:DefaultUnitySession array
    $global:DefaultUnitySession += $Sess

    #Return the new session
    Write-Output $Sess
  }

  End {}

}