Pipeline support for Set functions

This commit is contained in:
Petri Asikainen 2021-06-06 07:56:11 +03:00
parent 7ba3de55bb
commit 0cbfbab86e
12 changed files with 259 additions and 195 deletions

View file

@ -79,10 +79,10 @@ function Get-SnipeItAsset() {
[int]$id, [int]$id,
[string]$asset_tag, [string]$asset_tag,
[Alias('asset_serial')]
[string]$serial,
[string]$asset_serial, [string]$order_number,
[int]$order_number,
[int]$model_id, [int]$model_id,

View file

@ -44,7 +44,7 @@ function Get-SnipeItLicense() {
[string]$product_key, [string]$product_key,
[int]$order_number, [string]$order_number,
[string]$purchase_order, [string]$purchase_order,

View file

@ -8,6 +8,9 @@ Creates new accessory on Snipe-It system
.PARAMETER name .PARAMETER name
Accessory name Accessory name
.PARAMETER notes
Notes about the accessory
.PARAMETER qty .PARAMETER qty
Quantity of the accessory you have Quantity of the accessory you have

View file

@ -6,7 +6,10 @@ Updates accessory on Snipe-It system
Updates accessory on Snipe-It system Updates accessory on Snipe-It system
.PARAMETER name .PARAMETER name
ID number of Accessory on Snipe-It system ID number of Accessory or array of IDs on Snipe-It system
.PARAMETER notes
Notes about the accessory
.PARAMETER qty .PARAMETER qty
Quantity of the accessory you have Quantity of the accessory you have
@ -64,8 +67,8 @@ function Set-SnipeItAccessory() {
)] )]
Param( Param(
[parameter(mandatory = $true)] [parameter(mandatory = $true,ValueFromPipelineByPropertyName)]
[int]$id, [int[]]$id,
[ValidateLength(3, 255)] [ValidateLength(3, 255)]
[string]$name, [string]$name,
@ -81,6 +84,7 @@ function Set-SnipeItAccessory() {
[ValidateRange(1, [int]::MaxValue)] [ValidateRange(1, [int]::MaxValue)]
[int]$manufacturer_id, [int]$manufacturer_id,
[AllowEmptyString]
[string]$order_number, [string]$order_number,
[float]$purchase_cost, [float]$purchase_cost,
@ -99,27 +103,33 @@ function Set-SnipeItAccessory() {
[string]$apiKey [string]$apiKey
) )
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name begin {
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name
$Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
if ($values['purchase_date']) { if ($values['purchase_date']) {
$values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd") $values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd")
}
$Body = $Values | ConvertTo-Json;
}
process {
foreach($accessory_id in $id){
$Parameters = @{
Uri = "$url/api/v1/accessories/$accessory_id"
Method = 'Patch'
Body = $Body
Token = $apiKey
}
If ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
$result = Invoke-SnipeitMethod @Parameters
}
$result
}
} }
$Body = $Values | ConvertTo-Json;
$Parameters = @{
Uri = "$url/api/v1/accessories/$id"
Method = 'POST'
Body = $Body
Token = $apiKey
}
If ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
$result = Invoke-SnipeitMethod @Parameters
}
$result
} }

View file

@ -5,7 +5,7 @@
Checkout accessory to user Checkout accessory to user
.PARAMETER id .PARAMETER id
Unique ID For accessory to checkout Unique ID For accessory or array of IDs to checkout
.PARAMETER assigned_id .PARAMETER assigned_id
Id of target user Id of target user
@ -30,8 +30,8 @@ function Set-SnipeItAccessoryOwner()
)] )]
Param( Param(
[parameter(mandatory = $true)] [parameter(mandatory = $true,ValueFromPipelineByPropertyName)]
[int]$id, [int[]]$id,
[parameter(mandatory = $true)] [parameter(mandatory = $true)]
[int]$assigned_to, [int]$assigned_to,
@ -44,22 +44,27 @@ function Set-SnipeItAccessoryOwner()
[parameter(mandatory = $true)] [parameter(mandatory = $true)]
[string]$apiKey [string]$apiKey
) )
begin{
$Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters $Body = $Values | ConvertTo-Json;
$Body = $Values | ConvertTo-Json;
$Parameters = @{
Uri = "$url/api/v1/accessories/$id/checkout"
Method = 'POST'
Body = $Body
Token = $apiKey
} }
If ($PSCmdlet.ShouldProcess("ShouldProcess?")) process {
{ foreach($accessory_id in $id){
$result = Invoke-SnipeitMethod @Parameters $Parameters = @{
} Uri = "$url/api/v1/accessories/$accessory_id/checkout"
Method = 'POST'
Body = $Body
Token = $apiKey
}
return $result If ($PSCmdlet.ShouldProcess("ShouldProcess?"))
{
$result = Invoke-SnipeitMethod @Parameters
}
return $result
}
}
} }

