Inital Commit

This commit is contained in:
Stephen Maunder 2017-11-16 11:32:17 +00:00
commit 00305c6baf
11 changed files with 568 additions and 0 deletions

View file

@ -0,0 +1,112 @@
function Invoke-Method {
<#
.SYNOPSIS
Extracted invokation of the REST method to own function.
#>
[OutputType(
[PSObject]
)]
param (
# REST API to invoke
[Parameter(Mandatory = $true)]
[Uri]$URi,
# Method of the invokation
[ValidateSet("GET", "POST", "PUT", "PATCH", "DELETE")]
[string]$Method = "GET",
# Body of the request
[ValidateNotNullOrEmpty()]
[string]$Body,
[string] $Token
)
BEGIN {
# Validation of parameters
if (($Method -in ("POST", "PUT", "PATCH")) -and (!($Body))) {
$message = "The following parameters are required when using the ${Method} parameter: Body."
$exception = New-Object -TypeName System.ArgumentException -ArgumentList $message
Throw $exception
}
$_headers = @{
"Authorization" = "Bearer $($token)"
'Content-Type' = 'application/json; charset=utf-8'
"Accept" = "application/json"
}
}
Process {
# set mandatory parameters
$splatParameters = @{
Uri = $URi
Method = $Method
Headers = $_headers
UseBasicParsing = $true
ErrorAction = 'SilentlyContinue'
}
if ($Body) {$splatParameters["Body"] = [System.Text.Encoding]::UTF8.GetBytes($Body)}
$script:PSDefaultParameterValues = $global:PSDefaultParameterValues
Write-Debug $Body
# Invoke the API
try {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Invoking method $Method to URI $URi"
Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoke-WebRequest with: $($splatParameters | Out-String)"
$webResponse = Invoke-WebRequest @splatParameters
}
catch {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Failed to get an answer from the server"
$webResponse = $_.Exception.Response
}
Write-Debug "[$($MyInvocation.MyCommand.Name)] Executed WebRequest. Access $webResponse to see details"
if ($webResponse) {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Status code: $($webResponse.StatusCode)"
if ($webResponse.Content) {
Write-Verbose $webResponse.Content
# API returned a Content: lets work wit it
$response = ConvertFrom-Json -InputObject $webResponse.Content
if ($response.status -eq "error") {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] An error response was received from; resolving"
# This could be handled nicely in an function such as:
# ResolveError $response -WriteError
Write-Error $($response.messages | Out-String)
}
else {
$result = $response
if (($response) -and ($response | Get-Member -Name payload))
{
$result = $response.payload
}
elseif (($response) -and ($response | Get-Member -Name rows)) {
$result = $response.rows
}
$result
}
}
else {
# No content, although statusCode < 400
# This could be wanted behavior of the API
Write-Verbose "[$($MyInvocation.MyCommand.Name)] No content was returned from."
}
}
else {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] No Web result object was returned from. This is unusual!"
}
}
END {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function ended"
}
}

View file

