Skip to content

Commit 64902b5

Browse files
authored
Merge pull request #200 from snazy2000/develop
Release 1.9
2 parents 9bfa417 + fab7a71 commit 64902b5

File tree

87 files changed

+2575
-277
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2575
-277
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/),
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
# [v.1.9.x] - 2021-07-14
9+
10+
## Image uploads
11+
12+
## New features
13+
Support for image upload and removes. Just specify filename for -image para-
14+
meter when creating or updating item on snipe.
15+
Remove image use -image_delete parameter.
16+
17+
*Snipe It version greater than 5.1.8 is needed to support image parameters.*
18+
19+
Most of set-commands have new -RequestType parameter that defaults to Patch.
20+
If needed request method can be changed from default.
21+
22+
## New Functions
23+
Following new commands have been added to SnipeitPS:
24+
- New-Supplier
25+
- Set-Supplier
26+
- Remove-Supplier
27+
- Set-Manufacturer
28+
829

930
# [v.1.8.x] - 2021-06-17
1031

SnipeitPS/Private/Get-ParameterValue.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,13 @@ function Get-ParameterValue {
5757
}
5858
finally {}
5959
}
60+
61+
#Convert switch parameters to booleans so it converts nicely to json
62+
foreach ( $key in @($ParameterValues.Keys)) {
63+
if ($true -eq $ParameterValues[$key].IsPresent){
64+
$ParameterValues[$key]=$true;
65+
}
66+
}
67+
6068
return $ParameterValues
6169
}

SnipeitPS/Private/Invoke-SnipeitMethod.ps1

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# Body of the request
1919
[ValidateNotNullOrEmpty()]
20-
[string]$Body,
20+
[Hashtable]$Body,
2121

2222
[string] $Token,
2323

@@ -34,6 +34,8 @@
3434
Throw $exception
3535
}
3636

37+
#To support images "image" property have be handled before this
38+
3739
$_headers = @{
3840
"Authorization" = "Bearer $($token)"
3941
'Content-Type' = 'application/json; charset=utf-8'
@@ -59,17 +61,37 @@
5961
ErrorAction = 'SilentlyContinue'
6062
}
6163

62-
if ($Body) {$splatParameters["Body"] = [System.Text.Encoding]::UTF8.GetBytes($Body)}
64+
# Place holder for intended image manipulation
65+
# if and when snipe it API gets support for images
66+
if($null -ne $body -and $Body.Keys -contains 'image' ){
67+
if($PSVersionTable.PSVersion -ge '7.0'){
68+
$Body['image'] = get-item $body['image']
69+
# As multipart/form-data is always POST we need add
70+
# requested method for laravel named as '_method'
71+
$Body['_method'] = $Method
72+
$splatParameters["Method"] = 'POST'
73+
$splatParameters["Form"] = $Body
74+
} else {
75+
# use base64 encoded images for powershell version < 7
76+
Add-Type -AssemblyName "System.Web"
77+
$mimetype = [System.Web.MimeMapping]::GetMimeMapping($body['image'])
78+
$Body['image'] = 'data:@'+$mimetype+';base64,'+[Convert]::ToBase64String([IO.File]::ReadAllBytes($Body['image']))
79+
}
80+
}
81+
82+
if ($Body -and $splatParameters.Keys -notcontains 'Form') {
83+
$splatParameters["Body"] = $Body | Convertto-Json
84+
}
6385

6486
$script:PSDefaultParameterValues = $global:PSDefaultParameterValues
6587

66-
Write-Debug $Body
88+
Write-Debug "$($Body | ConvertTo-Json)"
6789

