Merge branch 'issue#67-powershell7-compatibility' of https://github.com/PetriAsi/SnipeitPS into PetriAsi-issue#67-powershell7-compatibility

This commit is contained in:
Petri Asikainen 2021-05-16 08:11:10 +03:00
commit 44c14d6cc0
29 changed files with 38 additions and 41 deletions

View file

@ -21,23 +21,21 @@ function Get-ParameterValue {
# } # }
[CmdletBinding()] [CmdletBinding()]
param( param(
# The $MyInvocation for the caller -- DO NOT pass this (dot-source Get-ParameterValues instead) # Pass $MyInvocation.MyCommand.Parameters to function, powershell 7 seems to only populate variables with dot sourcing
$Invocation = $MyInvocation, [parameter(mandatory = $true)]
# The $PSBoundParameters for the caller -- DO NOT pass this (dot-source Get-ParameterValues instead) $Parameters
$BoundParameters = $PSBoundParameters, ,
[string[]]$DefaultExcludeParameter = @("id", "url", "apiKey", 'Debug', 'Verbose') [string[]]$DefaultExcludeParameter = @("id", "url", "apiKey", 'Debug', 'Verbose')
) )
if ($MyInvocation.Line[($MyInvocation.OffsetInLine - 1)] -ne '.') { if ($MyInvocation.Line[($MyInvocation.OffsetInLine - 1)] -ne '.') {
throw "Get-ParameterValues must be dot-sourced, like this: . Get-ParameterValues" throw "Get-ParameterValue must be dot-sourced, like this: . Get-ParameterValues"
}
if ($PSBoundParameters.Count -gt 0) {
throw "You should not pass parameters to Get-ParameterValues, just dot-source it like this: . Get-ParameterValues"
} }
$ParameterValues = @{} $ParameterValues = @{}
foreach ($parameter in $Invocation.MyCommand.Parameters.GetEnumerator()) { foreach ($parameter in $Parameters.GetEnumerator()) {
# gm -in $parameter.Value | Out-Default # gm -in $parameter.Value | Out-Default
try { try {
$key = $parameter.Key $key = $parameter.Key
@ -48,9 +46,6 @@ function Get-ParameterValue {
} }
} }
if ($BoundParameters.ContainsKey($key)) {
$ParameterValues[$key] = $BoundParameters[$key]
}
} }
} }
finally {} finally {}

View file

