forked from kairood/SysAid-PowerShell-Automation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSysAid_Authentication.ps1
More file actions
87 lines (50 loc) · 1.55 KB
/
Copy pathSysAid_Authentication.ps1
File metadata and controls
87 lines (50 loc) · 1.55 KB
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
##EXAMPLE
# Get-SysAidToken -Username "admin" -Password "Password1" -SysAidURL "https://sysaid.example.com"
Function Get-SysAidToken {
Param(
[parameter(Mandatory=$true)]
[String]$Username,
[Parameter(Mandatory=$true)]
[String]$Password,
[Parameter(Mandatory=$true)]
[string]$SysAidURL
)
[System.Uri]$Uri = "$SysAidURL/api/v1/login"
$ContentType = "application/json" # Add the content type
$Method = 'POST' # Add the method type
$Body = @"
{
"user_name": "$Username",
"password": "$Password"
}
"@ # Add the body for the request
$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
# Splat the parameters
$props = @{
Uri = $Uri.AbsoluteUri
ContentType = $ContentType
Method = $Method
WebSession = $WebSession
Body = $Body
}
Try {
$WebRequest = Invoke-WebRequest @props -ErrorAction Stop
#Capture Status
$StatusCode = $WebRequest.StatusCode
}
Catch {
$StatusCode = $_.Exception.Response.StatusCode.value__
}
if ($StatusCode -eq "200") {
Write-Verbose "The Web Request Was Successful, Capturing JSESSIONID Cookie..." -Verbose
#Capture JSESSIONID Cookie
$cookies = $WebSession.Cookies.GetCookies($Uri)
Write-Verbose "$cookies" -Verbose
}
elseif ($StatusCode -eq "401") {
Write-Verbose "The Web Request Failed, with status code $StatusCode. Please enter the correct login information." -Verbose
}
elseif ($StatusCode -ne "200" -or "401") {
Write-Verbose "The Web Request Failed, with status code $StatusCode. Please verify the entered parameters." -Verbose
}
}