@@ -122,4 +122,128 @@ function New-CohesitySessionId {
122122 CSLog - Message $errorMsg - Severity 3
123123 }
124124
125+ }
126+ $Global :CohesityUserAgentName = $null
127+ $Global :CohesityAPIError = $null
128+
129+ # Allow the caller to have access to response object,
130+ # it is observed that some of the REST APIs (PUT method) do not return object,
131+ # therefore provisioning an object, so that the caller can identify using the status code, if the API call succeeded
132+ # Capture all response codes and the relevant error messages.
133+ $Global :CohesityAPIStatus = $null
134+
135+ function New-CohesitySessionId {
136+ <#
137+ . SYNOPSIS
138+ Returns the session ID For the user
139+ . DESCRIPTION
140+ Create the user session
141+ . NOTES
142+ Published by Cohesity
143+ . LINK
144+ https://cohesity.github.io/cohesity-powershell-module/#/README
145+ . EXAMPLE
146+ New-CohesitySessionId -Server 10.0.0.1 -Credential (Get-Credential)
147+ #>
148+ [CmdletBinding ()]
149+ Param (
150+ [Parameter (Mandatory = $true )]
151+ [string []]$Server ,
152+
153+ [Parameter (Mandatory = $true )]
154+ [PSCredential ]$Credential ,
155+
156+ [Parameter (Mandatory = $false )]
157+ [string ]$Domain = " LOCAL" , # default domain
158+
159+ [Parameter (Mandatory = $false )]
160+ [string ]$OtpCode ,
161+
162+ [Parameter (Mandatory = $false )]
163+ [string ]$OtpType
164+ )
165+ Enable-SelfSignedCertificates
166+ if ($null -eq $Global :CohesityCmdletConfig ) {
167+ $Global :CohesityCmdletConfig = Get-CohesityCmdletConfig
168+ }
169+
170+
171+ $Global :CohesityAPIError = $null
172+ try {
173+ $username = $Credential.UserName
174+ $password = $Credential.GetNetworkCredential ().Password
175+
176+ $cohesitycredentials = @ {
177+ otpType = " Totp"
178+ certificate = $null
179+ domain = " LOCAL"
180+ otpCode = $null
181+ password = $password
182+ privateKey = $null
183+ username = $username
184+ }
185+ $jsonProfile = $cohesitycredentials | ConvertTo-Json - Compress - Depth 5
186+
187+ [Environment ]::SetEnvironmentVariable(" cohesityCredentials" , $jsonProfile , ' Process' )
188+
189+ $baseUri = " https://$Server "
190+ $sessionUri = $baseUri + " /v2/users/sessions"
191+ $sessionBody = @ {
192+ username = $Username
193+ password = $Password
194+ }
195+
196+ $jsonBody = $sessionBody | ConvertTo-Json - Compress - Depth 5
197+
198+ $PSBoundParameters = @ {
199+ Uri = $sessionUri
200+ Method = ' Post'
201+ Body = $jsonBody
202+ }
203+
204+ $result = Invoke-WebRequest @PSBoundParameters
205+
206+ if ($result.Content ) {
207+ $parsedResponse = $result.Content | ConvertFrom-Json
208+ $sessionId = $parsedResponse.sessionId
209+ $cohesitySession = @ {
210+ ClusterUri = $baseUri
211+ AllowInvalidServerCertificates = $true
212+ AccessToken = $null
213+ SessionId = $sessionId
214+ ApiKey = $null
215+ }
216+ CohesityUserProfile - UserProfileData $cohesitySession
217+ }
218+ else {
219+ $errorMsg = " Session Id was not Created"
220+ Write-Output $errorMsg
221+ CSLog - Message $errorMsg
222+ }
223+
224+ if ($Global :CohesityCmdletConfig ) {
225+ if ($Global :CohesityCmdletConfig.LogResponseData -eq $true ) {
226+ if ($result.Content ) {
227+ Write-Host " Connected to the Cohesity Cluster $Server Successfully"
228+ }
229+ else {
230+ CSLog - Message " Response content not available" - Severity 1
231+ }
232+ }
233+ }
234+
235+
236+ }
237+ catch {
238+ # this flag can be optionally used by the caller to identify the details of failure
239+ $Global :CohesityAPIError = $_.Exception
240+ # to make the ScriptAnalyzer happy
241+ CSLog - Message ($Global :CohesityAPIError | ConvertTo-json ) - Severity 3
242+ # capturing the error message from the cluster rather than the powershell framework $_.Exception.Message
243+ $errorMsg = $_
244+ $Global :CohesityAPIStatus = ConstructResponseWithStatus - APIResponse $errorMsg
245+ Write-Output $errorMsg
246+ CSLog - Message $errorMsg - Severity 3
247+ }
248+
125249}
0 commit comments