diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bc7a59d..d0619a2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 \ No newline at end of file + script: + - .\build.ps1 -Tasks 'test' + except: + - master +Release: + stage: release + script: + - .\build.ps1 -Tasks 'test','release' + only: + - master diff --git a/SnipeItPS.build.ps1 b/SnipeItPS.build.ps1 index 5bbf1b5..2044ef4 100644 --- a/SnipeItPS.build.ps1 +++ b/SnipeItPS.build.ps1 @@ -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 diff --git a/SnipeitPS/Private/Invoke-SnipeitMethod.ps1 b/SnipeitPS/Private/Invoke-SnipeitMethod.ps1 index f340fa6..8400267 100644 --- a/SnipeitPS/Private/Invoke-SnipeitMethod.ps1 +++ b/SnipeitPS/Private/Invoke-SnipeitMethod.ps1 @@ -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 diff --git a/SnipeitPS/Public/Get-Asset.ps1 b/SnipeitPS/Public/Get-Asset.ps1 index 4eb0e2d..2cf5ed6 100644 --- a/SnipeitPS/Public/Get-Asset.ps1 +++ b/SnipeitPS/Public/Get-Asset.ps1 @@ -29,6 +29,9 @@ function Get-Asset() $Parameters = @{ Uri = "$url/api/v1/hardware" Method = 'Get' + GetParameters = @{ + limit = 9999 + } Token = $apiKey } 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/Get-Department.ps1 b/SnipeitPS/Public/Get-Department.ps1 new file mode 100644 index 0000000..d28cadf --- /dev/null +++ b/SnipeitPS/Public/Get-Department.ps1 @@ -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 +} + diff --git a/SnipeitPS/Public/Get-Location.ps1 b/SnipeitPS/Public/Get-Location.ps1 new file mode 100644 index 0000000..53218a4 --- /dev/null +++ b/SnipeitPS/Public/Get-Location.ps1 @@ -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 +} + diff --git a/SnipeitPS/Public/Get-Supplier.ps1 b/SnipeitPS/Public/Get-Supplier.ps1 new file mode 100644 index 0000000..8c8a0c4 --- /dev/null +++ b/SnipeitPS/Public/Get-Supplier.ps1 @@ -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 +} + diff --git a/SnipeitPS/Public/New-Asset.ps1 b/SnipeitPS/Public/New-Asset.ps1 index e6a8876..7d1678b 100644 --- a/SnipeitPS/Public/New-Asset.ps1 +++ b/SnipeitPS/Public/New-Asset.ps1 @@ -37,7 +37,7 @@ function New-Asset() { [CmdletBinding( SupportsShouldProcess = $true, - ConfirmImpact = "High" + ConfirmImpact = "Low" )] Param( diff --git a/SnipeitPS/Public/New-Component.ps1 b/SnipeitPS/Public/New-Component.ps1 index 7105811..690b1c8 100644 --- a/SnipeitPS/Public/New-Component.ps1 +++ b/SnipeitPS/Public/New-Component.ps1 @@ -31,7 +31,7 @@ function New-Component() { [CmdletBinding( SupportsShouldProcess = $true, - ConfirmImpact = "High" + ConfirmImpact = "Low" )] Param( diff --git a/SnipeitPS/Public/New-Department.ps1 b/SnipeitPS/Public/New-Department.ps1 new file mode 100644 index 0000000..0fe2536 --- /dev/null +++ b/SnipeitPS/Public/New-Department.ps1 @@ -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 +} + 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-Manufacturer.ps1 b/SnipeitPS/Public/New-Manufacturer.ps1 index b198a60..d85ee18 100644 --- a/SnipeitPS/Public/New-Manufacturer.ps1 +++ b/SnipeitPS/Public/New-Manufacturer.ps1 @@ -22,7 +22,7 @@ function New-Manufacturer() { [CmdletBinding( SupportsShouldProcess = $true, - ConfirmImpact = "High" + ConfirmImpact = "Low" )] Param( diff --git a/SnipeitPS/Public/New-Model.ps1 b/SnipeitPS/Public/New-Model.ps1 index c7a5067..af21b76 100644 --- a/SnipeitPS/Public/New-Model.ps1 +++ b/SnipeitPS/Public/New-Model.ps1 @@ -31,7 +31,7 @@ function New-Model() { [CmdletBinding( SupportsShouldProcess = $true, - ConfirmImpact = "High" + ConfirmImpact = "Low" )] Param( 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-Asset.ps1 b/SnipeitPS/Public/Set-Asset.ps1 index a53391a..a7decfb 100644 --- a/SnipeitPS/Public/Set-Asset.ps1 +++ b/SnipeitPS/Public/Set-Asset.ps1 @@ -37,7 +37,7 @@ function Set-Asset() { [CmdletBinding( SupportsShouldProcess = $true, - ConfirmImpact = "High" + ConfirmImpact = "Medium" )] Param( diff --git a/SnipeitPS/Public/Set-AssetOwner.ps1 b/SnipeitPS/Public/Set-AssetOwner.ps1 index 44d792e..3ecbc1c 100644 --- a/SnipeitPS/Public/Set-AssetOwner.ps1 +++ b/SnipeitPS/Public/Set-AssetOwner.ps1 @@ -2,7 +2,7 @@ function Set-AssetOwner() { [CmdletBinding( SupportsShouldProcess = $true, - ConfirmImpact = "High" + ConfirmImpact = "Medium" )] Param( diff --git a/SnipeitPS/Public/Set-Components.ps1 b/SnipeitPS/Public/Set-Components.ps1 index 84dd6b8..92f44f8 100644 --- a/SnipeitPS/Public/Set-Components.ps1 +++ b/SnipeitPS/Public/Set-Components.ps1 @@ -2,7 +2,7 @@ function Set-Component() { [CmdletBinding( SupportsShouldProcess = $true, - ConfirmImpact = "High" + ConfirmImpact = "Medium" )] Param( 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 76d510d..4e10ab5 100644 Binary files a/SnipeitPS/SnipeItPS.psd1 and b/SnipeitPS/SnipeItPS.psd1 differ diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 0000000..2c7d024 --- /dev/null +++ b/build.ps1 @@ -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+'')(?.*)(?='')') { + 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 + } + } +}