查询与Get-Member有什么关系?

时间:2019-07-10 22:39:51

标签: powershell

我有2个脚本。

脚本1 具有以下内容:

function Query($Query) {
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
    $SqlConnection.ConnectionString = "Server=$DB_Server;Initial Catalog=$Database;Integrated Security=SSPI" 
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand 
    $SqlCmd.Connection = $SqlConnection 
    $SqlCmd.CommandText = $Query 
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
    $SqlAdapter.SelectCommand = $SqlCmd 
    $DataSet = New-Object System.Data.DataSet 
    $a = $SqlAdapter.Fill($DataSet)
    $SqlConnection.Close() 
    $DataSet.Tables[0]
}

#create .net array object for csv export
$exportObject = New-Object System.Collections.ArrayList
#create ordered dictionary so column names come out in the ordered they were created
$rowObject = [ordered]@{}

$connection_string = "Connection Timeout=120;User Id=UID1;Data Source=datasource.com;Password=password12!553"

$rowObject.'Connection Details' = $connection_string

#INSERT connection string into Table
Query "UPDATE [$someTable]
SET [connection_string] = '$connection_string'
WHERE [cname] = '$cinput' AND ([pserver] = '$pinput'"

$exportObject.Add((New-Object PSObject -Property $rowObject)) | Out-Null

$exportObject | Select-Object

现在在script2中,我调用script1并将对象“输出变量”,然后将其转换为PSCustomObject以与HTML表函数一起使用(与该线程无关),因此不会包含在代码中。更多信息在here上)。

脚本2:

& ".\script1.ps1" -ViewConnection "$cinput" -OutVariable xprtOut | Format-Table -Wrap

#converting xprtOut from Arraylist to pscustomobject to be used with ConvertTo-HTMLTable
$Arr = @()
foreach ($Object in $xprtOut) {
    $i = -1
    $arrayListCount = -($Object | gm | Where-Object {$_.MemberType -like "noteproperty"}).Count

    $customObj = New-Object PSCustomObject
    do {
        $customObj | Add-Member -MemberType NoteProperty -Name (($Object | gm)[$($i)].Name) -Value ($Object."$(($Object | gm)[$($i)].Name)")
        $i--
    } while ($i -ge $arrayListCount)

    $Arr += $customObj
}

当我运行script2时,出现以下错误:

gm : You must specify an object for the Get-Member cmdlet.
$arrayListCount = -($Object | gm | Where-Object {$_.MemberType -l ...

Cannot index into a null array.
... dd-Member -MemberType NoteProperty -Name (($Object | gm)[$($i)].Name) ...

经过长时间的调试,我发现了根本问题:通过从script1删除UPDATE查询语句,script2停止了错误并且开始正常工作。

问题是,为什么script1中的查询语句会出现问题?与对象转换有什么关系?

1 个答案:

答案 0 :(得分:1)

当您将第一个脚本的输出输出为我已经提到的变量时,您在格式化第一个脚本的输出时会遇到问题。如果您遇到的问题是Update语句,那么我将检查您的语法在PowerShell中是否正确解析。还不清楚您的函数如何接收$ DB_Server和$ Database值。我不确定是否可以解决您的问题,但是我认为可以简化查询功能的代码,这是一个对我有用的最小示例。

function Query {
    Param (
        [parameter(Mandatory=$true,Position = 0)][string]$SqlConnection,
        [parameter(Mandatory=$true, Position = 1)][string]$Data
    )

    # Create a new connection
    $SqlClient = New-Object System.Data.SqlClient.SqlConnection($SqlConnection)
    $SqlClient.Open()

    # Submit the query
    $Query = "UPDATE <tablename> SET <column_name> = '$Data' WHERE <column_name> = '<value>'"
    # You may want to comment out the rest of the function here and see how your Query looks.
    $Command = New-Object System.Data.SqlClient.SqlCommand($Query,$SqlClient)
    $DataSet = New-Object System.Data.DataSet
    $DataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($Command)
    $DataAdapter.Fill($DataSet) | Out-Null

    # Close and return your data. You may want to dispose of the client too.
    $DataSet.Tables[0]
    $SqlClient.Close()
    $SqlClient.Dispose()
}

$UserConnection = "Connection Timeout=120;User Id=UID1;Data Source=datasource.com;Password=password12!553"
$SqlConnection = "Server=<Server\Instance>; Database=<Database_Name>; Integrated Security=True; Connect Timeout=15"
Query -SqlConnection $SqlConnection -Data $UserConnection

然后,您可能想遍历SQL Connections的自定义对象和要添加的用户连接,并以此方式创建输出对象。这应该按您提供的顺序保持输出。

$Export = $Connections | Foreach-Object { Query -SqlConnection $_.SqlConnection -Data $_.UserConnection }
return $Export

然后,您可以继续执行脚本2的操作,如下所示。我不确定您是否需要另外一个。

foreach ($obj in $Export) {
    # Do stuff here.
}