use invoke-webrequest, plan support for images

This commit is contained in:
Petri Asikainen 2021-06-21 10:13:43 +03:00
parent 95839398e9
commit 193f3f7f11
3 changed files with 48 additions and 29 deletions

View file

@ -35,7 +35,6 @@
}
#To support images "image" property have be handled before this
if($Body) { $body = $Body | ConvertTo-Json }
$_headers = @{
"Authorization" = "Bearer $($token)"
@ -60,9 +59,24 @@
Headers = $_headers
UseBasicParsing = $true
ErrorAction = 'SilentlyContinue'
Proxy = 'http://localhost:8080'
}
if ($Body) {$splatParameters["Body"] = [System.Text.Encoding]::UTF8.GetBytes($Body)}
#Place holder for intended image manipulation
# if and when snipe it API gets support for images
if($null -ne $body -and $Body.Keys -contains 'image' ){
if($PSVersionTable.PSVersion -ge 7){
$Body['image'] = get-item $body['image']
$splatParameters["Form"] = $Body
} else {
write-warning "Setting images is supported only with powershell version 7 or greater"
$Body.Remove('image')
}
}
if ($Body -and $splatParameters.Keys -notcontains 'Form') {
$splatParameters["Body"] = $Body | Convertto-Json
}
$script:PSDefaultParameterValues = $global:PSDefaultParameterValues
@ -72,7 +86,7 @@
try {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Invoking method $Method to URI $URi"
Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoke-WebRequest with: $($splatParameters | Out-String)"
$webResponse = Invoke-WebRequest @splatParameters
$webResponse = Invoke-RestMethod @splatParameters
}
catch {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Failed to get an answer from the server"
@ -84,27 +98,27 @@
if ($webResponse) {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Status code: $($webResponse.StatusCode)"
if ($webResponse.Content) {
Write-Verbose $webResponse.Content
if ($webResponse) {
Write-Verbose $webResponse
# API returned a Content: lets work wit it
try{
$response = ConvertFrom-Json -InputObject $webResponse.Content
if ($response.status -eq "error") {
if ($webResponse.status -eq "error") {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] An error response was received from; resolving"
# This could be handled nicely in an function such as:
# ResolveError $response -WriteError
Write-Error $($response.messages | Out-String)
Write-Error $($webResponse.messages | Out-String)
}
else {
$result = $response
if (($response) -and ($response | Get-Member -Name payload))
{
$result = $response.payload
if ($webResponse.payload){
$result = $webResponse.payload
}
elseif (($response) -and ($response | Get-Member -Name rows)) {
$result = $response.rows
elseif ($webResponse.rows) {
$result = $webResponse.rows
} else {
$result = $webResponse
}
$result

View file

@ -38,24 +38,12 @@ Cost of item being purchased.
.PARAMETER purchase_date
Date accessory was purchased
.PARAMETER order_number
Order number for this accessory.
.PARAMETER purchase_cost
Cost of item being purchased.
.PARAMETER purchase_date
Date accessory was purchased
.PARAMETER supplier_id
ID number of the supplier for this accessory
.PARAMETER location_id
ID number of the location the accessory is assigned to
.PARAMETER min_qty
Min quantity of the accessory before alert is triggered
.PARAMETER url
URL of Snipeit system, can be set using Set-SnipeitInfoeItInfo command
@ -100,6 +88,8 @@ function Set-SnipeitAccessory() {
[Nullable[System.Int32]]$supplier_id,
[Nullable[System.Int32]]$location_id,
[parameter(mandatory = $true)]
[string]$url,

View file

@ -16,7 +16,7 @@ Updates accessory on Snipe-It system
Set-SnipeitAccessory [-id] <Int32[]> [[-name] <String>] [[-qty] <Int32>] [[-category_id] <Int32>]
[[-company_id] <Int32>] [[-manufacturer_id] <Int32>] [[-model_number] <String>] [[-order_number] <String>]
[[-purchase_cost] <Single>] [[-purchase_date] <DateTime>] [[-min_amt] <Int32>] [[-supplier_id] <Int32>]
[-url] <String> [-apiKey] <String> [-WhatIf] [-Confirm] [<CommonParameters>]
[[-location_id] <Int32>] [-url] <String> [-apiKey] <String> [-WhatIf] [-Confirm] [<CommonParameters>]
```
## DESCRIPTION
@ -40,7 +40,7 @@ Parameter Sets: (All)
Aliases:
Required: True
Position: 14
Position: 15
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
@ -91,6 +91,21 @@ Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```
### -location_id
ID number of the location the accessory is assigned to
```yaml
Type: Int32
Parameter Sets: (All)
Aliases:
Required: False
Position: 13
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -manufacturer_id
ID number of the manufacturer for this accessory.
@ -235,7 +250,7 @@ Parameter Sets: (All)
Aliases:
Required: True
Position: 13
Position: 14
Default value: None
Accept pipeline input: False
Accept wildcard characters: False