commit 00305c6baf0a5440ab375d7e641406020791081d Author: Stephen Maunder Date: Thu Nov 16 11:32:17 2017 +0000 Inital Commit 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 0000000..c4b382c Binary files /dev/null and b/SnipeitPS/SnipeItPS.psd1 differ diff --git a/SnipeitPS/SnipeItPS.psm1 b/SnipeitPS/SnipeItPS.psm1 new file mode 100644 index 0000000..53f791c --- /dev/null +++ b/SnipeitPS/SnipeItPS.psm1 @@ -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 +} \ No newline at end of file