From bd9db2d9be67535433a4e2fa67793b2fc1bdabe7 Mon Sep 17 00:00:00 2001 From: Stephen Maunder Date: Sat, 17 Mar 2018 21:23:11 +0000 Subject: [PATCH] Added new Ability to create users and update them as well as add new locations --- SnipeitPS/Public/Get-Company.ps1 | 38 ++++++++ SnipeitPS/Public/New-Location.ps1 | 80 +++++++++++++++++ SnipeitPS/Public/New-User.ps1 | 143 ++++++++++++++++++++++++++++++ SnipeitPS/Public/Set-User.ps1 | 131 +++++++++++++++++++++++++++ SnipeitPS/SnipeItPS.psd1 | Bin 8658 -> 8992 bytes 5 files changed, 392 insertions(+) create mode 100644 SnipeitPS/Public/Get-Company.ps1 create mode 100644 SnipeitPS/Public/New-Location.ps1 create mode 100644 SnipeitPS/Public/New-User.ps1 create mode 100644 SnipeitPS/Public/Set-User.ps1 diff --git a/SnipeitPS/Public/Get-Company.ps1 b/SnipeitPS/Public/Get-Company.ps1 new file mode 100644 index 0000000..df66964 --- /dev/null +++ b/SnipeitPS/Public/Get-Company.ps1 @@ -0,0 +1,38 @@ +<# +.SYNOPSIS +# Gets a list of Snipe-it Companies + +.PARAMETER url +URL of Snipeit system, can be set using Set-Info command + +.PARAMETER apiKey +Users API Key for Snipeit, can be set using Set-Info command + +.EXAMPLE +Get-Company -url "https://assets.example.com" -token "token..." + +.EXAMPLE +Get-Company -url "https://assets.example.com" -token "token..." | Where-Object {$_.name -eq "Company1" } + +#> + +function Get-Company() +{ + Param( + [parameter(mandatory=$true)] + [string]$url, + + [parameter(mandatory=$true)] + [string]$apiKey + ) + + $Parameters = @{ + Uri = "$url/api/v1/companies" + Method = 'Get' + Token = $apiKey + } + + $result = Invoke-SnipeitMethod @Parameters + + $result +} diff --git a/SnipeitPS/Public/New-Location.ps1 b/SnipeitPS/Public/New-Location.ps1 new file mode 100644 index 0000000..d6cce02 --- /dev/null +++ b/SnipeitPS/Public/New-Location.ps1 @@ -0,0 +1,80 @@ +<# + .SYNOPSIS + Add a new Model to Snipe-it asset system + + .DESCRIPTION + Long description + + .PARAMETER name + Name of the Asset Model + + .PARAMETER category_id + Category ID that the asset belongs to this can be got using Get-Category + + .PARAMETER manufacturer_id + Manufacturer ID that the asset belongs to this can be got using Get-Manufacturer + + .PARAMETER fieldset_id + Fieldset ID that the asset uses (Custom fields) + + .PARAMETER url + URL of Snipeit system, can be set using Set-Info command + + .PARAMETER apiKey + Users API Key for Snipeit, can be set using Set-Info command + + .EXAMPLE + New-Model -name "DL380" -manufacturer_id 2 -fieldset_id 2 -category_id 1 +#> + +function New-Location() { + [CmdletBinding( + SupportsShouldProcess = $true, + ConfirmImpact = "Low" + )] + + Param( + [parameter(mandatory = $true)] + [string]$name, + + [string]$address, + + [string]$address2, + + [string]$state, + + [string]$country, + + [string]$zip, + + [parameter(mandatory = $true)] + [string]$url, + + [parameter(mandatory = $true)] + [string]$apiKey + ) + + $Values = @{ + name = $name + address = $address + address2 = $address2 + state = $state + country = $country + zip = $zip + } + + $Body = $Values | ConvertTo-Json; + + $Parameters = @{ + Uri = "$url/api/v1/locations" + Method = 'post' + Body = $Body + Token = $apiKey + } + + If ($PSCmdlet.ShouldProcess("ShouldProcess?")) { + $result = Invoke-SnipeitMethod @Parameters + } + + $result +} diff --git a/SnipeitPS/Public/New-User.ps1 b/SnipeitPS/Public/New-User.ps1 new file mode 100644 index 0000000..6801075 --- /dev/null +++ b/SnipeitPS/Public/New-User.ps1 @@ -0,0 +1,143 @@ +<# + .SYNOPSIS + Short description + + .DESCRIPTION + Long description + + .PARAMETER firstName + Parameter description + + .PARAMETER lastName + Parameter description + + .PARAMETER userName + Parameter description + + .PARAMETER jobTitle + Parameter description + + .PARAMETER email + Parameter description + + .PARAMETER phone + Parameter description + + .PARAMETER company_id + Parameter description + + .PARAMETER location_id + Parameter description + + .PARAMETER department_id + Parameter description + + .PARAMETER manager_id + Parameter description + + .PARAMETER employee_num + Parameter description + + .PARAMETER ldap_user + Parameter description + + .PARAMETER url + Parameter description + + .PARAMETER apiKey + Parameter description + + .EXAMPLE + An example + + .NOTES + General notes + #> +function New-User() { + + [CmdletBinding( + SupportsShouldProcess = $true, + ConfirmImpact = "Low" + )] + + Param( + [parameter(mandatory = $true)] + [string]$firstName, + + [parameter(mandatory = $true)] + [string]$lastName, + + [parameter(mandatory = $true)] + [string]$userName, + + [string]$jobTitle, + + [string]$email, + + [string]$phone, + + [int]$company_id, + + [int]$location_id, + + [int]$department_id, + + [int]$manager_id, + + [string]$employee_num, + + [bool]$ldap_user = $false, + + [parameter(mandatory = $true)] + [string]$url, + + [parameter(mandatory = $true)] + [string]$apiKey + ) + + $Values = @{ + first_name = $firstName + last_name = $lastName + username = $userName + + email = $email + phone = $phone + company_id = $company_id + location_id = $location_id + department_id = $department_id + manager_id = $manager_id + jobtitle = $jobTitle + employee_num = $employee_num + notes = "Imported using SnipeitPS Script" + activated = 1 + } + + if ($ldap_user -eq $false) { + $ldap = @{ + password = $password + ldap_import = 0 + } + $Values += $ldap + } + else { + $ldap = @{ + ldap_import = 1 + } + $Values += $ldap + } + + $Body = $Values | ConvertTo-Json; + + $Parameters = @{ + Uri = "$url/api/v1/users" + Method = 'post' + Body = $Body + Token = $apiKey + } + + If ($PSCmdlet.ShouldProcess("ShouldProcess?")) { + $result = Invoke-SnipeitMethod @Parameters + } + + $result +} diff --git a/SnipeitPS/Public/Set-User.ps1 b/SnipeitPS/Public/Set-User.ps1 new file mode 100644 index 0000000..f305e74 --- /dev/null +++ b/SnipeitPS/Public/Set-User.ps1 @@ -0,0 +1,131 @@ +<# + .SYNOPSIS + Short description + + .DESCRIPTION + Long description + + .PARAMETER id + Parameter description + + .PARAMETER first_name + Parameter description + + .PARAMETER last_name + Parameter description + + .PARAMETER userName + Parameter description + + .PARAMETER jobTitle + Parameter description + + .PARAMETER email + Parameter description + + .PARAMETER phone + Parameter description + + .PARAMETER company_id + Parameter description + + .PARAMETER location_id + Parameter description + + .PARAMETER department_id + Parameter description + + .PARAMETER manager_id + Parameter description + + .PARAMETER employee_num + Parameter description + + .PARAMETER activated + Parameter description + + .PARAMETER notes + Parameter description + + .PARAMETER url + Parameter description + + .PARAMETER apiKey + Parameter description + + .EXAMPLE + An example + + .NOTES + General notes + #> +function Set-User() { + + [CmdletBinding( + SupportsShouldProcess = $true, + ConfirmImpact = "Medium" + )] + + Param( + [parameter(mandatory = $true)] + [int]$id, + + [string]$first_name, + + [string]$last_name, + + [string]$userName, + + [string]$jobTitle, + + [string]$email, + + [string]$phone, + + [int]$company_id, + + [int]$location_id, + + [int]$department_id, + + [int]$manager_id, + + [string]$employee_num, + + [bool]$activated, + + [string]$notes, + + [parameter(mandatory = $true)] + [string]$url, + + [parameter(mandatory = $true)] + [string]$apiKey + ) + + $Values = @{} + + $exclude = @('id', 'url', 'apiKey') + $excludeRegex = [string]::Join('|', $exclude) # create the regex + + foreach ($psbp in $PSBoundParameters.GetEnumerator()) { + if ($psbp.Key -notmatch $excludeRegex) { + $Values.Add($psbp.Key, $psbp.Value) + } + } + + $Body = $Values | ConvertTo-Json; + + $Parameters = @{ + Uri = "$url/api/v1/users/$id" + Method = 'PATCH' + Body = $Body + Token = $apiKey + } + + If ($PSCmdlet.ShouldProcess("ShouldProcess?")) { + $result = Invoke-SnipeitMethod @Parameters + } + + $result +} diff --git a/SnipeitPS/SnipeItPS.psd1 b/SnipeitPS/SnipeItPS.psd1 index 2a45466aa6629aba3cc601ea56b7bbc38365044b..4e10ab59002769975316eb060501136e64159a8e 100644 GIT binary patch delta 290 zcmccQyufWkj?iR3IgZJvM7SoG2}yz36NFU2^bH}A$qhmRP_`aioq(_coNXnn1ZKww z>wxJFC=D`ipRfj4?3u6;T)mPA#10z~h#d(c5dEh_cqaEjcGx#y2GL%nVFCq;!PYZ7DKd5<1q6l|Ji3&05Om>tLm4)hL&}Hyp$Y)4qNMtBs h$ON+T7}OcSDnS0{bq0#$G86!1^B5{OXNqR>008ZZQXBvP delta 104 zcmZ4BcFB1|j?m;1A(hDsfY?ivYw|TADKO0>tTI_oSZi{Eu+HQuKn#+*B&;;~kFe2X xGZCZ71tKbw=KwKSTts5>ClR&D3ZhDr=ZR`fz96bMnN3V@vW=L`=3QcGJOCQiA^89R