Skip to content

Commit 75fb4ca

Browse files
authored
Merge pull request #98 from snazy2000/develop
Develop to master , first 1.1.x version
2 parents 756223f + d5d2825 commit 75fb4ca

38 files changed

+1198
-175
lines changed

SnipeitPS/Private/Get-ParameterValue.ps1

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,21 @@ function Get-ParameterValue {
2121
# }
2222
[CmdletBinding()]
2323
param(
24-
# The $MyInvocation for the caller -- DO NOT pass this (dot-source Get-ParameterValues instead)
25-
$Invocation = $MyInvocation,
26-
# The $PSBoundParameters for the caller -- DO NOT pass this (dot-source Get-ParameterValues instead)
27-
$BoundParameters = $PSBoundParameters,
24+
# Pass $MyInvocation.MyCommand.Parameters to function, powershell 7 seems to only populate variables with dot sourcing
25+
[parameter(mandatory = $true)]
26+
$Parameters
27+
,
2828

29-
[string[]]$DefaultExcludeParameter = @("id", "url", "apiKey", 'Debug', 'Verbose')
29+
[string[]]$DefaultExcludeParameter = @("id", "url", "apiKey", 'Debug', 'Verbose','RequestType','customfields')
3030
)
3131

3232
if ($MyInvocation.Line[($MyInvocation.OffsetInLine - 1)] -ne '.') {
33-
throw "Get-ParameterValues must be dot-sourced, like this: . Get-ParameterValues"
34-
}
35-
if ($PSBoundParameters.Count -gt 0) {
36-
throw "You should not pass parameters to Get-ParameterValues, just dot-source it like this: . Get-ParameterValues"
33+
throw "Get-ParameterValue must be dot-sourced, like this: . Get-ParameterValues"
3734
}
3835

36+
3937
$ParameterValues = @{}
40-
foreach ($parameter in $Invocation.MyCommand.Parameters.GetEnumerator()) {
38+
foreach ($parameter in $Parameters.GetEnumerator()) {
4139
# gm -in $parameter.Value | Out-Default
4240
try {
4341
$key = $parameter.Key
@@ -48,9 +46,6 @@ function Get-ParameterValue {
4846
}
4947
}
5048

51-
if ($BoundParameters.ContainsKey($key)) {
52-
$ParameterValues[$key] = $BoundParameters[$key]
53-
}
5449
}
5550
}
5651
finally {}

SnipeitPS/Public/Get-Accessory.ps1

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
<#
2+
.SYNOPSIS
3+
# Gets a list of Snipe-it Accessories
4+
5+
.PARAMETER search
6+
A text string to search the Accessory data
7+
8+
.PARAMETER id
9+
A id of specific Accessory
10+
11+
.PARAMETER limit
12+
Specify the number of results you wish to return. Defaults to 50. Defines batch size for -all
13+
14+
.PARAMETER offset
15+
Offset to use
16+
17+
.PARAMETER all
18+
A return all results, works with -offset and other parameters
19+
20+
.PARAMETER url
21+
URL of Snipeit system, can be set using Set-Info command
22+
23+
.PARAMETER apiKey
24+
Users API Key for Snipeit, can be set using Set-Info command
25+
26+
.EXAMPLE
27+
Get-Accessory -url "https://assets.example.com" -token "token..."
28+
29+
.EXAMPLE
30+
Get-Accessory -url "https://assets.example.com" -token "token..." | Where-Object {$_.name -eq "HP" }
31+
32+
#>
133
function Get-Accessory() {
234
Param(
335
[string]$search,
@@ -19,14 +51,16 @@ function Get-Accessory() {
1951

2052
[int]$offset,
2153

54+
[switch]$all = $false,
55+
2256
[parameter(mandatory = $true)]
2357
[string]$url,
2458

2559
[parameter(mandatory = $true)]
2660
[string]$apiKey
2761
)
2862

29-
$SearchParameter = . Get-ParameterValue
63+
$SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
3064

3165
$Parameters = @{
3266
Uri = "$url/api/v1/accessories"
@@ -35,9 +69,25 @@ function Get-Accessory() {
3569
Token = $apiKey
3670
}
3771

38-
$result = Invoke-SnipeitMethod @Parameters
39-
40-
$result
72+
if ($all) {
73+
$offstart = $(if($offset){$offset} Else {0})
74+
$callargs = $SearchParameter
75+
$callargs.Remove('all')
76+
77+
while ($true) {
78+
$callargs['offset'] = $offstart
79+
$callargs['limit'] = $limit
80+
$res=Get-Accessory @callargs
81+
$res
82+
if ($res.count -lt $limit) {
83+
break
84+
}
85+
$offstart = $offstart + $limit
86+
}
87+
} else {
88+
$result = Invoke-SnipeitMethod @Parameters
89+
$result
90+
}
4191
}
4292

4393

SnipeitPS/Public/Get-Asset.ps1

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
.PARAMETER search
66
A text string to search the assets data
77
8+
.PARAMETER id
9+
A text string to search the assets data
10+
11+
.PARAMETER asset_tag
12+
Specify exact asset tag to query
13+
14+
.PARAMETER asset_serial
15+
Specify exact asset serial to query
16+
817
.PARAMETER order_number
918
Optionally restrict asset results to this order number
1019
@@ -36,30 +45,42 @@ Specify the column name you wish to sort by
3645
Specify the order (asc or desc) you wish to order by on your sort column
3746
3847
.PARAMETER limit
39-
Specify the number of results you wish to return. Defaults to 50.
48+
Specify the number of results you wish to return. Defaults to 50. Defines batch size for -all
4049
4150
.PARAMETER offset
4251
Offset to use
4352
53+
.PARAMETER all
54+
A return all results, works with -offset and other parameters
55+
4456
.PARAMETER url
4557
URL of Snipeit system, can be set using Set-Info command
4658
4759
.PARAMETER apiKey
4860
Users API Key for Snipeit, can be set using Set-Info command
4961
5062
.EXAMPLE
51-
Get-Asset -url "https://assets.example.com" -token "token..."
63+
Get-Asset -url "https://assets.example.com"-token "token..."
5264
5365
.EXAMPLE
54-
Get-Asset -search "myMachine" -url "https://assets.example.com" -token "token..."
66+
Get-Asset -search "myMachine"-url "https://assets.example.com"-token "token..."
5567
5668
.EXAMPLE
57-
Get-Asset -search "myMachine" -url "https://assets.example.com" -token "token..."
69+
Get-Asset -search "myMachine"-url "https://assets.example.com"-token "token..."
70+
71+
.EXAMPLE
72+
Get-Asset -asset_tag "myAssetTag"-url "https://assets.example.com"-token "token..."
5873
#>
5974
function Get-Asset() {
6075
Param(
6176
[string]$search,
6277

78+
[string]$id,
79+
80+
[string]$asset_tag,
81+
82+
[string]$asset_serial,
83+
6384
[int]$order_number,
6485

6586
[int]$model_id,
@@ -89,25 +110,72 @@ function Get-Asset() {
89110

90111
[int]$offset,
91112

113+
[switch]$all = $false,
92114
[parameter(mandatory = $true)]
93115
[string]$url,
94116

95117
[parameter(mandatory = $true)]
96118
[string]$apiKey
97119
)
98120

99-
$SearchParameter = . Get-ParameterValue
121+
$SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
122+
123+
124+
$apiurl = "$url/api/v1/hardware"
125+
126+
if ($search -and ($asset_tag -or $asset_serial -or $id)) {
127+
Throw "[$($MyInvocation.MyCommand.Name)] Please specify only one of -search , -asset_tag or -asset_serial parameter"
128+
}
129+
130+
if ($id) {
131+
if ( $search -or $asset_serial -or $asset_tag) {
132+
Throw "[$($MyInvocation.MyCommand.Name)] Please specify only one of -search , -asset_tag or -asset_serial parameter"
133+
}
134+
$apiurl= "$url/api/v1/hardware/$id"
135+
}
136+
137+
if ($asset_tag) {
138+
if ( $search -or $asset_serial -or $id) {
139+
Throw "[$($MyInvocation.MyCommand.Name)] Please specify only one of -search , -asset_tag or -asset_serial parameter"
140+
}
141+
$apiurl= "$url/api/v1/hardware/bytag/$asset_tag"
142+
}
143+
144+
if ($asset_serial) {
145+
if ( $search -or $asset_tag) {
146+
Throw "[$($MyInvocation.MyCommand.Name)] Please specify only one of-search , -asset_tag or -asset_serial parameter"
147+
}
148+
$apiurl= "$url/api/v1/hardware/byserial/$asset_serial"
149+
}
100150

101151
$Parameters = @{
102-
Uri = "$url/api/v1/hardware"
152+
Uri = $apiurl
103153
Method = 'Get'
104154
GetParameters = $SearchParameter
105155
Token = $apiKey
106156
}
107157

108-
$result = Invoke-SnipeitMethod @Parameters
158+
if ($all) {
159+
$offstart = $(if ($offset){$offset} Else {0})
160+
$callargs = $SearchParameter
161+
$callargs.Remove('all')
162+
163+
while ($true) {
164+
$callargs['offset'] = $offstart
165+
$callargs['limit'] = $limit
166+
$res=Get-Asset @callargs
167+
$res
168+
if ( $res.count -lt $limit) {
169+
break
170+
}
171+
$offstart = $offstart + $limit
172+
}
173+
} else {
174+
$result = Invoke-SnipeitMethod @Parameters
175+
$result
176+
}
177+
109178

110-
$result
111179
}
112180

113181

SnipeitPS/Public/Get-AssetMaintenance.ps1

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ Specify the column name you wish to sort by
1515
Specify the order (asc or desc) you wish to order by on your sort column
1616
1717
.PARAMETER limit
18-
Specify the number of results you wish to return. Defaults to 50.
18+
Specify the number of results you wish to return. Defaults to 50. Defines batch size for -all
1919
2020
.PARAMETER offset
2121
Offset to use
2222
23+
.PARAMETER all
24+
A return all results, works with -offset and other parameters
25+
2326
.PARAMETER url
2427
URL of Snipeit system, can be set using Set-Info command
2528
@@ -48,6 +51,8 @@ function Get-AssetMaintenance() {
4851

4952
[int]$limit = 50,
5053

54+
[switch]$all = $false,
55+
5156
[int]$offset,
5257

5358
[parameter(mandatory = $true)]
@@ -57,7 +62,7 @@ function Get-AssetMaintenance() {
5762
[string]$apiKey
5863
)
5964

60-
$SearchParameter = . Get-ParameterValue
65+
$SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
6166

6267
$Parameters = @{
6368
Uri = "$url/api/v1/maintenances"
@@ -66,9 +71,25 @@ function Get-AssetMaintenance() {
6671
Token = $apiKey
6772
}
6873

69-
$result = Invoke-SnipeitMethod @Parameters
70-
71-
$result
74+
if ($all) {
75+
$offstart = $(if($offset){$offset} Else {0})
76+
$callargs = $SearchParameter
77+
$callargs.Remove('all')
78+
79+
while ($true) {
80+
$callargs['offset'] = $offstart
81+
$callargs['limit'] = $limit
82+
$res=Get-AssetMaintenance @callargs
83+
$res
84+
if ($res.count -lt $limit) {
85+
break
86+
}
87+
$offstart = $offstart + $limit
88+
}
89+
} else {
90+
$result = Invoke-SnipeitMethod @Parameters
91+
$result
92+
}
7293
}
7394

7495

0 commit comments

Comments
 (0)