我有这个从powershell查询SQL:
PARAM([String] $env_code, [String] $db_server)
$connectionString = "ConnectTheShizz"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$sqlCommand = "SELECT Yada FROM YadaYada"
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()
$strStuff = $DataSet.Tables[0].rows| ft -HideTableHeaders
我不确定$ strStuff是什么(它是什么DataType或者其他什么)。如果我只是将它打印到控制台,我会得到一个看起来像一个整数的数组(虽然在数据库中,这些是字符串,所以我不确定这是否是他们回来的那样):
$strStuff
0003128
0005588
0016308
0021216
...
但如果我试图让它们成为字符串:
$strStuff | ForEach-Object {$_.ToString()}
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
Microsoft.PowerShell.Commands.Internal.Format.GroupStartData
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
...
如果我只是假设他们是字符串,并尝试继续前进,我就明白了:
$strCusips -join ','
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData,Microsoft.PowerShell.Commands.Internal.Format.GroupStartData,Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData,Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData,...
编辑:
为了更清楚,这就是我所拥有的:
0003128
0005588
0016308
0021216
...
这就是我想要的(一个以逗号分隔的单个字符串):
0003128,0005588,0016308,0021216,...
编辑2:
使用.GetType()
我明白了:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
| Get-Member
这样做:
TypeName: Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object...
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
autosizeInfo Property Microsoft.PowerShell.Comm...
ClassId2e4f51ef21dd47e99d3c952918aff9cd Property string ClassId2e4f51ef21d...
groupingEntry Property Microsoft.PowerShell.Comm...
pageFooterEntry Property Microsoft.PowerShell.Comm...
pageHeaderEntry Property Microsoft.PowerShell.Comm...
shapeInfo Property Microsoft.PowerShell.Comm...
TypeName: Microsoft.PowerShell.Commands.Internal.Format.GroupStartData
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object...
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
ClassId2e4f51ef21dd47e99d3c952918aff9cd Property string ClassId2e4f51ef21d...
groupingEntry Property Microsoft.PowerShell.Comm...
shapeInfo Property Microsoft.PowerShell.Comm...
TypeName: Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object...
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
ClassId2e4f51ef21dd47e99d3c952918aff9cd Property string ClassId2e4f51ef21d...
formatEntryInfo Property Microsoft.PowerShell.Comm...
outOfBand Property bool outOfBand {get;set;}
writeStream Property Microsoft.PowerShell.Comm...
TypeName: Microsoft.PowerShell.Commands.Internal.Format.GroupEndData
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object...
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
ClassId2e4f51ef21dd47e99d3c952918aff9cd Property string ClassId2e4f51ef21d...
groupingEntry Property Microsoft.PowerShell.Comm...
TypeName: Microsoft.PowerShell.Commands.Internal.Format.FormatEndData
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object...
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
ClassId2e4f51ef21dd47e99d3c952918aff9cd Property string ClassId2e4f51ef21d...
groupingEntry Property Microsoft.PowerShell.Comm...