forked from canonical/landscape-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-LSComputers.ps1
More file actions
54 lines (44 loc) · 1.88 KB
/
Get-LSComputers.ps1
File metadata and controls
54 lines (44 loc) · 1.88 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
<#
.SYNOPSIS
Get all registered computers from Canonical Landscape
.DESCRIPTION
Get a list of computers associated with the account used for authentication.
.PARAMETER LandscapeAPI
Landscape API endpoint. Defaults to https://landscape.canonical.com/api
.PARAMETER Key
Landscape Access Key
.PARAMETER Secret
Landscape Secret Key
.EXAMPLE
Get-LSComputers -Secret <Secret> -Key <Key> | Format-Table
Gets all computers from Landscape in a table format.
#>
function Get-LSComputers {
[CmdletBinding()]
param (
[string]$LandscapeAPI = "https://landscape.canonical.com/api",
[Parameter(Mandatory, HelpMessage="Landscape Access Key")]
[string]$Key,
[Parameter(Mandatory, HelpMessage="Landscape Secret Key")]
[string]$Secret
)
$Action = "GetComputers"
$LandscapeAPI -match 'https?://(.*)/(.*)' >$null
$Domain = $Matches[1]
$Path = $Matches[2]
Add-Type -AssemblyName System.Web
$Timestamp = (Get-Date (Get-Date).ToUniversalTime() -Format s) + 'Z'
$TimestampEncoded = [System.Web.HTTPUtility]::UrlEncode($Timestamp)
$TimestampEncoded = [string]$TimestampEncoded.ToUpper()
$Message = "GET`n$Domain`n/$Path/`n"
$Params = "access_key_id=$($Key)&action=$($Action)&signature_method=HmacSHA256&signature_version=2×tamp=$($TimestampEncoded)&version=2011-08-01"
$Message = $Message + $Params
# Signature
$Hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$Hmacsha.key = [Text.Encoding]::UTF8.GetBytes($Secret)
$Hash = $Hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($Message))
$Base64Hash = [Convert]::ToBase64String($Hash)
$EncodedBase64Hash = [System.Web.HTTPUtility]::UrlEncode($Base64Hash)
$URI = $LandscapeAPI + "/?" + $Params + "&signature=$($EncodedBase64Hash)"
Return Invoke-RestMethod -Method Get -Uri $URI -UseBasicParsing
}