Keep it simple ...

This commit is contained in:
Petri Asikainen 2021-08-22 22:09:34 +03:00
parent 64d4d4f55d
commit e934d20ced
3 changed files with 142 additions and 38 deletions

View file

@ -0,0 +1,17 @@
function Test-SnipeitPSConnection {
#test api connection
$Parameters = @{
Api = '/api/v1/statuslabels'
Method = 'Get'
GetParameters = @{'limit'=1}
}
Write-Verbose "Testing connection to $url."
$contest = Invoke-SnipeitMethod @Parameters
if ( $contest) {
Write-Verbose "Connection to $url tested succesfully."
return $true
} else {
return $false
}
}

View file

@ -18,73 +18,70 @@
.EXAMPLE
Connect-SnipeitPS -Url $url -apiKey $myapikey
Connect to Snipe it api and stores connection information.
Connect to Snipe it api.
.EXAMPLE
Connect-SnipeitPS -Url $url -apiKey $myapikey -DontStore
Just connects to Snipe it api, connection information is not stored.
Connect-SnipeitPS -Url $url -SecureApiKey $myapikey
Connects to Snipe it api with apikey stored to securestring
.EXAMPLE
Connect-SnipeitPS -Url $url
Connects existing Snipe It Url with stored apiKey
Connect-SnipeitPS -siteCred (Get-Credential -message "Use site url as username and apikey as password")
Connect to Snipe It with PSCredential object
.EXAMPLE
Connect-SnipeitPS
Connects last used Snipe It Url with stored apikey
Build credential with apiakay value from secret vault (Microsoft.PowerShell.SecretManagement)
$siteurl = "https://mysnipeitsite.url"
$apikey = Get-SecretInfo -Name SnipeItApiKey
$siteCred = New-Object -Type PSCredential -Argumentlist $siteurl,$spikey
Connect-SnipeitPS -siteCred $siteCred
#>
function Connect-SnipeitPS {
[CmdletBinding(
DefaultParameterSetName = 'Connect to existing connection'
DefaultParameterSetName = 'Connect with url and apikey'
)]
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')]
param (
[Parameter(ParameterSetName='Setup new connection',Mandatory=$true)]
[Parameter(ParameterSetName='Connect to existing connection',Mandatory=$false)]
[Parameter(ParameterSetName='Connect with url and apikey',Mandatory=$true)]
[Parameter(ParameterSetName='Connect with url and secure apikey',Mandatory=$true)]
[Uri]$url,
[Parameter(ParameterSetName='Setup new connection',Mandatory=$true)]
[Parameter(ParameterSetName='Connect with url and apikey',Mandatory=$true)]
[String]$apiKey,
[Parameter(ParameterSetName='Setup new connection')]
[switch]$DontStore
[Parameter(ParameterSetName='Connect with url and secure apikey',Mandatory=$true)]
[SecureString]$SecureApiKey,
[Parameter(ParameterSetName='Connect with credential',Mandatory=$true)]
[PSCredertial]$siteCred
)
PROCESS {
switch ($PsCmdlet.ParameterSetName) {
'Setup new connection' {
try {
'Connect with url and apikey' {
$SnipeitPSSession.url = $url
$SnipeitPSSession.apiKey = $apiKey
#test connection
$Parameters = @{
Api = '/api/v1/statuslabels'
Method = 'Get'
GetParameters = @{'limit'=1}
}
Write-Verbose "Testin connection to $url."
$contest = Invoke-SnipeitMethod @Parameters
if ( $contest) {
Write-Verbose "Connection to $url tested succesfully."
}
}
catch {
throw "Cannot setup connection to $url. To start troubleshooting, check your url, certificates and apiKey"
}
# TODO: Save connection information safely on disk
$SnipeitPSSession.apiKey = $apiKey | ConvertTo-SecureString -AsPlainText
}
'Connect to existing connection' {
# TODO: everything
'Connect with url and secure apikey' {
$SnipeitPSSession.url = $url
$SnipeitPSSession.apiKey = $secureApiKey
}
'Connect with credential' {
$SnipeitPSSession.url = $siteCred.Username
$SnipeitPSSession.apiKey = $siteCred.GetNetworkCredential().SecurePassword
}
}
if (-not (Test-SnipeitPSConnection)) {
throw "Cannot verify connection to snipe it. For the start check url and provided apikey"
}
}
}

View file

@ -0,0 +1,90 @@
<#
.SYNOPSIS
Sets authetication information
.DESCRIPTION
Set and stores apikey and url user to connect Snipe-It system.
Based on Set-SnipeitInfo command, that's now just combatipility wrapper
and calls Connect-SnipeitPS
.PARAMETER url
URL of Snipeit system.
.PARAMETER apiKey
User's API Key for Snipeit.
.PARAMETER DontStore
Don't store connection information just connect to Url
.EXAMPLE
Connect-SnipeitPS -Url $url -apiKey $myapikey
Connect to Snipe it api and stores connection information.
.EXAMPLE
Connect-SnipeitPS -Url $url -apiKey $myapikey -DontStore
Just connects to Snipe it api, connection information is not stored.
.EXAMPLE
Connect-SnipeitPS -Url $url
Connects existing Snipe It Url with stored apiKey
.EXAMPLE
Connect-SnipeitPS
Connects last used Snipe It Url with stored apikey
#>
function Connect-SnipeitPS {
[CmdletBinding(
DefaultParameterSetName = 'Connect to existing connection'
)]
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')]
param (
[Parameter(ParameterSetName='Setup new connection',Mandatory=$true)]
[Parameter(ParameterSetName='Connect to existing connection',Mandatory=$false)]
[Uri]$url,
[Parameter(ParameterSetName='Setup new connection',Mandatory=$true)]
[String]$apiKey,
[Parameter(ParameterSetName='Setup new connection')]
[switch]$DontStore
)
PROCESS {
switch ($PsCmdlet.ParameterSetName) {
'Setup new connection' {
try {
$SnipeitPSSession.url = $url
$SnipeitPSSession.apiKey = $apiKey
#test connection
$Parameters = @{
Api = '/api/v1/statuslabels'
Method = 'Get'
GetParameters = @{'limit'=1}
}
Write-Verbose "Testin connection to $url."
$contest = Invoke-SnipeitMethod @Parameters
if ( $contest) {
Write-Verbose "Connection to $url tested succesfully."
}
}
catch {
throw "Cannot setup connection to $url. To start troubleshooting, check your url, certificates and apiKey"
}
# TODO: Save connection information safely on disk
}
'Connect to existing connection' {
# TODO: everything
}
}
}
}