-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateAssessment.ps1
More file actions
132 lines (118 loc) · 4.7 KB
/
createAssessment.ps1
File metadata and controls
132 lines (118 loc) · 4.7 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
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
#Either change these values in the script, or enter them as arguments
[CmdletBinding()]
Param
(
[string]$path="C:\YOUR\PATH\TO\FILES",
[string]$ftpPath="c:\mitrend2\apiSamples\ftpExample.txt",
[string]$email=,
[string]$password=,
#Required for EMC and Partners
[string]$company="YOUR COMPANY NAME",
#Optional for EMC and Partners, Required for Customers
[string]$assessmentName="ASSESSMENT NAME",
#Use 2 letter codes for state and country
[string]$city = "YOUR CITY",
[string]$state="MA",
[string]$county = "US",
#Timezones should be written in the Area/Location format (Note that %2f will escape it for you)
[string]$timezone="US%2FEastern",
#For available device types look at http://mitrend.com/#api/addFiles
[string]$deviceType="Clariion"
)
$apiBase="https://app.mitrend.com/api"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $email,$password)))
$body = @{
company=$company
assessment_name=$assessmentName
city=$city
state=$state
timezone=$timezone
}
echo "Creating $assessmentName"
try{
$content = Invoke-RestMethod "$apiBase/assessments" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -Body $body
}catch [System.Net.WebException] {
Write-Error( "FAILED to reach '$URL': $_" )
throw $_
}
$assessmentId=$content.id
$fileUrl="$apiBase/assessments/$assessmentId/files"
#Powershell doesn't provide a way to do multi part form encoding, this code is taken from http://stackoverflow.com/questions/25075010/upload-multiple-files-from-powershell-script
function Send-Results {
param (
[parameter(Mandatory=$True,Position=1)] [ValidateScript({ Test-Path -PathType Leaf $_ })] [String] $file,
[parameter(Mandatory=$True,Position=2)] [string] $url,
[parameter(Mandatory=$True,Position=3)] [string] $deviceType,
[parameter(Mandatory=$True,Position=3)] [string] $base64AuthInfo
)
$fileBin = [IO.File]::ReadAllBytes($file)
# Convert byte-array to string (without changing anything)
#
$fileEnc = [System.Convert]::ToBase64String($fileBin)
<#
# PowerShell does not (heh) have built-in support for making 'multipart' (i.e. binary file upload compatible)
# form uploads. So we have to craft one...
#
# This is doing similar to:
# $ curl -i -F "file=@file.any" -F "computer=MYPC" http://url
#
# Boundary is anything that is guaranteed not to exist in the sent data (i.e. string long enough)
#
# Note: The protocol is very precise about getting the number of line feeds correct (both CRLF or LF work).
#>
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"
$bodyLines = (
"--$boundary",
"content-transfer-encoding: base64",
"Content-Disposition: form-data; content-transfer-encoding: `"base64`"; name=`"file`"; filename=`" [System.IO.Path]::GetFileName $file`"$LF",
$fileEnc,
"--$boundary",
"Content-Disposition: form-data; name=`"device_type`"$LF",
$deviceType,
"--$boundary--$LF"
) -join $LF
try {
# Returns the response gotten from the server (we pass it on).
#
Invoke-RestMethod -Uri $url -Method Post -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "multipart/form-data; boundary=`"$boundary`"" -TimeoutSec 20 -Body $bodyLines
}
catch [System.Net.WebException] {
Write-Error( "FAILED to reach '$URL': $_" )
throw $_
}
}
if([System.IO.File]::Exists($path)){
$paths=Get-ChildItem $path
foreach($subPath in $paths)
{
$fullPath = Join-Path $path -childPath $subPath
echo $fullPath.name
Write-Host "Uploading $fullpath $fileUrl"
$fileBody={
device_type=$deviceType
File = Get-Content($fullPath) -Raw
}
Send-Results -file $fullPath -url $fileUrl -deviceType $deviceType -base64AuthInfo $base64AuthInfo
}
}
if([System.IO.File]::Exists($ftpPath)){
$reader = [System.IO.File]::OpenText($ftpPath)
try {
for() {
$line = $reader.ReadLine()
if ($line -eq $null) { break }
# process the line
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $email,$password)))
$body = @{
device_type=$deviceType
ftpUrl= $line
}
$content = Invoke-RestMethod $fileUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -Body $body
}
}
finally {
$reader.Close()
}
}
$content = Invoke-RestMethod "$apiBase/assessments/$assessmentId/submit" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -Body $body