mirror of
https://github.com/snazy2000/SnipeitPS.git
synced 2025-12-15 02:35:47 +00:00
Keep it simple ...
This commit is contained in:
parent
64d4d4f55d
commit
e934d20ced
3 changed files with 142 additions and 38 deletions
17
SnipeitPS/Private/Test-SnipeitPSConnection.ps1
Normal file
17
SnipeitPS/Private/Test-SnipeitPSConnection.ps1
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -18,73 +18,70 @@
|
||||||
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
Connect-SnipeitPS -Url $url -apiKey $myapikey
|
Connect-SnipeitPS -Url $url -apiKey $myapikey
|
||||||
Connect to Snipe it api and stores connection information.
|
Connect to Snipe it api.
|
||||||
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
Connect-SnipeitPS -Url $url -apiKey $myapikey -DontStore
|
Connect-SnipeitPS -Url $url -SecureApiKey $myapikey
|
||||||
Just connects to Snipe it api, connection information is not stored.
|
Connects to Snipe it api with apikey stored to securestring
|
||||||
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
Connect-SnipeitPS -Url $url
|
Connect-SnipeitPS -siteCred (Get-Credential -message "Use site url as username and apikey as password")
|
||||||
Connects existing Snipe It Url with stored apiKey
|
Connect to Snipe It with PSCredential object
|
||||||
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
Connect-SnipeitPS
|
Build credential with apiakay value from secret vault (Microsoft.PowerShell.SecretManagement)
|
||||||
Connects last used Snipe It Url with stored apikey
|
$siteurl = "https://mysnipeitsite.url"
|
||||||
|
$apikey = Get-SecretInfo -Name SnipeItApiKey
|
||||||
|
$siteCred = New-Object -Type PSCredential -Argumentlist $siteurl,$spikey
|
||||||
|
Connect-SnipeitPS -siteCred $siteCred
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#>
|
#>
|
||||||
function Connect-SnipeitPS {
|
function Connect-SnipeitPS {
|
||||||
[CmdletBinding(
|
[CmdletBinding(
|
||||||
DefaultParameterSetName = 'Connect to existing connection'
|
DefaultParameterSetName = 'Connect with url and apikey'
|
||||||
)]
|
)]
|
||||||
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')]
|
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')]
|
||||||
|
|
||||||
param (
|
param (
|
||||||
|
|
||||||
[Parameter(ParameterSetName='Setup new connection',Mandatory=$true)]
|
[Parameter(ParameterSetName='Connect with url and apikey',Mandatory=$true)]
|
||||||
[Parameter(ParameterSetName='Connect to existing connection',Mandatory=$false)]
|
[Parameter(ParameterSetName='Connect with url and secure apikey',Mandatory=$true)]
|
||||||
[Uri]$url,
|
[Uri]$url,
|
||||||
|
|
||||||
[Parameter(ParameterSetName='Setup new connection',Mandatory=$true)]
|
[Parameter(ParameterSetName='Connect with url and apikey',Mandatory=$true)]
|
||||||
[String]$apiKey,
|
[String]$apiKey,
|
||||||
|
|
||||||
[Parameter(ParameterSetName='Setup new connection')]
|
[Parameter(ParameterSetName='Connect with url and secure apikey',Mandatory=$true)]
|
||||||
[switch]$DontStore
|
[SecureString]$SecureApiKey,
|
||||||
|
|
||||||
|
|
||||||
|
[Parameter(ParameterSetName='Connect with credential',Mandatory=$true)]
|
||||||
|
[PSCredertial]$siteCred
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
PROCESS {
|
PROCESS {
|
||||||
switch ($PsCmdlet.ParameterSetName) {
|
switch ($PsCmdlet.ParameterSetName) {
|
||||||
'Setup new connection' {
|
'Connect with url and apikey' {
|
||||||
try {
|
|
||||||
$SnipeitPSSession.url = $url
|
$SnipeitPSSession.url = $url
|
||||||
$SnipeitPSSession.apiKey = $apiKey
|
$SnipeitPSSession.apiKey = $apiKey | ConvertTo-SecureString -AsPlainText
|
||||||
|
|
||||||
#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' {
|
'Connect with url and secure apikey' {
|
||||||
# TODO: everything
|
$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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
90
SnipeitPS/Public/Connect-SnipeitPS.ps1
Normal file
90
SnipeitPS/Public/Connect-SnipeitPS.ps1
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue