From 00305c6baf0a5440ab375d7e641406020791081d Mon Sep 17 00:00:00 2001 From: Stephen Maunder Date: Thu, 16 Nov 2017 11:32:17 +0000 Subject: [PATCH] Inital Commit --- SnipeitPS/Private/Invoke-Method.psm1 | 112 +++++++++++++++++++++ SnipeitPS/Public/Assets.psm1 | 143 +++++++++++++++++++++++++++ SnipeitPS/Public/Categories.psm1 | 16 +++ SnipeitPS/Public/Components.psm1 | 102 +++++++++++++++++++ SnipeitPS/Public/Manufacturers.psm1 | 45 +++++++++ SnipeitPS/Public/Models.psm1 | 57 +++++++++++ SnipeitPS/Public/Set-Info.psm1 | 50 ++++++++++ SnipeitPS/Public/Status.psm1 | 16 +++ SnipeitPS/Public/Users.psm1 | 16 +++ SnipeitPS/SnipeItPS.psd1 | Bin 0 -> 7874 bytes SnipeitPS/SnipeItPS.psm1 | 11 +++ 11 files changed, 568 insertions(+) create mode 100644 SnipeitPS/Private/Invoke-Method.psm1 create mode 100644 SnipeitPS/Public/Assets.psm1 create mode 100644 SnipeitPS/Public/Categories.psm1 create mode 100644 SnipeitPS/Public/Components.psm1 create mode 100644 SnipeitPS/Public/Manufacturers.psm1 create mode 100644 SnipeitPS/Public/Models.psm1 create mode 100644 SnipeitPS/Public/Set-Info.psm1 create mode 100644 SnipeitPS/Public/Status.psm1 create mode 100644 SnipeitPS/Public/Users.psm1 create mode 100644 SnipeitPS/SnipeItPS.psd1 create mode 100644 SnipeitPS/SnipeItPS.psm1 diff --git a/SnipeitPS/Private/Invoke-Method.psm1 b/SnipeitPS/Private/Invoke-Method.psm1 new file mode 100644 index 0000000..ab49489 --- /dev/null +++ b/SnipeitPS/Private/Invoke-Method.psm1 @@ -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" + } +} \ No newline at end of file diff --git a/SnipeitPS/Public/Assets.psm1 b/SnipeitPS/Public/Assets.psm1 new file mode 100644 index 0000000..eccc456 --- /dev/null +++ b/SnipeitPS/Public/Assets.psm1 @@ -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 +} diff --git a/SnipeitPS/Public/Categories.psm1 b/SnipeitPS/Public/Categories.psm1 new file mode 100644 index 0000000..31fc78a --- /dev/null +++ b/SnipeitPS/Public/Categories.psm1 @@ -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 +} diff --git a/SnipeitPS/Public/Components.psm1 b/SnipeitPS/Public/Components.psm1 new file mode 100644 index 0000000..1b2e785 --- /dev/null +++ b/SnipeitPS/Public/Components.psm1 @@ -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 +}#> \ No newline at end of file diff --git a/SnipeitPS/Public/Manufacturers.psm1 b/SnipeitPS/Public/Manufacturers.psm1 new file mode 100644 index 0000000..410c5bd --- /dev/null +++ b/SnipeitPS/Public/Manufacturers.psm1 @@ -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 +} \ No newline at end of file diff --git a/SnipeitPS/Public/Models.psm1 b/SnipeitPS/Public/Models.psm1 new file mode 100644 index 0000000..b7e39b7 --- /dev/null +++ b/SnipeitPS/Public/Models.psm1 @@ -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 +} \ No newline at end of file diff --git a/SnipeitPS/Public/Set-Info.psm1 b/SnipeitPS/Public/Set-Info.psm1 new file mode 100644 index 0000000..3307a01 --- /dev/null +++ b/SnipeitPS/Public/Set-Info.psm1 @@ -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 + } + } + } +} \ No newline at end of file diff --git a/SnipeitPS/Public/Status.psm1 b/SnipeitPS/Public/Status.psm1 new file mode 100644 index 0000000..3a4887a --- /dev/null +++ b/SnipeitPS/Public/Status.psm1 @@ -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 +} \ No newline at end of file diff --git a/SnipeitPS/Public/Users.psm1 b/SnipeitPS/Public/Users.psm1 new file mode 100644 index 0000000..78a5841 --- /dev/null +++ b/SnipeitPS/Public/Users.psm1 @@ -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 +} diff --git a/SnipeitPS/SnipeItPS.psd1 b/SnipeitPS/SnipeItPS.psd1 new file mode 100644 index 0000000000000000000000000000000000000000..c4b382c6c74e6d954799e430b31233f0eca57e09 GIT binary patch literal 7874 zcmeI1YfoE85Qg`2rT&K_@c~j5Qj)Z(Dpd-CkRXvX;*$0&wlQ^sFEtPnMfvM(-)F~@ z-Ss&K3=L{kRhE5jb7p7Wxv&5HV<~(Yz6eX93NOMq^uk3r4ad3~hNCbFgV5L2Svc37 zzMfCxlbmij>_@tZ=ypW+O4rLXp3eE1o{jXKis!kGJ#p!T_u&T}_v4*O_%+fX#gSwf zi3WP(a221iXyLOSz76;FyRP2{;Ts(vXK%ydN%#=&IJf=K)e3_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