-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoke-upload.ps1
164 lines (131 loc) · 5.46 KB
/
invoke-upload.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
param (
[string][parameter(Mandatory = $true)][ValidateNotNullOrEmpty()] $File,
[string][parameter(Mandatory = $true)][ValidateNotNullOrEmpty()] $Server
)
#Add Certification Exception for connecting to the BigFix Server
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
#Import net assemblies for HTTP Client
Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Net.http
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#Credit for this function goes to: Mario Majcica
# http://blog.majcica.com/2016/01/13/powershell-tips-and-tricks-multipartform-data-requests/
function Invoke-bffileimport
{
[CmdletBinding()]
PARAM
(
[string][parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]$InFile,
[string]$ContentType,
[Uri][parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]$Uri = "$(Get-BFServer)" + "api/upload" ,
[System.Management.Automation.PSCredential]$Credential
)
BEGIN
{
if (-not (Test-Path $InFile))
{
$errorMessage = ("File {0} missing or unable to read." -f $InFile)
$exception = New-Object System.Exception $errorMessage
$errorRecord = New-Object System.Management.Automation.ErrorRecord $exception, 'MultipartFormDataUpload', ([System.Management.Automation.ErrorCategory]::InvalidArgument), $InFile
$PSCmdlet.ThrowTerminatingError($errorRecord)
}
if (-not $ContentType)
{
Add-Type -AssemblyName System.Web
$mimeType = [System.Web.MimeMapping]::GetMimeMapping($InFile)
if ($mimeType)
{
$ContentType = $mimeType
}
else
{
$ContentType = "application/octet-stream"
}
}
}
PROCESS
{
Add-Type -AssemblyName System.Net.Http
$httpClientHandler = New-Object System.Net.Http.HttpClientHandler
if ($Credential)
{
$networkCredential = New-Object System.Net.NetworkCredential @($Credential.UserName, $Credential.Password)
$httpClientHandler.Credentials = $networkCredential
}
$httpClient = New-Object System.Net.Http.Httpclient $httpClientHandler
$httpClient.Timeout = New-TimeSpan -Minutes 10
#$httpClient.DefaultRequestHeaders.Authorization = (get-bfconnectheaders)
$packageFileStream = New-Object System.IO.FileStream @($InFile, [System.IO.FileMode]::Open)
$contentDispositionHeaderValue = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue "form-data"
$contentDispositionHeaderValue.Name = "fileData"
$contentDispositionHeaderValue.FileName = (Split-Path $InFile -leaf)
$streamContent = New-Object System.Net.Http.StreamContent $packageFileStream
$streamContent.Headers.ContentDisposition = $contentDispositionHeaderValue
$streamContent.Headers.ContentType = New-Object System.Net.Http.Headers.MediaTypeHeaderValue $ContentType
$content = New-Object System.Net.Http.MultipartFormDataContent
$content.Add($streamContent)
try
{
$response = $httpClient.PostAsync($Uri, $content).Result
if (!$response.IsSuccessStatusCode)
{
$responseBody = $response.Content.ReadAsStringAsync().Result
$errorMessage = "Status code {0}. Reason {1}. Server reported the following message: {2}." -f $response.StatusCode, $response.ReasonPhrase, $responseBody
throw [System.Net.Http.HttpRequestException] $errorMessage
}
return $response.Content.ReadAsStringAsync().Result
}
catch [Exception]
{
$PSCmdlet.ThrowTerminatingError($_)
}
finally
{
if($null -ne $httpClient)
{
$httpClient.Dispose()
}
if($null -ne $response)
{
$response.Dispose()
}
}
}
END { }
}
#Verify File Exists
if (!(test-path $File)) { throw "Invalid file path provided" }
$NewFile = get-item $File
#Get Credentials for upload
$Credentials = Get-Credential
#Perform Upload
$Result = Invoke-BFFileImport -Uri "https://$($Server):52311/api/upload" -InFile $NewFile -Credential $Credentials
if ($Result -eq $null) { write-host "No response from server?!"; exit 0 }
if (!(select-xml -content ($Result) -xpath "/BESAPI/FileUpload")) {throw "Unknown response from server" }
#Prepare Prefetch
$URL = (select-xml -content ($Result) -xpath "/BESAPI/FileUpload/URL").node.InnerText
$Name = $NewFile.Name
$SHA1 = (select-xml -content ($Result) -xpath "/BESAPI/FileUpload/SHA1").node.InnerText
$SHA256 = (select-xml -content ($Result) -xpath "/BESAPI/FileUpload/SHA256").node.InnerText
$Size = (select-xml -content ($Result) -xpath "/BESAPI/FileUpload/Size").node.InnerText
#Print Prefetch
$Output
$Output = "prefetch $SHA1 sha1:$SHA1 size:$Size $URL sha256:$SHA256 `n"
#Provide extraction commands if the file is an archive
if ($NewFile.Extension -like "*bftemp*") {
$Output += "move ""__Download\$SHA1"" ""__Download\$Name"" `n"
$Output += "extract ""$Name"" `n"
}
write-output $Output
$output | clip