snazy2000/SnipeitPS

Accessing SnipeitPS functions from a module

yaroz opened this issue · 3 comments

yaroz commented

Context

Trying to create a module, to call functions based on SnipeitPS and it will not recognize the URL/apikey after it's set.

Your Environment

SnipeitPS version 1.9.183
VSCode, running latest powershell version

Code example from module

function Get-DepartmentID { 
    param (
    [Parameter(Mandatory)]
    $Name
    )
    $department = get-snipeitdepartment | Where-Object {$_.name -eq $Name}
    if (([string]::IsNullOrEmpty($department)))
    {
        return (New-snipeitDepartment -name $Name).id
    }
    else
    {
        return $department.ID
    }
} 

When I load the module that contains this code, then do Get-DepartmentID -Name IT, it asks for the url and apikey. If I copy and paste that code into the window I'm currently in, and run the same command, it works, or if I run just Get-SnipeitDepartment | ? {$_.name -eq "IT"} it works as well.

yaroz commented

I was able to get around this by setting the $url and $api key, then using the parameters on the snipeitps commands. But, it didn't matter where I was using the set-snipeitinfo command at, whether it was the script that I was calling the module from, or the module itself.

yaroz commented

Ok.. so in the module, I have the set-snipeitinfo line, then I have to scope the variables with $Script:url = 'xxxxxx' and $Script:apikey = 'xxxxx'

Is this normal? This is my first time trying to create modules and scoping variables..

yaroz commented

I updated to the latest, and removed the scoped variables, and variables from the calls, and it worked as intended.