6890
# Invoke the API
6991
try {
7092
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Invoking method $Method to URI $URi"
7193
Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoke-WebRequest with: $($splatParameters | Out-String)"
72-
$webResponse = Invoke-WebRequest @splatParameters
94+
$webResponse = Invoke-RestMethod @splatParameters
7395
}
7496
catch {
7597
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Failed to get an answer from the server"
@@ -81,30 +103,43 @@
81103
if ($webResponse) {
82104
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Status code: $($webResponse.StatusCode)"
83105

84-
if ($webResponse.Content) {
85-
Write-Verbose $webResponse.Content
106+
if ($webResponse) {
107+
Write-Verbose $webResponse
86108

87109
# API returned a Content: lets work wit it
88110
try{
89-
$response = ConvertFrom-Json -InputObject $webResponse.Content
90111

91-
if ($response.status -eq "error") {
112+
if ($webResponse.status -eq "error") {
92113
Write-Verbose "[$($MyInvocation.MyCommand.Name)] An error response was received from; resolving"
93114
# This could be handled nicely in an function such as:
94115
# ResolveError $response -WriteError
95-
Write-Error $($response.messages | Out-String)
116+
Write-Error $($webResponse.messages | Out-String)
96117
}
97118
else {
98-
$result = $response
99-
if (($response) -and ($response | Get-Member -Name payload))
100-
{
101-
$result = $response.payload
119+
#update operations return payload
120+
if ($webResponse.payload){
121+
$result = $webResponse.payload
122+
}
123+
#Search operations return rows
124+
elseif ($webResponse.rows) {
125+
$result = $webResponse.rows
102126
}
103-
elseif (($response) -and ($response | Get-Member -Name rows)) {
104-
$result = $response.rows
127+
#Remove operations returns status and message
128+
elseif ($webResponse.status -eq 'success'){
129+
$result = $webResponse.payload
105130
}
131+
#get operations with id returns just one object
132+
else {
133+
$result = $webResponse
134+
}
135+
136+
Write-Verbose "Status: $($webResponse.status)"
137+
Write-Verbose "Messages: $($webResponse.messages)"
106138

107139
$result
140+
141+
142+
108143
}
109144
}
110145
catch {

SnipeitPS/Public/Get-SnipeitAsset.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ function Get-SnipeitAsset() {
221221
'Assets with component id' {$apiurl = "$url/api/v1/components/$component_id/assets"}
222222
}
223223

224-
225224
$Parameters = @{
226225
Uri = $apiurl
227226
Method = 'Get'

SnipeitPS/Public/New-SnipeitAccessory.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ ID number of the location the accessory is assigned to
5353
.PARAMETER min_amt
5454
Min quantity of the accessory before alert is triggered
5555
56+
.PARAMETER image
57+
Accessory image fileame and path
58+
5659
.PARAMETER url
5760
URL of Snipeit system, can be set using Set-SnipeitInfo command
5861
@@ -103,6 +106,9 @@ function New-SnipeitAccessory() {
103106
[ValidateRange(1, [int]::MaxValue)]
104107
[int]$location_id,
105108

109+
[ValidateScript({Test-Path $_})]
110+
[string]$image,
111+
106112
[parameter(mandatory = $true)]
107113
[string]$url,
108114

@@ -118,12 +124,10 @@ function New-SnipeitAccessory() {
118124
$values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd")
119125
}
120126

121-
$Body = $Values | ConvertTo-Json;
122-
123127
$Parameters = @{
124128
Uri = "$url/api/v1/accessories"
125129
Method = 'POST'
126-
Body = $Body
130+
Body = $Values
127131
Token = $apiKey
128132
}
129133

SnipeitPS/Public/New-SnipeitAsset.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ Optional Purchase cost of the Asset
4242
.PARAMETER rtd_location_id
4343
Optional Default location id for the asset
4444
45+
.PARAMETER image
46+
Asset image filename and path
47+
4548
.PARAMETER url
4649
URL of Snipeit system, can be set using Set-SnipeitInfo command
4750
@@ -114,6 +117,9 @@ function New-SnipeitAsset()
114117
[parameter(mandatory = $false)]
115118
[int]$rtd_location_id,
116119

120+
[ValidateScript({Test-Path $_})]
121+
[string]$image,
122+
117123
[parameter(mandatory = $true)]
118124
[string]$url,
119125

@@ -137,12 +143,10 @@ function New-SnipeitAsset()
137143
$Values += $customfields
138144
}
139145

140-
$Body = $Values | ConvertTo-Json;
141-
142146
$Parameters = @{
143147
Uri = "$url/api/v1/hardware"
144148
Method = 'Post'
145-
Body = $Body
149+
Body = $Values
146150
Token = $apiKey
147151
}
148152

SnipeitPS/Public/New-SnipeitAssetMaintenance.ps1

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,19 @@ function New-SnipeitAssetMaintenance() {
8181

8282
$Values = . Get-ParameterValue -Parameters $MyInvocation.MyCommand.Parameters -BoundParameters $PSBoundParameters
8383

84-
if ($values['start_date']) {
85-
$values['start_date'] = $values['start_date'].ToString("yyyy-MM-dd")
84+
if ($Values['start_date']) {
85+
$Values['start_date'] = $Values['start_date'].ToString("yyyy-MM-dd")
8686
}
8787

88-
if ($values['completion_date']) {
89-
$values['completion_date'] = $values['completion_date'].ToString("yyyy-MM-dd")
88+
if ($Values['completion_date']) {
89+
$Values['completion_date'] = $Values['completion_date'].ToString("yyyy-MM-dd")
9090
}
9191

92-
$Body = $Values | ConvertTo-Json;
9392

9493
$Parameters = @{
9594
Uri = "$url/api/v1/maintenances"
9695
Method = 'Post'
97-
Body = $Body
96+
Body = $Values
9897
Token = $apiKey
9998
}
10099

SnipeitPS/Public/New-SnipeitAudit.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ function New-SnipeitAudit()
4848
$Values += @{"asset_tag" = $tag}
4949
}
5050

51-
$Body = $Values | ConvertTo-Json;
52-
5351
$Parameters = @{
5452
Uri = "$url/api/v1/hardware/audit"
5553
Method = 'Post'
56-
Body = $Body
54+
Body = $Values
5755
Token = $apiKey
5856
}
5957

SnipeitPS/Public/New-SnipeitCategory.ps1

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ If switch is present, require users to confirm acceptance of assets in this cate
2020
.PARAMETER checkin_email
2121
If switch is present, send email to user on checkin/checkout
2222
23+
.PARAMETER image
24+
Category image filename and path
25+
2326
.PARAMETER url
2427
URL of Snipeit system, can be set using Set-SnipeitInfo command
2528
@@ -47,12 +50,15 @@ function New-SnipeitCategory()
4750

4851
[string]$eula_text,
4952

50-
5153
[switch]$use_default_eula,
5254

5355
[switch]$require_acceptance,
5456

5557
[switch]$checkin_email,
58+
59+
[ValidateScript({Test-Path $_})]
60+
[string]$image,
61+
5662
[parameter(mandatory = $true)]
5763
[string]$url,
5864

@@ -68,16 +74,14 @@ function New-SnipeitCategory()
6874
}
6975

7076
$Values = . Get-ParameterValue -Parameters $MyInvocation.MyCommand.Parameters -BoundParameters $PSBoundParameters
71-
72-
$Body = $Values | ConvertTo-Json;
7377
}
7478

7579
process {
7680

7781
$Parameters = @{
7882
Uri = "$url/api/v1/categories"
7983
Method = 'POST'
80-
Body = $Body
84+
Body = $Values
8185
Token = $apiKey
8286
}
8387

SnipeitPS/Public/New-SnipeitCompany.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Creates new company on Snipe-It system
88
.PARAMETER name
99
Comapany name
1010
11+
.PARAMETER image
12+
Company image filename and path
13+
1114
.PARAMETER url
1215
URL of Snipeit system, can be set using Set-SnipeitInfo command
1316
@@ -30,6 +33,9 @@ function New-SnipeitCompany()
3033
[parameter(mandatory = $true)]
3134
[string]$name,
3235

36+
[ValidateScript({Test-Path $_})]
37+
[string]$image,
38+
3339
[parameter(mandatory = $true)]
3440
[string]$url,
3541

@@ -41,12 +47,10 @@ function New-SnipeitCompany()
4147

4248
$Values = . Get-ParameterValue -Parameters $MyInvocation.MyCommand.Parameters -BoundParameters $PSBoundParameters
4349

44-
$Body = $Values | ConvertTo-Json;
45-
4650
$Parameters = @{
4751
Uri = "$url/api/v1/companies"
4852
Method = 'POST'
49-
Body = $Body
53+
Body = $Values
5054
Token = $apiKey
5155
}
5256

0 commit comments

Comments
 (0)