新对象:无法绑定参数“属性”。无法将PSCustomObject类型的“”值转换为IDictionary类型

时间:2019-11-22 09:27:58

标签: sql-server powershell automation

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

因此,我输入查询和SQL Server,然后运行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对象调用中失败了。我已经尝试过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

谢谢!

1 个答案:

答案 0 :(得分:0)

收集此数据的最简单方法是在if ($sqlInstance -match "---") {语句的开头捕获所有数据,然后仅输出PsCustomObject,而无需尝试对其进行转换。 像

$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将成为PSCustomObjects的数组,以在其余函数中进行处理。

希望这会有所帮助