@ -0,0 +1,143 @@
function Get-Asset()
{
Param(
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$result = Invoke-Method -URi "$url/api/v1/hardware" `
-Method GET `
-Token $apiKey
$result
}
function New-Asset()
{
Param(
[parameter(mandatory=$true)]
[string]$Name,
[parameter(mandatory=$true)]
[string]$Status_id,
[parameter(mandatory=$true)]
[string]$Model_id,
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey,
[string]$CPU,
[string]$memory,
[string]$OSDrive,
[string]$OS
)
$Values = @{
"name" = $Name
"status_id" = $status_id
"model_id" = $model_id
_snipeit_cpu_2 = $CPU
_snipeit_memory_gb_3 = $memory
_snipeit_os_drive_4 = $OSDrive
_snipeit_os_5 = $OS
}
$Body = $Values | ConvertTo-Json;
$result = Invoke-Method -URi "$url/api/v1/hardware" `
-Method POST `
-Body $Body `
-Token $apiKey
$result
}
function Set-Asset()
{
Param(
[parameter(mandatory=$true)]
[int]$id,
[parameter(mandatory=$true)]
[string]$Name,
[parameter(mandatory=$true)]
[string]$Status_id,
[parameter(mandatory=$true)]
[string]$Model_id,
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey,
[string]$CPU,
[string]$memory,
[string]$OSDrive,
[string]$OS
)
$Values = @{
"name" = $asset_name
"status_id" = $status_id
"model_id" = $model_id
"_snipeit_cpu_2" = $CPU
"_snipeit_memory_gb_3" = $memory
"_snipeit_os_drive_4" = $OSDrive
"_snipeit_os_5" = $OS
}
$Body = $Values | ConvertTo-Json;
$result = Invoke-Method -URi "$url/api/v1/hardware/$id" `
-Method PUT `
-Body $Body `
-Token $apiKey
$result
}
function Checkout-Asset()
{
Param(
[parameter(mandatory=$true)]
[int]$id,
[parameter(mandatory=$true)]
[int]$user_id,
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$Values = @{
"user_id" = $user_id
}
$Body = $Values | ConvertTo-Json;
$result = Invoke-Method -Uri "$url/api/v1/hardware/$id/checkout" `
-Method POST `
-Token $apiKey `
-Body $Body
return $result
}

View file

@ -0,0 +1,16 @@
function Get-Categories()
{
Param(
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$result = Invoke-Method -URi "$url/api/v1/categories" `
-Method GET `
-Token $apiKey
$result
}

View file

@ -0,0 +1,102 @@
function Get-Component()
{
Param(
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$result = Invoke-Method -URi "$url/api/v1/components" `
-Method GET `
-Token $apiKey
$result
}
function New-Component()
{
Param(
[parameter(mandatory=$true)]
[string]$name,
[parameter(mandatory=$true)]
[string]$category_id,
[parameter(mandatory=$true)]
[string]$qty,
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$Values = @{
"name" = $name
"category_id" = $category_id
"qty" = $qty
}
$Body = $Values | ConvertTo-Json;
$result = Invoke-Method -URi "$url/api/v1/components" `
-Method POST `
-Body $Body `
-Token $apiKey
$result
}
function Update-Component()
{
Param(
[parameter(mandatory=$true)]
[string]$id,
[parameter(mandatory=$true)]
[string]$qty,
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$Values = @{
"qty" = $qty
}
$Body = $Values | ConvertTo-Json;
$result = Invoke-Method -URi "$url/api/v1/components/$component_id" `
-Method Patch `
-Body $Body `
-Token $apiKey
$result
}
<#
Checkout does not exsit yet :(
function Checkout-Component($component_id, $asset_id = "")
{
$Values = @{
"asset_id" = $asset_id
}
$Body = $Values | ConvertTo-Json;
$Manufacturers = Invoke-RestMethod -Uri "http://assets.dip.co.uk/api/v1/hardware/$component_id/checkout" `
-Method Post `
-Header $headers `
-Body $Body `
-ContentType "application/json" `
-UserAgent "DI Script/0.1"
return $Manufacturers
}#>

View file

@ -0,0 +1,45 @@
function Get-Manufacturers()
{
Param(
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$result = Invoke-Method -URi "$url/api/v1/manufacturers" `
-Method GET `
-Token $apiKey
$result
}
function New-Manufacturer()
{
Param(
[parameter(mandatory=$true)]
[string]$Name,
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$Values = @{
"name" = $Name
}
#Convert Values to JSON format
$Body = $Values | ConvertTo-Json;
$result = Invoke-Method -URi "$url/api/v1/manufacturers" `
-Method POST `
-Body $Body `
-Token $apiKey
$result
}

View file

@ -0,0 +1,57 @@
function Get-Models()
{
Param(
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$result = Invoke-Method -URi "$url/api/v1/models" `
-Method GET `
-Token $apiKey
$result
}
function New-Model()
{
Param(
[parameter(mandatory=$true)]
[string]$name,
[parameter(mandatory=$true)]
[int]$category_id,
[parameter(mandatory=$true)]
[int]$manufacturer_id,
[parameter(mandatory=$true)]
[int]$fieldset_id,
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$Values = @{
name = $name
category_id = $category_id
manufacturer_id = $manufacturer_id
fieldset_id = $fieldset_id
}
$Body = $Values | ConvertTo-Json;
$result = Invoke-Method -URi "$url/api/v1/models" `
-Method POST `
-Body $Body `
-Token $apiKey
$result
}

View file

@ -0,0 +1,50 @@
function Set-Info {
[CmdletBinding()]
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')]
param (
[Uri]$url,
[String]$apiKey
)
BEGIN {
function Add-DefaultParameter {
param(
[Parameter(Mandatory = $true)]
[string]$Command,
[Parameter(Mandatory = $true)]
[string]$Parameter,
[Parameter(Mandatory = $true)]
$Value
)
PROCESS {
#Write-Verbose "[$($MyInvocation.MyCommand.Name)] Setting [$command : $parameter] = $value"
# Needs to set both global and module scope for the private functions:
# http://stackoverflow.com/questions/30427110/set-psdefaultparametersvalues-for-use-within-module-scope
$PSDefaultParameterValues["${command}:${parameter}"] = $Value
$global:PSDefaultParameterValues["${command}:${parameter}"] = $Value
}
}
$moduleCommands = Get-Command -Module SnipeitPS
}
PROCESS {
foreach ($command in $moduleCommands) {
$parameter = "url"
if ($url -and ($command.Parameters.Keys -contains $parameter)) {
Add-DefaultParameter -Command $command -Parameter $parameter -Value ($url.AbsoluteUri.TrimEnd('/'))
}
$parameter = "apiKey"
if ($apiKey -and ($command.Parameters.Keys -contains $parameter)) {
Add-DefaultParameter -Command $command -Parameter $parameter -Value $apiKey
}
}
}
}

View file

@ -0,0 +1,16 @@
function Get-Status()
{
Param(
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$result = Invoke-Method -URi "$url/api/v1/statuslabels" `
-Method GET `
-Token $apiKey
$result
}

View file

@ -0,0 +1,16 @@
function Get-Users()
{
Param(
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$result = Invoke-Method -URi "$url/api/v1/users?limit=999" `
-Method GET `
-Token $apiKey
$result
}

BIN
SnipeitPS/SnipeItPS.psd1 Normal file

Binary file not shown.

11
SnipeitPS/SnipeItPS.psm1 Normal file
View file

@ -0,0 +1,11 @@
$scriptRoot = $PSScriptRoot + '\public'
Get-ChildItem $scriptRoot *.psm1 | ForEach-Object {
Import-Module $_.FullName
}
$scriptRoot = $PSScriptRoot + '\private'
Get-ChildItem $scriptRoot *.psm1 | ForEach-Object {
Import-Module $_.FullName
}