powershell PS -“方法异常:找不到重载”-自定义类异常

osh3o9ms  于 2023-01-13  发布在  Shell
关注(0)|答案(2)|浏览(123)

我正在使用一个定制类来创建一个定制PSObject,它可以被填充并转换为JSON文件,用作API请求的主体
我的目标是:

  • 示例化自定义类以初始化自定义对象
  • 使用不同的方法(此处为AddExtensions)填充此对象

此方法的目标是填充字符串数组和一些硬编码值
这个类看起来像这样

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

我的代码运行如下

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

我的**$extensions**对象只是一个字符串数组

PS C:\Project> $Extensions

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

当我尝试使用该方法时,出现以下错误:

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

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

Hashtable对象中有一个"Add"方法,所以我不明白错误。如果我通过[System.Collections.Generic.List [object]]切换Hashtable,它会工作,但我特别需要一个键/值对用于稍后的Json转换。

    • 更新**

我们的目标是将对象转换为JSON,最后看起来如下所示:

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

mklgxw1f1#

正如在注解中提到的,在向哈希表添加新条目时,您需要提供一个与每个值相关联的 key。假设您希望通过“扩展名”来索引扩展,您可以执行以下操作:

class AllowList {

    # ...

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

pvabu6sv2#

特别关注如何更好地解决这个问题,看看你的JsonExtensions属性出现了两次,它们都有相同的结构。在这种情况下,你可以把问题分解成小块,创建一个具有此结构的Extensions类,然后让主类将此新类型作为其属性(为了集中讨论这个特定的问题,我已经删除了代码中不相关的部分):

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

相关问题