Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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
将powershell SQL Server输出格式化为列和行_Sql_Sql Server 2008_Tsql_Powershell - Fatal编程技术网

将powershell SQL Server输出格式化为列和行

将powershell SQL Server输出格式化为列和行,sql,sql-server-2008,tsql,powershell,Sql,Sql Server 2008,Tsql,Powershell,我是Powershell的初学者,尝试查询SQL Server数据库并将csv输出到有组织的行和列文件中。我可以通过以下命令部分实现这一点: sqlps -Command Invoke-Sqlcmd -ServerInstance "xxx.xx.xxx.xxx" -Database "xxxx" -InputFile "xxx" | out-file -filepath "xxxx" 我的问题是,查询的输出不是以行和列的形式出现,而是将每一行分组为字符串列表,每个单元格一个字符

我是Powershell的初学者,尝试查询SQL Server数据库并将csv输出到有组织的行和列文件中。我可以通过以下命令部分实现这一点:

sqlps -Command Invoke-Sqlcmd -ServerInstance "xxx.xx.xxx.xxx" 
      -Database "xxxx" -InputFile "xxx" | out-file -filepath "xxxx"
我的问题是,查询的输出不是以行和列的形式出现,而是将每一行分组为字符串列表,每个单元格一个字符串。例如:

column a: value 1  
column b: value 1  
column c: value 1  

column a: value 2  
column b: value 2  
column c: value 2  
我也尝试过使用
导出csv
,但是这只是返回上面命令返回的每个字符串的长度,与此问题完全相同:


我尝试了一个
组对象
,但我正在努力使它工作。如果有人能用一些明确的指导原则从概念上解释我要做的事情,我将不胜感激。

我想你要找的是-notype信息开关

$results | export.csv$FilePathOfCSV-notype信息
-NoTypeInformation是一个从输出的CSV文件中忽略类型信息的开关。 CSV的第一行包含类型,后跟它正在使用的.NET framework对象的全名

这将为您提供结构化的CSV输出


希望它能起作用

导出Csv
希望输入是对象。字符串输入被视为字符串对象,它们只有一个属性(
Length
),因此仅导出此属性。如果输入是字符串数组,则需要将其转换为对象,例如:

$array = "foo", "bar", "baz"

New-Object -Type PSCustomObject -Property @{
  "a" = $array[0]
  "b" = $array[1]
  "c" = $array[2]
} | Export-Csv output.csv -NoTypeInformation
Invoke-Sqlcmd ... | % {
  New-Object -Type PSCustomObject -Property @{
    "col1" = $_[0]
    "col2" = $_[1]
    "col3" = $_[2]
  }
} | Export-Csv output.csv -NoTypeInformation
上面将创建一个包含以下内容的文件
output.csv

"c","a","b"
"baz","foo","bar"
属性名称(
a
b
c
)成为CSV标题,属性值(
foo
bar
baz
)成为CSV值

如果SQL查询生成数组列表,则可能需要执行以下操作:

$array = "foo", "bar", "baz"

New-Object -Type PSCustomObject -Property @{
  "a" = $array[0]
  "b" = $array[1]
  "c" = $array[2]
} | Export-Csv output.csv -NoTypeInformation
Invoke-Sqlcmd ... | % {
  New-Object -Type PSCustomObject -Property @{
    "col1" = $_[0]
    "col2" = $_[1]
    "col3" = $_[2]
  }
} | Export-Csv output.csv -NoTypeInformation

不过,我手头没有SQL server,所以请谨慎考虑。

谢谢,我收到一些错误消息,是否正确:-Property@{“mypropertyname”=$columnheaderA[example value]}?从外观上看,可能不是。我给我的答案添加了更多的解释。非常感谢,我会给这个一个goHi-Robin,我尝试了这个没有用,我想问题在于我输入文件的查询输出