View file

@ -6,7 +6,7 @@
Long description Long description
.PARAMETER id .PARAMETER id
ID of the Asset ID of the Asset or array of IDs
.PARAMETER Name .PARAMETER Name
Asset name Asset name
@ -70,6 +70,9 @@
.EXAMPLE .EXAMPLE
Set-SnipeItAsset -id 1 -status_id 1 -model_id 1 -name "Machine1" -CustomValues = @{ "_snipeit_os_5 = "Windows 10 Pro" } Set-SnipeItAsset -id 1 -status_id 1 -model_id 1 -name "Machine1" -CustomValues = @{ "_snipeit_os_5 = "Windows 10 Pro" }
.EXAMPLE
Get-SnipeItAsset -serial 12345678 | Set-SnipeItAsset -notes 'Just updated'
#> #>
function Set-SnipeItAsset() function Set-SnipeItAsset()
@ -80,8 +83,8 @@ function Set-SnipeItAsset()
)] )]
Param( Param(
[parameter(mandatory = $true)] [parameter(mandatory = $true,ValueFromPipelineByPropertyName)]
[int]$id, [int[]]$id,
[string]$name, [string]$name,
@ -124,33 +127,38 @@ function Set-SnipeItAsset()
[hashtable] $customfields [hashtable] $customfields
) )
begin{
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters if ($values['purchase_date']) {
$values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd")
}
if ($values['purchase_date']) { if ($customfields)
$values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd") {
$Values += $customfields
}
$Body = $Values | ConvertTo-Json;
} }
if ($customfields) process {
{ foreach($asset_id in $id){
$Values += $customfields $Parameters = @{
Uri = "$url/api/v1/hardware/$asset_id"
Method = $RequestType
Body = $Body
Token = $apiKey
}
If ($PSCmdlet.ShouldProcess("ShouldProcess?"))
{
$result = Invoke-SnipeitMethod @Parameters
}
$result
}
} }
$Body = $Values | ConvertTo-Json;
$Parameters = @{
Uri = "$url/api/v1/hardware/$id"
Method = $RequestType
Body = $Body
Token = $apiKey
}
If ($PSCmdlet.ShouldProcess("ShouldProcess?"))
{
$result = Invoke-SnipeitMethod @Parameters
}
$result
} }

View file

@ -5,7 +5,7 @@
Checkout asset to user/localtion/asset Checkout asset to user/localtion/asset
.PARAMETER ID .PARAMETER ID
Unique ID For asset to checkout Unique IDs For assets to checkout
.PARAMETER assigned_id .PARAMETER assigned_id
Id of target user , location or asset Id of target user , location or asset
@ -41,8 +41,8 @@ function Set-SnipeItAssetOwner()
)] )]
Param( Param(
[parameter(mandatory = $true)] [parameter(mandatory = $true,ValueFromPipelineByPropertyName)]
[int]$id, [int[]]$id,
[parameter(mandatory = $true)] [parameter(mandatory = $true)]
[int]$assigned_id, [int]$assigned_id,
@ -65,41 +65,48 @@ function Set-SnipeItAssetOwner()
[string]$apiKey [string]$apiKey
) )
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name begin{
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name
$Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
if ($Values['expected_checkin']) {
$Values['expected_checkin'] = $values['expected_checkin'].ToString("yyyy-MM-dd")
}
if ($Values['checkout_at']) {
$Values['checkout_at'] = $values['checkout_at'].ToString("yyyy-MM-dd")
}
switch ($checkout_to_type)
{
'location' { $Values += @{ "assigned_location" = $assigned_id } }
'user' { $Values += @{ "assigned_user" = $assigned_id } }
'asset' { $Values += @{ "assigned_asset" = $assigned_id } }
}
#This can be removed now
if($Values.ContainsKey('assigned_id')){$Values.Remove('assigned_id')}
$Body = $Values | ConvertTo-Json;
if ($Values['expected_checkin']) {
$Values['expected_checkin'] = $values['expected_checkin'].ToString("yyyy-MM-dd")
} }
if ($Values['checkout_at']) { process{
$Values['checkout_at'] = $values['checkout_at'].ToString("yyyy-MM-dd") foreach($asset_id in $id){
$Parameters = @{
Uri = "$url/api/v1/hardware/$asset_id/checkout"
Method = 'POST'
Body = $Body
Token = $apiKey
}
If ($PSCmdlet.ShouldProcess("ShouldProcess?"))
{
$result = Invoke-SnipeitMethod @Parameters
}
return $result
}
} }
switch ($checkout_to_type)
{
'location' { $Values += @{ "assigned_location" = $assigned_id } }
'user' { $Values += @{ "assigned_user" = $assigned_id } }
'asset' { $Values += @{ "assigned_asset" = $assigned_id } }
}
#This can be removed now
if($Values.ContainsKey('assigned_id')){$Values.Remove('assigned_id')}
$Body = $Values | ConvertTo-Json;
$Parameters = @{
Uri = "$url/api/v1/hardware/$id/checkout"
Method = 'POST'
Body = $Body
Token = $apiKey
}
If ($PSCmdlet.ShouldProcess("ShouldProcess?"))
{
$result = Invoke-SnipeitMethod @Parameters
}
return $result
} }

