Skip to content

Commit d33aa90

Browse files
authored
Merge pull request #24 from snazy2000/develop
Added more commands and changed way parameters work, also added more …
2 parents 29345d4 + 56852d8 commit d33aa90

28 files changed

+501
-233
lines changed

SnipeitPS/Private/ConvertTo-GetParameter.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
function ConvertTo-GetParameter {
2+
23
<#
34
.SYNOPSIS
45
Generate the GET parameter string for an URL from a hashtable
@@ -14,6 +15,8 @@ function ConvertTo-GetParameter {
1415
}
1516

1617
PROCESS {
18+
Add-Type -AssemblyName System.Web
19+
1720
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Making HTTP get parameter string out of a hashtable"
1821
foreach ($key in $InputObject.Keys) {
1922
$parameters += "$key=$([System.Web.HttpUtility]::UrlEncode($InputObject[$key]))&"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function Get-ParameterValue {
2+
#.Synopsis
3+
# Get the actual values of parameters which have manually set (non-null) default values or values passed in the call
4+
#.Description
5+
# Unlike $PSBoundParameters, the hashtable returned from Get-ParameterValues includes non-empty default parameter values.
6+
# NOTE: Default values that are the same as the implied values are ignored (e.g.: empty strings, zero numbers, nulls).
7+
#.Example
8+
# function Test-Parameters {
9+
# [CmdletBinding()]
10+
# param(
11+
# $Name = $Env:UserName,
12+
# $Age
13+
# )
14+
# $Parameters = . Get-ParameterValues
15+
#
16+
# # This WILL ALWAYS have a value...
17+
# Write-Host $Parameters["Name"]
18+
#
19+
# # But this will NOT always have a value...
20+
# Write-Host $PSBoundParameters["Name"]
21+
# }
22+
[CmdletBinding()]
23+
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,
28+
29+
[string[]]$DefaultExcludeParameter = @("id", "url", "apiKey", 'Debug', 'Verbose')
30+
)
31+
32+
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"
37+
}
38+
39+
$ParameterValues = @{}
40+
foreach ($parameter in $Invocation.MyCommand.Parameters.GetEnumerator()) {
41+
# gm -in $parameter.Value | Out-Default
42+
try {
43+
$key = $parameter.Key
44+
if ($key -notin $DefaultExcludeParameter) {
45+
if ($null -ne ($value = Get-Variable -Name $key -ValueOnly -ErrorAction Ignore )) {
46+
if ($value -ne ($null -as $parameter.Value.ParameterType)) {
47+
$ParameterValues[$key] = $value
48+
}
49+
}
50+
51+
if ($BoundParameters.ContainsKey($key)) {
52+
$ParameterValues[$key] = $BoundParameters[$key]
53+
}
54+
}
55+
}
56+
finally {}
57+
}
58+
return $ParameterValues
59+
}

SnipeitPS/Public/Get-Accessory.ps1

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function Get-Accessory() {
2+
Param(
3+
[string]$search,
4+
5+
[int]$company_id,
6+
7+
[int]$category_id,
8+
9+
[int]$manufacturer_id,
10+
11+
[int]$supplier_id,
12+
13+
[string]$sort = "created_at",
14+
15+
[ValidateSet("asc", "desc")]
16+
[string]$order = "desc",
17+
18+
[int]$limit = 50,
19+
20+
[int]$offset,
21+
22+
[parameter(mandatory = $true)]
23+
[string]$url,
24+
25+
[parameter(mandatory = $true)]
26+
[string]$apiKey
27+
)
28+
29+
$SearchParameter = . Get-ParameterValue
30+
31+
$Parameters = @{
32+
Uri = "$url/api/v1/accessories"
33+
Method = 'Get'
34+
GetParameters = $SearchParameter
35+
Token = $apiKey
36+
}
37+
38+
$result = Invoke-SnipeitMethod @Parameters
39+
40+
$result
41+
}
42+
43+
44+
45+
46+
47+

SnipeitPS/Public/Get-Asset.ps1

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ Users API Key for Snipeit, can be set using Set-Info command
5151
Get-Asset -url "https://assets.example.com" -token "token..."
5252
5353
.EXAMPLE
54-
Get-Asset -url "https://assets.example.com" -token "token..." | Where-Object {$_.name -eq "MyMachine" }
54+
Get-Asset -search "myMachine" -url "https://assets.example.com" -token "token..."
55+
56+
.EXAMPLE
57+
Get-Asset -search "myMachine" -url "https://assets.example.com" -token "token..."
5558
#>
5659
function Get-Asset() {
5760
Param(
@@ -69,6 +72,10 @@ function Get-Asset() {
6972

7073
[int]$location_id,
7174

75+
[int]$depreciation_id,
76+
77+
[bool]$requestable = $false,
78+
7279
[string]$status,
7380

7481
[int]$status_id,
@@ -89,23 +96,7 @@ function Get-Asset() {
8996
[string]$apiKey
9097
)
9198

92-
$SearchParameter = @{
93-
sort = $sort
94-
order = $order
95-
limit = $limit
96-
offset = $offset
97-
}
98-
99-
if ($PSBoundParameters.ContainsKey('search')) { $SearchParameter.Add("search", $search) }
100-
if ($PSBoundParameters.ContainsKey('order_number')) { $SearchParameter.Add("order_number", $order_number) }
101-
if ($PSBoundParameters.ContainsKey('model_id')) { $SearchParameter.Add("model_id", $model_id) }
102-
if ($PSBoundParameters.ContainsKey('category_id')) { $SearchParameter.Add("category_id", $category_id) }
103-
if ($PSBoundParameters.ContainsKey('manufacturer_id')) { $SearchParameter.Add("manufacturer_id", $manufacturer_id) }
104-
if ($PSBoundParameters.ContainsKey('company_id')) { $SearchParameter.Add("company_id", $company_id) }
105-
if ($PSBoundParameters.ContainsKey('location_id')) { $SearchParameter.Add("location_id", $location_id) }
106-
if ($PSBoundParameters.ContainsKey('status_id')) { $SearchParameter.Add("status_id", $order_number) }
107-
if ($PSBoundParameters.ContainsKey('status')) { $SearchParameter.Add("status", $order_number) }
108-
if ($PSBoundParameters.ContainsKey('order_number')) { $SearchParameter.Add("order_number", $order_number) }
99+
$SearchParameter = . Get-ParameterValue
109100

110101
$Parameters = @{
111102
Uri = "$url/api/v1/hardware"

SnipeitPS/Public/Get-AssetMaintenance.ps1

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,7 @@ function Get-AssetMaintenance() {
5757
[string]$apiKey
5858
)
5959

60-
$SearchParameter = @{
61-
sort = $sort
62-
order = $order
63-
limit = $limit
64-
offset = $offset
65-
}
66-
67-
if ($PSBoundParameters.ContainsKey('search')) { $SearchParameter.Add("search", $search) }
68-
if ($PSBoundParameters.ContainsKey('asset_id')) { $SearchParameter.Add("asset_id", $asset_id) }
60+
$SearchParameter = . Get-ParameterValue
6961

7062
$Parameters = @{
7163
Uri = "$url/api/v1/maintenances"

SnipeitPS/Public/Get-Category.ps1

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,7 @@ function Get-Category()
3535
[string]$apiKey
3636
)
3737

38-
$SearchParameter = @{
39-
sort = $sort
40-
order = $order
41-
limit = $limit
42-
offset = $offset
43-
}
44-
45-
if ($PSBoundParameters.ContainsKey('search')) { $SearchParameter.Add("search", $search) }
38+
$SearchParameter = . Get-ParameterValue
4639

4740
$Parameters = @{
4841
Uri = "$url/api/v1/categories"

SnipeitPS/Public/Get-Company.ps1

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,7 @@ function Get-Company()
3535
[string]$apiKey
3636
)
3737

38-
$SearchParameter = @{
39-
sort = $sort
40-
order = $order
41-
limit = $limit
42-
offset = $offset
43-
}
44-
45-
if ($PSBoundParameters.ContainsKey('search')) { $SearchParameter.Add("search", $search) }
38+
$SearchParameter = . Get-ParameterValue
4639

4740
$Parameters = @{
4841
Uri = "$url/api/v1/companies"

SnipeitPS/Public/Get-Component.ps1

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ function Get-Component() {
2424

2525
[int]$company_id,
2626

27+
[int]$location_id,
28+
2729
[ValidateSet("asc", "desc")]
2830
[string]$order = "desc",
2931

32+
[ValidateSet('id', 'name', 'min_amt', 'order_number', 'serial', 'purchase_date', 'purchase_cost', 'company', 'category', 'qty', 'location', 'image', 'created_at')]
33+
[string]$sort = "created_at",
34+
3035
[int]$limit = 50,
3136

3237
[int]$offset,
@@ -38,16 +43,7 @@ function Get-Component() {
3843
[string]$apiKey
3944
)
4045

41-
$SearchParameter = @{
42-
sort = $sort
43-
order = $order
44-
limit = $limit
45-
offset = $offset
46-
}
47-
48-
if ($PSBoundParameters.ContainsKey('search')) { $SearchParameter.Add("search", $search) }
49-
if ($PSBoundParameters.ContainsKey('category_id')) { $SearchParameter.Add("category_id", $category_id) }
50-
if ($PSBoundParameters.ContainsKey('company_id')) { $SearchParameter.Add("company_id", $company_id) }
46+
$SearchParameter = . Get-ParameterValue
5147

5248
$Parameters = @{
5349
Uri = "$url/api/v1/components"

SnipeitPS/Public/Get-Department.ps1

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,17 @@ function Get-Department()
2828

2929
[int]$offset,
3030

31+
[ValidateSet('id', 'name', 'image', 'users_count', 'created_at')]
32+
[string]$sort = "created_at",
33+
3134
[parameter(mandatory = $true)]
3235
[string]$url,
3336

3437
[parameter(mandatory = $true)]
3538
[string]$apiKey
3639
)
3740

38-
$SearchParameter = @{
39-
sort = $sort
40-
order = $order
41-
limit = $limit
42-
offset = $offset
43-
}
44-
45-
if ($PSBoundParameters.ContainsKey('search')) { $SearchParameter.Add("search", $search) }
41+
$SearchParameter = . Get-ParameterValue
4642

4743
$Parameters = @{
4844
Uri = "$url/api/v1/departments"

SnipeitPS/Public/Get-License.ps1

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,22 @@ function Get-License() {
3232

3333
[string]$license_name,
3434

35-
[string]$license_email,
35+
[mailaddress]$license_email,
3636

3737
[int]$manufacturer_id,
3838

3939
[int]$supplier_id,
4040

4141
[int]$depreciation_id,
4242

43+
[int]$category_id,
44+
4345
[ValidateSet("asc", "desc")]
4446
[string]$order = "desc",
4547

48+
[ValidateSet('id', 'name', 'purchase_cost', 'expiration_date', 'purchase_order', 'order_number', 'notes', 'purchase_date', 'serial', 'company', 'category', 'license_name', 'license_email', 'free_seats_count', 'seats', 'manufacturer', 'supplier')]
49+
[string]$sort = "created_at",
50+
4651
[int]$limit = 50,
4752

4853
[int]$offset,
@@ -54,24 +59,7 @@ function Get-License() {
5459
[string]$apiKey
5560
)
5661

57-
$SearchParameter = @{
58-
sort = $sort
59-
order = $order
60-
limit = $limit
61-
offset = $offset
62-
}
63-
64-
if ($PSBoundParameters.ContainsKey('search')) { $SearchParameter.Add("search", $search) }
65-
if ($PSBoundParameters.ContainsKey('name')) { $SearchParameter.Add("name", $name) }
66-
if ($PSBoundParameters.ContainsKey('company_id')) { $SearchParameter.Add("company_id", $company_id) }
67-
if ($PSBoundParameters.ContainsKey('product_key')) { $SearchParameter.Add("product_key", $product_key) }
68-
if ($PSBoundParameters.ContainsKey('order_number')) { $SearchParameter.Add("order_number", $order_number) }
69-
if ($PSBoundParameters.ContainsKey('purchase_order')) { $SearchParameter.Add("purchase_order", $purchase_order) }
70-
if ($PSBoundParameters.ContainsKey('license_name')) { $SearchParameter.Add("license_name", $license_name) }
71-
if ($PSBoundParameters.ContainsKey('license_email')) { $SearchParameter.Add("license_email", $license_email) }
72-
if ($PSBoundParameters.ContainsKey('manufacturer_id')) { $SearchParameter.Add("manufacturer_id", $manufacturer_id) }
73-
if ($PSBoundParameters.ContainsKey('supplier_id')) { $SearchParameter.Add("supplier_id", $supplier_id) }
74-
if ($PSBoundParameters.ContainsKey('depreciation_id')) { $SearchParameter.Add("depreciation_id", $depreciation_id) }
62+
$SearchParameter = . Get-ParameterValue
7563

7664
$Parameters = @{
7765
Uri = "$url/api/v1/licenses"

0 commit comments

Comments
 (0)