upload/Upload-HubBom.ps1
|
function Upload-HubBom { <# .SYNOPSIS Uploads a Hub BOM file (BDIO) to the Hub instance .DESCRIPTION Uploads a BOM file generated by Hub-Detect or other means to the Hub. This is the equivalent operation of Navigating to the Scans page in the Hub's web interfaces, clicking on the "Add" menu, and choosing 'Bom File". .EXAMPLE Upload-HubBom ./myBomFile.jsonld --------------------------------------------------------------- Uploads the BDIO file myBomFile.jsonld to the hub .EXAMPLE dir ./*.jsonld | Upload-HubBom -V --------------------------------------------------------------- Uploads all the .jsonld files in the current directory to the Hub. With the verbose output (-V) enabled, the path of each successfully uploaded file will be displayed. #> Param( # The file to upload [Parameter(ValueFromPipeline, HelpMessage = 'The File to be uploaded', Mandatory = $true, ParameterSetName = 'FileInfoInPipeline')] [System.IO.FileSystemInfo] $FileInfo, [Parameter(HelpMessage = 'The File to be uploaded', Mandatory = $true, ParameterSetName = 'FilePathInArgument', Position = 0)] [string] $Path ) begin { VerifyHubLogin } process { $multipartContent = [System.Net.Http.MultipartFormDataContent]::new() if ($FileInfo) { $fileStream = $fileStream = [System.IO.fileStream]::new($FileInfo.FullName, [System.IO.FileMode]::Open) $fileName = $FileInfo.Name Write-Verbose "Uploading $($FileInfo.FullName)" } else { $fileStream = [System.IO.fileStream]::new($Path, [System.IO.FileMode]::Open) $fileName = split-path -Path $Path -Leaf Write-Verbose "Uploading ${Path}" } try { $fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data") $fileHeader.Name = "file" $fileHeader.FileName = $fileName $fileContent = [System.Net.Http.StreamContent]::new($fileStream) $fileContent.Headers.ContentDisposition = $fileHeader $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("application/json") $multipartContent.Add($fileContent) $response = Invoke-WebRequest -Uri "${Global:hubUrl}/api/v1/bom-import" -Body $multipartContent -Method 'POST' -Verbose:$false @Global:hubInvocationParams if (!$response -or $response.StatusCode -lt 200 -or $response.StatusCode -ge 300 ) { $message = "Unable to upload. "; if ($response) { $message = "${message} $($response.StatusCode) $($response.Content)" } throw message } } finally { if ($fileStream) { $fileStream.Close(); } } } } |