Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 新对象:无法绑定参数';属性';。无法转换“"&引用;PSCustomObject类型的值转换为IDictionary类型_Sql Server_Powershell_Automation - Fatal编程技术网

Sql server 新对象:无法绑定参数';属性';。无法转换“"&引用;PSCustomObject类型的值转换为IDictionary类型

Sql server 新对象:无法绑定参数';属性';。无法转换“"&引用;PSCustomObject类型的值转换为IDictionary类型,sql-server,powershell,automation,Sql Server,Powershell,Automation,我很难将调用SqlCmd的结果转换为PSCustomobject 因此,我输入我的查询和SQL server,然后运行Invoke-SqlCmd函数,然后尝试将其中的数据(数据库逻辑名称和自动增长状态)添加到我的PSCustomobject,这样我就可以将其返回到我的modules-public函数 $sqlinstance = "---" $query = "---" if ($sqlInstance -match "---") { $dbAutogrowIGP =

我很难将调用SqlCmd的结果转换为PSCustomobject

因此,我输入我的查询和SQL server,然后运行Invoke-SqlCmd函数,然后尝试将其中的数据(数据库逻辑名称和自动增长状态)添加到我的PSCustomobject,这样我就可以将其返回到我的modules-public函数

$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
我已经调试过了,我注意到它在新对象调用时失败了。我试过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

谢谢

收集此数据的最简单方法是在
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
将成为PSCustomObjects数组,以便在其余函数中处理


希望这有帮助

太棒了,这么简单的修复方法。效果非常好。谢谢!