@ -60,7 +60,7 @@ function Get-Accessory() {
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Parameters = @{ $Parameters = @{
Uri = "$url/api/v1/accessories" Uri = "$url/api/v1/accessories"

View file

@ -118,7 +118,7 @@ function Get-Asset() {
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/hardware" $apiurl = "$url/api/v1/hardware"

View file

@ -62,7 +62,7 @@ function Get-AssetMaintenance() {
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Parameters = @{ $Parameters = @{
Uri = "$url/api/v1/maintenances" Uri = "$url/api/v1/maintenances"

View file

@ -54,7 +54,7 @@ function Get-Category()
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/categories" $apiurl = "$url/api/v1/categories"

View file

@ -54,7 +54,7 @@ function Get-Company()
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/companies" $apiurl = "$url/api/v1/companies"

View file

@ -62,7 +62,7 @@ function Get-Component() {
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/components" $apiurl = "$url/api/v1/components"

View file

@ -57,7 +57,7 @@ function Get-Department()
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/departments" $apiurl = "$url/api/v1/departments"

View file

@ -79,7 +79,7 @@ function Get-License() {
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/licenses" $apiurl = "$url/api/v1/licenses"

View file

@ -54,7 +54,7 @@ function Get-Manufacturer()
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/manufacturers" $apiurl = "$url/api/v1/manufacturers"

View file

@ -54,7 +54,7 @@ function Get-Model()
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/models" $apiurl = "$url/api/v1/models"

View file

@ -54,7 +54,7 @@ function Get-SnipeitLocation()
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/locations" $apiurl = "$url/api/v1/locations"

View file

@ -54,7 +54,7 @@ function Get-Status()
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/statuslabels" $apiurl = "$url/api/v1/statuslabels"

View file

@ -54,7 +54,7 @@ function Get-Supplier()
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/suppliers" $apiurl = "$url/api/v1/suppliers"

View file

@ -60,7 +60,7 @@ function Get-User() {
[string]$apiKey [string]$apiKey
) )
$SearchParameter = . Get-ParameterValue $SearchParameter = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$apiurl = "$url/api/v1/users" $apiurl = "$url/api/v1/users"

View file

@ -41,7 +41,7 @@ function New-Accessory() {
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
if ($values['purchase_date']) { if ($values['purchase_date']) {
$values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd") $values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd")

View file

@ -36,7 +36,7 @@ function New-AssetMaintenance() {
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
if ($values['start_date']) { if ($values['start_date']) {
$values['start_date'] = $values['start_date'].ToString("yyyy-MM-dd") $values['start_date'] = $values['start_date'].ToString("yyyy-MM-dd")

View file

@ -39,7 +39,7 @@ function New-Company()
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Body = $Values | ConvertTo-Json; $Body = $Values | ConvertTo-Json;

View file

@ -58,7 +58,7 @@ function New-Component() {
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
if ($values['purchase_date']) { if ($values['purchase_date']) {
$values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd") $values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd")

View file

@ -46,7 +46,7 @@ function New-CustomField()
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
#Convert Values to JSON format #Convert Values to JSON format
$Body = $Values | ConvertTo-Json; $Body = $Values | ConvertTo-Json;

View file

@ -52,7 +52,7 @@ function New-Department() {
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Body = $Values | ConvertTo-Json; $Body = $Values | ConvertTo-Json;

View file

@ -56,7 +56,7 @@ function New-License() {
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
if ($values['expiration_date']) { if ($values['expiration_date']) {
$values['expiration_date'] = $values['expiration_date'].ToString("yyyy-MM-dd") $values['expiration_date'] = $values['expiration_date'].ToString("yyyy-MM-dd")

View file

@ -78,7 +78,7 @@ function New-Location() {
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Body = $Values | ConvertTo-Json; $Body = $Values | ConvertTo-Json;

View file

@ -41,7 +41,7 @@ function Set-Accessory() {
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
if ($values['purchase_date']) { if ($values['purchase_date']) {
$values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd") $values['purchase_date'] = $values['purchase_date'].ToString("yyyy-MM-dd")

View file

@ -16,7 +16,7 @@
.PARAMETER Model_id .PARAMETER Model_id
Model ID of the asset, this can be got using Get-Model Model ID of the asset, this can be got using Get-Model
.PARAMETER last_checkout .PARAMETER last_checkout
Date the asset was last checked out Date the asset was last checked out
@ -82,7 +82,7 @@ function Set-Asset()
[string]$Status_id, [string]$Status_id,
[string]$Model_id, [string]$Model_id,
[DateTime]$last_checkout, [DateTime]$last_checkout,
[int]$assigned_to, [int]$assigned_to,
@ -114,7 +114,9 @@ function Set-Asset()
[hashtable] $customfields [hashtable] $customfields
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
if ($model_id) { $Values.Add('model_id',$model_id)}
if ($customfields) if ($customfields)
{ {

View file

@ -58,7 +58,7 @@ function Set-License() {
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
if ($values['expiration_date']) { if ($values['expiration_date']) {
$values['expiration_date'] = $values['expiration_date'].ToString("yyyy-MM-dd") $values['expiration_date'] = $values['expiration_date'].ToString("yyyy-MM-dd")

View file

@ -31,7 +31,7 @@ function Set-Model() {
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Body = $Values | ConvertTo-Json; $Body = $Values | ConvertTo-Json;

View file

@ -82,7 +82,7 @@ function Set-SnipeitLocation() {
[string]$apiKey [string]$apiKey
) )
$Values = . Get-ParameterValue $Values = . Get-ParameterValue $MyInvocation.MyCommand.Parameters
$Body = $Values | ConvertTo-Json; $Body = $Values | ConvertTo-Json;

Binary file not shown.