SnipeitPS/SnipeitPS/Public/Get-SnipeitLocation.ps1

106 lines
2.5 KiB
PowerShell
Raw Normal View History

<#
.SYNOPSIS
2021-05-19 15:51:49 +03:00
Gets a list of Snipe-it Locations
2021-01-17 06:28:08 +02:00
.PARAMETER search
A text string to search the Locations data
.PARAMETER id
A id of specific Location
.PARAMETER limit
Specify the number of results you wish to return. Defaults to 50. Defines batch size for -all
.PARAMETER offset
Offset to use
.PARAMETER all
A return all results, works with -offset and other parameters
.PARAMETER url
2021-06-08 20:23:32 +03:00
URL of Snipeit system, can be set using Set-SnipeitInfoeItInfo command
.PARAMETER apiKey
2021-06-08 20:23:32 +03:00
Users API Key for Snipeit, can be set using Set-SnipeitInfo command
.EXAMPLE
2021-06-08 20:23:32 +03:00
Get-SnipeitLocation -search Location1
.EXAMPLE
2021-06-08 20:23:32 +03:00
Get-SnipeitLocation -id 3
#>
function Get-SnipeitLocation()
{
2021-06-09 23:46:21 +03:00
[CmdletBinding(DefaultParameterSetName = 'Search')]
Param(
2021-06-07 21:16:51 +03:00
[parameter(ParameterSetName='Search')]
[string]$search,
2021-05-17 10:07:17 +03:00
2021-06-07 21:16:51 +03:00
[parameter(ParameterSetName='Get with ID')]
[int]$id,
2021-06-07 21:16:51 +03:00
[parameter(ParameterSetName='Search')]
[ValidateSet("asc", "desc")]
[string]$order = "desc",
2021-06-07 21:16:51 +03:00
[parameter(ParameterSetName='Search')]
[int]$limit = 50,
2021-06-07 21:16:51 +03:00
[parameter(ParameterSetName='Search')]
[int]$offset,
2021-06-07 21:16:51 +03:00
[parameter(ParameterSetName='Search')]
[switch]$all = $false,
2021-07-31 22:25:13 +03:00
[parameter(mandatory = $false)]
[string]$url,
2021-07-31 22:25:13 +03:00
[parameter(mandatory = $false)]
[string]$apiKey
)
2021-05-23 19:23:24 +03:00
2021-06-08 20:23:32 +03:00
Test-SnipeitAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name
2021-06-07 21:27:10 +03:00
$SearchParameter = . Get-ParameterValue -Parameters $MyInvocation.MyCommand.Parameters -BoundParameters $PSBoundParameters
2021-01-17 06:28:08 +02:00
$apiurl = "$url/api/v1/locations"
if ($search -and $id ) {
Throw "[$($MyInvocation.MyCommand.Name)] Please specify only -search or -id parameter , not both "
}
2021-05-17 10:07:17 +03:00
2021-01-17 06:28:08 +02:00
if ($id) {
2021-05-17 10:07:17 +03:00
$apiurl= "$url/api/v1/locations/$id"
2021-01-17 06:28:08 +02:00
}
$Parameters = @{
2021-01-17 06:28:08 +02:00
Uri = $apiurl
Method = 'Get'
Token = $apiKey
GetParameters = $SearchParameter
}
if ($all) {
$offstart = $(if($offset){$offset} Else {0})
$callargs = $SearchParameter
$callargs.Remove('all')
while ($true) {
$callargs['offset'] = $offstart
2021-05-17 10:07:17 +03:00
$callargs['limit'] = $limit
$res=Get-SnipeitLocation @callargs
$res
if ($res.count -lt $limit) {
break
}
$offstart = $offstart + $limit
}
} else {
$result = Invoke-SnipeitMethod @Parameters
$result
}
}