-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCENTRAL_API_Grab_Installers.ps1
142 lines (126 loc) · 5.39 KB
/
CENTRAL_API_Grab_Installers.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
param (
[Parameter(Mandatory=$true)]
[string]$ParamClientId = "",
[Parameter(Mandatory=$true)]
[string]$ParamClientSecret = ""
)
function Split-StringAfterEqualSign {
param (
[string]$inputString
)
try {
if (-not $inputString.Contains("=")) {
throw "Input string does not contain an '=' sign."
}
$splitString = $inputString -split "="
return @{
Key = $splitString[0]
Value = $splitString[1]
}
}
catch {
Write-Error "An error occurred: $_"
}
}
Clear-Host
try {
if (($null -eq $ParamClientId) -or ($ParamClientId -eq "")) {
Write-Output "No Client Id provided"
{break}
}
if (($null -eq $ParamClientSecret) -or ($ParamClientSecret -eq "")){
Write-Output "No Client Secret provided"
{break}
}
else {
$resultClient = Split-StringAfterEqualSign -inputString $ParamClientId
#Write-Host "Id Client : "$resultClient.Key
#Write-Host "Client ID : "$resultClient.Value
$ClientId=$resultClient.Value
$resultSecret = Split-StringAfterEqualSign -inputString $ParamClientSecret
#Write-Host "Id Secret : "$resultSecret.Key
#Write-Host "Client Secret : "$resultSecret.Value
$ClientSecret=$resultSecret.Value
}
} catch {
Write-Error "A basic error occurred: $_"
exit 1
}
Write-Output "==============================================================================="
Write-Output " Sophos Central API - Retrieve installers URL for the Central tenant"
Write-Output "==============================================================================="
#CSV filename and full directory
$ScriptLaunchDate= Get-Date -Format "yyyyMMddHHmmssfff"
$OutputFile = "Installation_package_URLs_$ScriptLaunchDate.json"
# SOPHOS OAuth URL
$AuthURI = "https://id.sophos.com/api/v2/oauth2/token"
# Body and Header for oAuth2 Authentication
$AuthBody = @{}
$AuthBody.Add("grant_type", "client_credentials")
$AuthBody.Add("client_id", $ClientId)
$AuthBody.Add("client_secret", $ClientSecret)
#$AuthBody.Add("client_id", $SecureCredentials.GetNetworkCredential().Username)
#$AuthBody.Add("client_secret", $SecureCredentials.GetNetworkCredential().Password)
$AuthBody.Add("scope", "token")
$AuthHead = @{}
$AuthHead.Add("content-type", "application/x-www-form-urlencoded")
# Set TLS Version
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Post Request to SOPHOS for OAuth2 token
try {
$Result = (Invoke-RestMethod -Method Post -Uri $AuthURI -Body $AuthBody -Headers $AuthHead -ErrorAction SilentlyContinue -ErrorVariable ScriptError)
if ($SaveCredentials) {
$ClientSecret = $ClientSecret | ConvertFrom-SecureString
ConvertTo-Json $ClientID, $ClientSecret | Out-File $CredentialFile -Force
}
} catch {
# If there's an error requesting the token, say so, display the error, and break:
Write-Output ""
Write-Output "AUTHENTICATION FAILED - Unable to retreive SOPHOS API Authentication Token"
Write-Output "Please verify the credentials used!"
Read-Host -Prompt "Press ENTER to continue..."
exit 2
}
# Set the Token for use later on:
$Token = $Result.access_token
# SOPHOS Whoami URI:
$WhoamiURI = "https://api.central.sophos.com/whoami/v1"
# SOPHOS Whoami Headers:
$WhoamiHead = @{}
$WhoamiHead.Add("Content-Type", "application/json")
$WhoamiHead.Add("Authorization", "Bearer $Token")
# Post Request to SOPHOS for Whoami Details:
$Result = (Invoke-RestMethod -Method Get -Uri $WhoamiURI -Headers $WhoamiHead -ErrorAction SilentlyContinue -ErrorVariable ScriptError)
# Check if we are using tenant (Central Admin) credentials
if ($Result.idType -ne "tenant") {
Write-Output "Aborting script - idType does not match tenant!"
Break
}
# Save Response details
$TenantID = $Result.id
$DataRegion = $Result.ApiHosts.dataRegion
# SOPHOS Endpoint API Headers:
$TenantHead = @{}
$TenantHead.Add("Authorization" ,"Bearer $Token")
$TenantHead.Add("X-Tenant-ID" ,"$TenantID")
$TenantHead.Add("Content-Type", "application/json")
# List all Installers sorted by PLatform and Platform Type
$Getinstallers = (Invoke-RestMethod -Method Get -Uri $DataRegion"/endpoint/v1/downloads" -Headers $TenantHead -ErrorAction SilentlyContinue -ErrorVariable ScriptError)
Write-Output $GetInstallers.Installers | Format-Table @{label='Product Name';e={$_.productName}},
@{label='Platform';e={$_.platform}},
@{label='Type';e={$_.type}},
@{label='Supported Products';e={$_.supportedProducts}},
@{label='Link';e={$_.downloadUrl}}
$InstallersUrlList = @()
foreach ($Node in $Getinstallers.installers) {
$InstallersUrlList += [pscustomobject]@{
"Product Name" = $Node.productName
"Platform" = $Node.platform
"type" = $Node.type
"Download URL" = $Node.downloadUrl
}
}
#$InstallersUrlList | Format-Table -Wrap
$Table_In_JSON = $InstallersUrlList | Sort-Object -Property id | ConvertTo-Json -Depth 5
#$Table_In_JSON
$Table_In_JSON | Out-File -FilePath $OutputFile utf8