Home > Software design >  PS - "MethodException: Cannot find an overload" - cutom class exceptions
PS - "MethodException: Cannot find an overload" - cutom class exceptions

Time:12-24

I'm working on a custom class to create a custom PSObject ready to be filled and converted to a JSON file to be used as a body API request

Here's my goal :

  • Instantiate my custom class to initialize my custom object
  • Use different methods (here AddExtensions) to fill this object

This method's goal is to populate an array of strings, and some hard-coded values

the class looks like this

class allowlist {
[object] $Applications
[object] $Certificates
[object] $webdomains
[object] $ips_hosts
[object] $Extensions
[object] $windows
# Setting up the PSCustomObject structure from the JSON example : https://pastebin.com/FaKYpgw3
# TODO finish obj structure
allowlist() {
    $this.applications = [System.Collections.Generic.List[object]]::new()
    $this.Certificates = [System.Collections.Generic.List[object]]::new()
    $this.webdomains = [System.Collections.Generic.List[object]]::new()
    $this.ips_hosts = [System.Collections.Generic.List[object]]::new()
    $this.extensions = [System.Collections.Hashtable]::new()# TODO Extensions obj be hashtable. Converting to JSON will not be incorrect format (list instead of k/v pair)
    $this.windows = [PSCustomObject]@{
        files       = [System.Collections.Generic.List[object]]::new()
        directories = [System.Collections.Generic.List[object]]::new()
    }
}
#Method to add EXTENSIONS tab to the main obj
[void] AddExtensions(
    [array] $name
    # hardcoded values
    # [bool] $scheduled,
    # [string] $features
) {
    $this.extensions.add([PSCustomObject]@{
            names     = $name
            scheduled = $true
            features  = "AUTO_PROTECT"
        })
}

My code runs like this

    # Get Object from AllowList Class
    $obj_policy_excel = [allowlist]::new()
    ...
    # Add Extensions
    $obj_policy_excel.AddExtensions($Extensions.extensions)

my $extensions object is just an array of strings

PS C:\Project> $Extensions

extensions
----------
ade
adp
ldb

When I'm trying to use the Method, I have the following error :

PS C:\Project> $obj_policy_excel.AddExtensions($Extensions.extensions)

MethodException: Cannot find an overload for "add" and the argument count: "1".

There is a "Add" Method in the Hashtable object, so I don't understand the error. If I switch the Hashtable by a [System.Collections.Generic.List[object]] it works, but I specifically need a key/value pair for my later Json conversion.

UPDATE

The goal is to have object converted in JSON that looks like this at the end :

"extensions": {
        "names": [
            "txt",
            "exe",
            "doc"
        ],
        "scheduled": true,
        "features": [
            "AUTO_PROTECT"
        ]
    }

CodePudding user response:

As mentioned in the comments, you need to supply a key associated with each value when adding new entries to a hashtable. Assuming you want to index the extensions by "extension name", you could do something like:

class AllowList {

    # ...

    [void]
    AddExtensions([array] $extensionNames)
    {
        foreach($name in $extensionNames) {
            $this.Extensions.Add($name, [PSCustomObject]@{
                names     = $name
                scheduled = $true
                features  = "AUTO_PROTECT"
            })
        }
    }
}

CodePudding user response:

Focusing specifically on how might be a better way to approach it, looking at your Json there are 2 appearances of Extensions property and they both have the same structure. In this case you could break the problem into small pieces, create an Extensions class that has this structure and then and then have your main class have this new type as it's property (I've removed the not relevant pieces of your code for the sake of focusing on this specific problem):

class Extensions {
    [Collections.Generic.List[string]] $names = [Collections.Generic.List[string]]::new()
    [bool] $scheduled
    [Collections.Generic.List[string]] $features = [Collections.Generic.List[string]]::new()
}

class allowlist {
    [Extensions] $Extensions

    [void] AddExtensions([Extensions] $Extension) {
        $this.Extensions = $Extension
    }
}

$allowList = [allowlist]::new()
$allowList.AddExtensions(@{
    names     = 'pdf'
    scheduled = $true
    features  = 'AUTO_PROTECT'
})

$allowList | ConvertTo-Json
  • Related