itglue / automation

A place for IT Glue's user community to learn ways of automating their documentation.
Apache License 2.0
99 stars 33 forks source link

Issue running the module commandlets twice using Azure Function #33

Closed EricB-OnestepIT closed 4 years ago

EricB-OnestepIT commented 4 years ago

Hello!

I've made a small script that is supposed to run as an Azure Function. Everything works the first time a request is received but on the second request I get this error message regarding Convert-Json Depth:

`ERROR: Cannot validate argument on parameter 'Depth'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again.

Exception : Type : System.Management.Automation.ParameterBindingValidationException Message : Cannot validate argument on parameter 'Depth'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again. ParameterName : Depth ParameterType : int ErrorId : ParameterArgumentValidationError Line : 247 Offset : 54 CommandInvocation : MyCommand : ConvertTo-Json BoundParameters : Comparer : System.OrdinalIgnoreCaseComparer`

My script: `##################################### #

Onestep IT Agent Backend

Powershell 7.0

#

Version: 0.1

Eric Birgersson / Onestep IT 2020

# #####################################

using namespace System.Net

Input bindings are passed in via param block.

param($Request, $TriggerMetadata)

##################################

IT Glue API Parameters

##################################

Add-ITGlueBaseURI -base_uri https://api.eu.itglue.com Add-ITGlueAPIKey -Api_Key #########

##################################

Get input data

##################################

$hostname = $Request.Body.Hostname $ipaddress = $Request.Body.IPaddress $Serialnumber = $Request.Body.Serialnumber $model = $Request.Body.Model $manufacturer = $Request.Body.Manufacturer

$platform = $Request.Body.Platform # Set automatically when setting OS

$version = $Request.Body.Version $isserver = $Request.Body.IsServer $isvm = $Request.Body.IsVM $orgid = $Request.Body.OrgID

Write-Host $Request.Body

Trim input data to Match IT Glue

$version = $version.replace("Microsoft ","") $version = $version -replace '( Pro)',' Professional' $manufacturer = $manufacturer.replace(" Inc.","")

Get configurations for the org with the same hostname, if exists

$configs = Get-ITGlueConfigurations -organization_id $orgid -filter_name $hostname -page_size 1000

##################################

Set or create Manufacturer

##################################

Get manufacturers to loop through to get id or create new

$manufacturers = Get-ITGlueManufacturers -page_size 1000 $manu_id = 0;

Search for existing manufacturer and save id

foreach ($man in $manufacturers.data) { if ($man.attributes.name -like $manufacturer) {

Found matching manufacturer - set id

    $manu_id = $man.id
}

}

Check if match was found, if not create manufacturer

if ($manu_id -eq 0) {

No manufacturer found, create new

$new_manu = @{
    type = "manufacturers"
    attributes = @{
        name = $manufacturer
    }
}
# Create and set new ID
$return = New-ITGlueManufacturers -data $new_manu
$manu_id = $return.id

}

##################################

Set or create Model

##################################

Get models to loop through to get id or create new

$models = Get-ITGlueModels -page_size 1000 $mod_id = 0;

Search for existing model and save id

foreach ($mod in $models.data) { if ($mod.attributes.name -like $model) {

Found matching model - set id

    $mod_id = $mod.id
}

}

Check if match was found, if not create model

if ($mod_id -eq 0) {

No model found, create new

$new_mod = @{
    type = "models"
    attributes = @{
        name = $model
        manufacturer_id = $manu_id
    }
}
# Create and set new ID
$return = New-ITGlueModels -data $new_mod
$mod_id = $return.id

}

##################################

Create data table to send

##################################

Data to push to ITG

$data = @{ type = "configurations" attributes = @{ name = $hostname hostname = $hostname configuration_type_id = 1378672435314917 configuration_status_id = 1378672425238664 # Status is always Active manufacturer_id = $manu_id model_id = $mod_id primary_ip = $ipaddress serial_number = $Serialnumber } }

##################################

Set Operating system

##################################

Get Operating Systems to get ID

$oss = Get-ITGlueOperatingSystems -page_size 1000

foreach ($os in $oss.data) { if ($version -like $os.attributes.name) { $data.attributes.operating_system_id= $os.id } }

##################################

Set Config Type

##################################

Determine type

$types = Get-ITGlueConfigurationTypes -page_size 1000

Is VM?

if ($isvm) {

Is VM

foreach ($type in $types.data) {
    if ($type.attributes.name -Like "VM") {
        # Found VM, set id
        $data.attributes.configuration_type_id = $type.id
    }
}

} else {

Not VM

if ($isserver) {
    #Server
    foreach ($type in $types.data) {
        if ($type.attributes.name -Like "Server") {
            # Found VM, set id
            $data.attributes.configuration_type_id = $type.id
        }
    }
} else {
    #Workstation
    foreach ($type in $types.data) {
        if ($type.attributes.name -Like "Workstation") {
            # Found VM, set id
            $data.attributes.configuration_type_id = $type.id
        }
    }
}

}

##################################

Create or Update Configuration

##################################

Check if config exist to update or create

if ($configs.meta.'total-count' -eq 0) {

Configuration not found, create!

$new_config = New-ITGlueConfigurations -organization_id $orgid -data $data

} else {

Configuration found, update!

$idtoupdate = $configs.data.id | Select-Object -First 1 # Set the first one if multiple configurations with the same hostname exists
$new_config = Set-ITGlueConfigurations -id $idtoupdate -organization_id $orgid -data $data

}

$body = $new_config.id

Associate values to output bindings by calling 'Push-OutputBinding'.

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ StatusCode = [HttpStatusCode]::OK Body = $body }) `

EricB-OnestepIT commented 4 years ago

I posted wrong, sorry!