【问题标题】:New-Object : Cannot bind parameter 'Property'. Cannot convert the "" value of type PSCustomObject to type IDictionary新对象:无法绑定参数“属性”。无法将 PSCustomObject 类型的“”值转换为 IDictionary 类型
【发布时间】:2020-03-18 08:17:16
【问题描述】:

我很难将结果从 Invoke-SqlCmd 转换为 PSCustomobject。

所以,我输入了我的查询和 SQL 服务器,然后我运行 Invoke-SqlCmd 函数,然后我尝试将数据(数据库逻辑名称和自动增长状态)添加到我的 PSCustomobject,这样我就可以返回它回到我的模块公共功能。

$sqlinstance = "---"
$query = "---"

    if ($sqlInstance -match "---") {
        $dbAutogrowIGP = Invoke-Sqlcmd -Query $query -ServerInstance $sqlInstance

        if ($dbAutogrowIGP.Growth -gt 100 -and $dbAutogrowIGP.Growth -lt 500) {
            $autogrowStatus = [PSCustomObject]@{
                'SQL_Instance'  = $dbAutogrowIGP.LogicalName
                'Check'         = "Autogrow"
                'Status'        = "green"
                'Status_reason' = ""
            }
            New-Object -Type Dictionary -Property $autogrowStatus
        }

        foreach ($db in $dbAutogrowIGP) {
            if ($db.Growth -lt 100 -or $db.Growth -gt 500 -and $db.Growth -notlike "%") {
                $autogrowStatus = [PSCustomObject]@{
                    'SQL_Instance'  = $db.LogicalName
                    'Check'         = "Autogrow"
                    'Status'        = "red"
                    'Status_reason' = "$($db.LogicalName) has autogrowth set to $($db.Growth)."
                }
                New-Object -Type Dictionary -Property $autogrowStatus
            }

            if ($db.Growth -like "%") {
                $autogrowStatus = [PSCustomObject]@{
                    'SQL_Instance'  = $db.LogicalName
                    'Check'         = "Autogrow"
                    'Status'        = "yellow"
                    'Status_reason' = "$($db.LogicalName) has autogrowth set percentually, it should be an absolute number."
                }
                New-Object -Type Dictionary -Property $autogrowStatus
            }
        }
    }

return $autogrowStatus

我已经调试过了,我注意到它在 New-object 调用中失败了。我已经尝试过 Dictionary 和 PSObject/PSCustomObject - 但是两者都不起作用

在我的其他函数中,这按预期工作,但是在这些函数中,我使用 dbatools 进行调用。

        $getLogSizeIGP = Get-DbaDbLogSpace -sqlInstance $sqlInstance

        if ($getLogSizeIGP.LogSize.Gigabyte -lt 10 -and $getLogSizeIGP.LogSpaceUsedPercent -lt 50) {
            $logStatus = @{
                'SQL_Instance'  = $getLogSizeIGP.SqlInstance
                'Check'         = "Log_size"
                'Status'        = [gmEnvStatuses]::green
                'Status_reason' = ""
            }
            New-Object -Type PSObject -Property $logStatus
        }

我该如何解决这个问题?

这是整个错误信息:

New-Object : Cannot bind parameter 'Property'. Cannot convert the "@{SQL_Instance=Maintenance_log; Check=Autogrow; Status=red; Status_reason=Maintenance_log has autogrowth set to 10%.}" value of type "System.Management.Automation.PSCustomObject" to type 
"System.Collections.IDictionary".
At C:\Users\---\Desktop\autogrowth.ps1:50 char:55
+                 New-Object -Type Dictionary -Property $autogrowStatus
+                                                       ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-Object], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.NewObjectCommand

谢谢!

【问题讨论】:

    标签: sql-server powershell automation


    【解决方案1】:

    收集此数据的最简单方法是在 if ($sqlInstance -match "---") { 语句的开头捕获所有数据,然后简单地输出 PsCustomObjects 而无需尝试转换它们。 类似的东西

    $sqlinstance = "---"
    $query = "---"
    
    $autogrowStatus = if ($sqlInstance -match "---") {
        $dbAutogrowIGP = Invoke-Sqlcmd -Query $query -ServerInstance $sqlInstance
    
        if ($dbAutogrowIGP.Growth -gt 100 -and $dbAutogrowIGP.Growth -lt 500) {
            # output the object to be captured in the $autogrowStatus variable
            [PSCustomObject]@{
                'SQL_Instance'  = $dbAutogrowIGP.LogicalName
                'Check'         = "Autogrow"
                'Status'        = "green"
                'Status_reason' = ""
            }
        }
    
        foreach ($db in $dbAutogrowIGP) {
            if ($db.Growth -lt 100 -or $db.Growth -gt 500 -and $db.Growth -notlike "%") {
                [PSCustomObject]@{
                    'SQL_Instance'  = $db.LogicalName
                    'Check'         = "Autogrow"
                    'Status'        = "red"
                    'Status_reason' = "$($db.LogicalName) has autogrowth set to $($db.Growth)."
                }
            }
    
            if ($db.Growth -like "%") {
                [PSCustomObject]@{
                    'SQL_Instance'  = $db.LogicalName
                    'Check'         = "Autogrow"
                    'Status'        = "yellow"
                    'Status_reason' = "$($db.LogicalName) has autogrowth set percentually, it should be an absolute number."
                }
            }
        }
    }
    
    return $autogrowStatus
    

    变量$autogrowStatus 将成为一个 PSCustomObject 数组,以便在您的其余函数中进行处理。

    希望对你有帮助

    【讨论】:

    • 太棒了,如此简单的修复。这完美地工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2021-07-08
    • 1970-01-01
    相关资源
    最近更新 更多