View file

@ -6,7 +6,7 @@
Updates licence on Snipe-It system Updates licence on Snipe-It system
.PARAMETER id .PARAMETER id
ID number of licence ID number of license or array of license IDs
.PARAMETER name .PARAMETER name
Name of license Name of license
@ -77,8 +77,8 @@ function Set-SnipeItLicense() {
)] )]
Param( Param(
[parameter(mandatory = $true)] [parameter(mandatory = $true, ValueFromPipelineByPropertyName)]
[int]$id, [int[]]$id,
[ValidateLength(3, 255)] [ValidateLength(3, 255)]
[string]$name, [string]$name,
@ -128,36 +128,39 @@ function Set-SnipeItLicense() {
[parameter(mandatory = $true)] [parameter(mandatory = $true)]
[string]$apiKey [string]$apiKey
) )
begin{
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters if ($values['expiration_date']) {
$values['expiration_date'] = $values['expiration_date'].ToString("yyyy-MM-dd")
}
if ($values['expiration_date']) { if ($values['purchase_date']) {
$values['expiration_date'] = $values['expiration_date'].ToString("yyyy-MM-dd") $values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd")
}
if ($values['termination_date']) {
$values['termination_date'] = $values['termination_date'].ToString("yyyy-MM-dd")
}
$Body = $Values | ConvertTo-Json;
} }
process {
foreach($license_id in $id){
$Parameters = @{
Uri = "$url/api/v1/licenses/$license_id"
Method = 'PUT'
Body = $Body
Token = $apiKey
}
if ($values['purchase_date']) { If ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
$values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd") $result = Invoke-SnipeitMethod @Parameters
}
$result
}
} }
if ($values['termination_date']) {
$values['termination_date'] = $values['termination_date'].ToString("yyyy-MM-dd")
}
$Body = $Values | ConvertTo-Json;
$Parameters = @{
Uri = "$url/api/v1/licenses/$id"
Method = 'PUT'
Body = $Body
Token = $apiKey
}
If ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
$result = Invoke-SnipeitMethod @Parameters
}
$result
} }

View file

@ -5,7 +5,7 @@
Checkout specific license seat to user, asset or both Checkout specific license seat to user, asset or both
.PARAMETER ID .PARAMETER ID
Unique ID For asset to checkout Unique ID For license to checkout or array of IDs
.PARAMETER assigned_to .PARAMETER assigned_to
Id of target user Id of target user
@ -40,7 +40,7 @@ function Set-SnipeItLicenseSeat()
Param( Param(
[parameter(mandatory = $true)] [parameter(mandatory = $true)]
[int]$id, [int[]]$id,
[parameter(mandatory = $true)] [parameter(mandatory = $true)]
[int]$seat_id, [int]$seat_id,
@ -58,21 +58,27 @@ function Set-SnipeItLicenseSeat()
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters begin{
$Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Body = $Values | ConvertTo-Json; $Body = $Values | ConvertTo-Json;
$Parameters = @{
Uri = "$url/api/v1/licenses/$id/seats/$seat_id"
Method = 'Patch'
Body = $Body
Token = $apiKey
} }
If ($PSCmdlet.ShouldProcess("ShouldProcess?")) process{
{ foreach($license_id in $id) {
$result = Invoke-SnipeitMethod @Parameters $Parameters = @{
} Uri = "$url/api/v1/licenses/$license_id/seats/$seat_id"
Method = 'Patch'
Body = $Body
Token = $apiKey
}
return $result If ($PSCmdlet.ShouldProcess("ShouldProcess?"))
{
$result = Invoke-SnipeitMethod @Parameters
}
return $result
}
}
} }

View file

@ -5,6 +5,9 @@
.DESCRIPTION .DESCRIPTION
Long description Long description
.PARAMETER id
ID number of location or array or IDs
.PARAMETER name .PARAMETER name
Name of Location Name of Location
@ -59,8 +62,8 @@ function Set-SnipeitLocation() {
)] )]
Param( Param(
[parameter(mandatory = $true)] [parameter(mandatory = $true,ValueFromPipelineByPropertyName)]
[int]$id, [int[]]$id,
[ValidateLength(3, 255)] [ValidateLength(3, 255)]
[string]$name, [string]$name,
@ -92,23 +95,29 @@ function Set-SnipeitLocation() {
[string]$apiKey [string]$apiKey
) )
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name begin{
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name
$Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Body = $Values | ConvertTo-Json; $Body = $Values | ConvertTo-Json;
$Parameters = @{
Uri = "$url/api/v1/locations/$id"
Method = 'PUT'
Body = $Body
Token = $apiKey
} }
If ($PSCmdlet.ShouldProcess("ShouldProcess?")) { process{
$result = Invoke-SnipeitMethod @Parameters foreach ($location_id in $id) {
} $Parameters = @{
Uri = "$url/api/v1/locations/$location_id"
Method = 'PUT'
Body = $Body
Token = $apiKey
}
$result If ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
$result = Invoke-SnipeitMethod @Parameters
}
$result
}
}
} }

View file

@ -6,7 +6,7 @@
Updates Model on Snipe-it asset system Updates Model on Snipe-it asset system
.PARAMETER id .PARAMETER id
ID number of the Asset Model ID number of the Asset Model or array of IDs
.PARAMETER name .PARAMETER name
Name of the Asset Model Name of the Asset Model
@ -40,8 +40,8 @@ function Set-SnipeItModel() {
)] )]
Param( Param(
[parameter(mandatory = $true)] [parameter(mandatory = $true,ValueFromPipelineByPropertyName)]
[int]$id, [int[]]$id,
[ValidateLength(1, 255)] [ValidateLength(1, 255)]
[string]$name, [string]$name,
@ -66,22 +66,27 @@ function Set-SnipeItModel() {
[string]$apiKey [string]$apiKey
) )
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name begin {
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name
$Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Body = $Values | ConvertTo-Json; $Body = $Values | ConvertTo-Json;
$Parameters = @{
Uri = "$url/api/v1/models/$id"
Method = 'put'
Body = $Body
Token = $apiKey
} }
process {
foreach ($model_id in $id) {
$Parameters = @{
Uri = "$url/api/v1/models/$model_id"
Method = 'put'
Body = $Body
Token = $apiKey
}
If ($PSCmdlet.ShouldProcess("ShouldProcess?")) { If ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
$result = Invoke-SnipeitMethod @Parameters $result = Invoke-SnipeitMethod @Parameters
}
$result
}
} }
$result
} }

View file

@ -2,6 +2,9 @@
.SYNOPSIS .SYNOPSIS
Creates a new user Creates a new user
.PARAMETER id
ID number of Snipe--It user or array of IDs
.DESCRIPTION .DESCRIPTION
Creates a new user to Snipe-IT system Creates a new user to Snipe-IT system
@ -68,8 +71,8 @@ function Set-SnipeItUser() {
)] )]
Param( Param(
[parameter(mandatory = $true)] [parameter(mandatory = $true,ValueFromPipelineByPropertyName)]
[int]$id, [int[]]$id,
[string]$first_name, [string]$first_name,
@ -103,23 +106,28 @@ function Set-SnipeItUser() {
[parameter(mandatory = $true)] [parameter(mandatory = $true)]
[string]$apiKey [string]$apiKey
) )
begin{
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name
Test-SnipeItAlias -invocationName $MyInvocation.InvocationName -commandName $MyInvocation.MyCommand.Name $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters $Body = $Values | ConvertTo-Json;
$Body = $Values | ConvertTo-Json;
$Parameters = @{
Uri = "$url/api/v1/users/$id"
Method = 'PATCH'
Body = $Body
Token = $apiKey
} }
If ($PSCmdlet.ShouldProcess("ShouldProcess?")) { process{
$result = Invoke-SnipeitMethod @Parameters foreach($user_id in $id) {
} $Parameters = @{
Uri = "$url/api/v1/users/$user_id"
Method = 'PATCH'
Body = $Body
Token = $apiKey
}
$result If ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
$result = Invoke-SnipeitMethod @Parameters
}
$result
}
}
} }