Merge pull request #3 from snazy2000/develop

Merge develop to main
This commit is contained in:
Stephen 2018-03-17 21:41:17 +00:00 committed by GitHub
commit 7bc58aea88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 716 additions and 33 deletions

View file

@ -1,29 +1,18 @@
variables:
GIT_SSL_NO_VERIFY: "true"
before_script:
- Get-PackageProvider -Name NuGet -ForceBootstrap | Out-Null
- Install-Module InvokeBuild, BuildHelpers, PSScriptAnalyzer -force -Scope CurrentUser
- Install-Module Pester -Force -SkipPublisherCheck -Scope CurrentUser
- Import-Module PSScriptAnalyzer -Scope CurrentUser
stages:
- test
# - release
ps_scriptanalyzer:
- release
variables:
GIT_SSL_NO_VERIFY: "true"
ErrorActionPreference: STOP
Test:
stage: test
script:
- $res = (Invoke-ScriptAnalyzer -Path . -Severity Error).count
- if ($res -gt 0) { throw "$($res) Analytics failed."}
tags:
- powershell
# except:
# - master
#Release:
# stage: release
# script:
# - .\build.ps1 -Tasks 'analyze','test','release'
# only:
# - master
script:
- .\build.ps1 -Tasks 'test'
except:
- master
Release:
stage: release
script:
- .\build.ps1 -Tasks 'test','release'
only:
- master

View file

@ -56,6 +56,7 @@ task InstallPandoc -If (-not (Test-Path Tools\pandoc.exe)) {
# Get latest bits
$latestRelease = "https://github.com/jgm/pandoc/releases/download/1.19.2.1/pandoc-1.19.2.1-windows.msi"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $latestRelease -OutFile "$($env:temp)\pandoc.msi"
# Extract bits

View file

@ -106,6 +106,9 @@
$result
}
}
elseif ($webResponse.StatusCode -eq "Unauthorized") {
Write-Error "[$($MyInvocation.MyCommand.Name)] You are not Authorized to access the resource, check your token is correct"
}
else {
# No content, although statusCode < 400
# This could be wanted behavior of the API

View file

@ -29,6 +29,9 @@ function Get-Asset()
$Parameters = @{
Uri = "$url/api/v1/hardware"
Method = 'Get'
GetParameters = @{
limit = 9999
}
Token = $apiKey
}

View file

@ -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
}

View file

@ -0,0 +1,39 @@
<#
.SYNOPSIS
# Gets a list of Snipe-it Departments
.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-Department -url "https://assets.example.com" -token "token..."
.EXAMPLE
Get-Department -url "https://assets.example.com" -token "token..." | Where-Object {$_.name -eq "Department1" }
#>
function Get-Department()
{
Param(
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$Parameters = @{
Uri = "$url/api/v1/departments"
Method = 'Get'
Token = $apiKey
}
$result = Invoke-SnipeitMethod @Parameters
$result
}

View file

@ -0,0 +1,39 @@
<#
.SYNOPSIS
# Gets a list of Snipe-it Locations
.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-Location -url "https://assets.example.com" -token "token..."
.EXAMPLE
Get-Location -url "https://assets.example.com" -token "token..." | Where-Object {$_.name -eq "Location1" }
#>
function Get-Location()
{
Param(
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$Parameters = @{
Uri = "$url/api/v1/locations"
Method = 'Get'
Token = $apiKey
}
$result = Invoke-SnipeitMethod @Parameters
$result
}

View file

@ -0,0 +1,39 @@
<#
.SYNOPSIS
# Gets a list of Snipe-it Suppliers
.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-Supplier -url "https://assets.example.com" -token "token..."
.EXAMPLE
Get-Supplier -url "https://assets.example.com" -token "token..." | Where-Object {$_.name -eq "MySupplier" }
#>
function Get-Supplier()
{
Param(
[parameter(mandatory=$true)]
[string]$url,
[parameter(mandatory=$true)]
[string]$apiKey
)
$Parameters = @{
Uri = "$url/api/v1/suppliers"
Method = 'Get'
Token = $apiKey
}
$result = Invoke-SnipeitMethod @Parameters
$result
}

View file

@ -37,7 +37,7 @@ function New-Asset()
{
[CmdletBinding(
SupportsShouldProcess = $true,
ConfirmImpact = "High"
ConfirmImpact = "Low"
)]
Param(

View file

@ -31,7 +31,7 @@ function New-Component()
{
[CmdletBinding(
SupportsShouldProcess = $true,
ConfirmImpact = "High"
ConfirmImpact = "Low"
)]
Param(

View file

@ -0,0 +1,77 @@
<#
.SYNOPSIS
Short description
.DESCRIPTION
Long description
.PARAMETER name
Parameter description
.PARAMETER category_id
Parameter description
.PARAMETER qty
Parameter description
.PARAMETER url
Parameter description
.PARAMETER apiKey
Parameter description
.EXAMPLE
An example
.NOTES
General notes
#>
function New-Department()
{
[CmdletBinding(
SupportsShouldProcess = $true,
ConfirmImpact = "Low"
)]
Param(
[parameter(mandatory = $true)]
[string]$name,
[string]$company_id,
[string]$location_id,
[string]$manager_id,
[parameter(mandatory = $true)]
[string]$url,
[parameter(mandatory = $true)]
[string]$apiKey
)
$Values = @{
"name" = $name
"company_id" = $company_id
"location_id" = $location_id
"manager_id" = $manager_id
}
$Body = $Values | ConvertTo-Json;
$Parameters = @{
Uri = "$url/api/v1/departments"
Method = 'POST'
Body = $Body
Token = $apiKey
}
If ($PSCmdlet.ShouldProcess("ShouldProcess?"))
{
$result = Invoke-SnipeitMethod @Parameters
}
$result
}

View file

@ -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
}

View file

@ -22,7 +22,7 @@ function New-Manufacturer()
{
[CmdletBinding(
SupportsShouldProcess = $true,
ConfirmImpact = "High"
ConfirmImpact = "Low"
)]
Param(

View file

@ -31,7 +31,7 @@ function New-Model()
{
[CmdletBinding(
SupportsShouldProcess = $true,
ConfirmImpact = "High"
ConfirmImpact = "Low"
)]
Param(

View file

@ -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
}

View file

@ -37,7 +37,7 @@ function Set-Asset()
{
[CmdletBinding(
SupportsShouldProcess = $true,
ConfirmImpact = "High"
ConfirmImpact = "Medium"
)]
Param(

View file

@ -2,7 +2,7 @@ function Set-AssetOwner()
{
[CmdletBinding(
SupportsShouldProcess = $true,
ConfirmImpact = "High"
ConfirmImpact = "Medium"
)]
Param(

View file

@ -2,7 +2,7 @@ function Set-Component()
{
[CmdletBinding(
SupportsShouldProcess = $true,
ConfirmImpact = "High"
ConfirmImpact = "Medium"
)]
Param(

View file

@ -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
}

Binary file not shown.

101
build.ps1 Normal file
View file

@ -0,0 +1,101 @@
param(
[string[]]$Tasks
)
function Install-Dependency([string] $Name)
{
$policy = Get-PSRepository -Name "PSGallery" | Select-Object -ExpandProperty "InstallationPolicy"
if($policy -ne "Trusted") {
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
}
if (!(Get-Module -Name $Name -ListAvailable)) {
Install-Module -Name $Name -Scope CurrentUser
}
}
function Run-Tests
{
param(
[string]$Path = "$PSScriptRoot\SnipeitPS"
)
$results = Invoke-Pester -PassThru
if($results.FailedCount -gt 0) {
Write-Output " > $($results.FailedCount) tests failed. The build cannot continue."
foreach($result in $($results.TestResult | Where {$_.Passed -eq $false} | Select-Object -Property Describe,Context,Name,Passed,Time)){
Write-Output " > $result"
}
EXIT 1
}
}
function Release
{
Write-Output "Setting Variables"
$BuildRoot = $env:CI_PROJECT_DIR
$releasePath = "$BuildRoot\Release"
Write-Output "Build Root : $BuildRoot"
Write-Output "Release Root : $releasePath"
if (-not (Test-Path "$releasePath\SnipeitPS")) {
$null = New-Item -Path "$releasePath\SnipeitPS" -ItemType Directory
}
# Copy module
Copy-Item -Path "$BuildRoot\SnipeitPS\*" -Destination "$releasePath\SnipeitPS" -Recurse -Force
# Copy additional files
$additionalFiles = @(
"$BuildRoot\CHANGELOG.md"
"$BuildRoot\LICENSE"
"$BuildRoot\README.md"
)
Copy-Item -Path $additionalFiles -Destination "$releasePath\SnipeitPS" -Force
$manifestContent = Get-Content -Path "$releasePath\SnipeitPS\SnipeitPS.psd1" -Raw
if ($manifestContent -notmatch '(?<=ModuleVersion\s+=\s+'')(?<ModuleVersion>.*)(?='')') {
throw "Module version was not found in manifest file,"
}
$currentVersion = [Version] $Matches.ModuleVersion
if ($env:CI_JOB_ID) {
$newRevision = $env:CI_JOB_ID
}
else {
$newRevision = 0
}
$version = New-Object -TypeName System.Version -ArgumentList $currentVersion.Major,
$currentVersion.Minor,
$newRevision
Write-Output "New version : $version"
Update-Metadata -Path "$releasePath\SnipeitPS\SnipeitPS.psd1" -PropertyName ModuleVersion -Value $version
$functionsToExport = Get-ChildItem "$BuildRoot\SnipeitPS\Public" | ForEach-Object {$_.BaseName}
Set-ModuleFunctions -Name "$releasePath\SnipeitPS\SnipeitPS.psd1" -FunctionsToExport $functionsToExport
#Remove-Module SnipeitPS
Import-Module $env:CI_PROJECT_DIR\SnipeitPS\SnipeitPS.psd1 -force -ErrorAction Stop
Publish-Module -Name SnipeitPS -Repository InternalPowerShellModules -NuGetApiKey 123456789
}
foreach($task in $Tasks){
switch($task)
{
"test" {
Install-Dependency -Name "PSScriptAnalyzer"
Install-Dependency -Name "Pester"
Write-Output "Running Pester Tests..."
Run-Tests
}
"release" {
Write-Output "Releasing..."
Release
}
}
}