Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PS – "MethodException: Cannot find an overload" – cutom class exceptions

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"
        ]
    }

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading