Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
C# 将对象传递给PowerShell函数_C#_Powershell_Powershell 2.0 - Fatal编程技术网

C# 将对象传递给PowerShell函数

C# 将对象传递给PowerShell函数,c#,powershell,powershell-2.0,C#,Powershell,Powershell 2.0,我试图将SQL适配器对象传递给PowerShell函数,但出现以下错误: executeQueryAndFillTable:无法在上处理参数转换 参数“da”。无法转换类型为的“System.Object[]”值 “System.Object[]”输入“System.Data.SqlClient.SqlDataAdapter” 这是密码 function sql_pull { # define Objects $xmlDoc = New-Object System.Xml.Xml

我试图将SQL适配器对象传递给PowerShell函数,但出现以下错误:

executeQueryAndFillTable:无法在上处理参数转换 参数“da”。无法转换类型为的“System.Object[]”值 “System.Object[]”输入“System.Data.SqlClient.SqlDataAdapter”

这是密码

function sql_pull
{
    # define Objects
    $xmlDoc = New-Object System.Xml.XmlDocument
    $sqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $sqlCommand = New-Object System.Data.SqlClient.SqlCommand
    $sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $connectionString = "Password=$password;Persist Security Info=True;User ID=$userId;Data Source=$dataSource"
    $counter = 0

    # database queries 
    $queries = @(
    "Select * from sys.configurations for xml Raw ('Cretiria'), type, ROOT('sys.configurations'), ELEMENTS");

    $sqlConnection.ConnectionString = $connectionString
    $sqlCommand.Connection = $sqlConnection

    try {
        $sqlConnection.Open()
        
        foreach($q in $queries) 
        {
            $sqlCommand.CommandText = $q
            $sqlAdapter.SelectCommand = $sqlCommand.CommandText
            $sqlAdapter.SelectCommand.CommandTimeout = 300

            $res = executeQueryAndFillTable($sqlAdapter, $sqlCommand)              
        }

        $sqlConnection.Dispose()
        $sqlCommand.Dispose()
        $sqlAdapter.Dispose()
    } 
    catch
    {
        Throw
    }    
}

function executeQueryAndFillTable
{
    param(
        [System.Data.SqlClient.SqlDataAdapter]$da,
        [System.Data.SqlClient.SqlCommand] $command
    )

    $dataTable = New-Object System.Data.DataTable
    $da.SelectCommand = $command
    $da.Fill($dataTable)
    #check
    $data = $dataTable.Rows[0][0]
    return $data
}
两件事:

首先:应在使用前声明PowerShell中的函数

:调用函数的方式

executeQueryAndFillTable($sqlAdapter, $sqlCommand)
这不是在PowerShell中调用函数的正确方法。如果您以这种方式调用它,PowerShell会认为您调用的函数只有一个参数,即两个不同类型元素的数组(非常重要的
是PowerShell中的数组运算符)(错误中
System.Object[]
的原因)

正确的方法是:

executeQueryAndFillTable $sqlAdapter $sqlCommand

$da=New Object System.Data.SqlClient.SqlDataAdapter是否可能重复?@JaquelineVanek问题是如何调用函数。因此,dupePlease给出了一个您在这里使用的完整命令/管道的示例。仅显示函数不足以确定问题。@FoxDeploy问题就在这里:
executeQueryAndFillTable($sqlAdapter,$sqlCommand)
tnx!这帮了大忙@C.E也许你可以把这个问题作为答案来回答?@CE,请把这个标记为“答案”,这样它就不会一直出现在未回答的问题下面。