我在Powershell脚本中有多行数据,如下所示:
SSAS=PerformanceAnalysisService, Provider=SQLNCLI11.1;Data Source=LAP123;Integrated Security=SSPI;Initial Catalog=PerformanceDataMart:
SSAS=EnvironmentalAnalysisService, Provider=SQLNCLI11.1;Data Source=LAP123;Integrated Security=SSPI;Initial Catalog=EnvironmentalDataMart:
我当前正在使用:
ConvertFrom-String -Delimiter ";" -PropertyNames Server, Provider, Source, Security, Catalog.
哪个给我:
Server Provider Source Security
------ -------- ------ --------
SSAS=EnvironmentalAnalysisService, Provider=SQLNCLI11.1 Data Source=LAP123 Integrated Security=SSPI Initial Catalog=EnvironmentalDataMart:
SSAS=PerformanceAnalysisService, Provider=SQLNCLI11.1 Data Source=DEV-EDW Integrated Security=SSPI Initial Catalog=PerformanceDataMart:
我想创建一个表,以键作为列标题,或者甚至能够从表数据中删除键。
+------------------------------+-------------+-------------+-----------------+-----------------------+
| SSAS | Provider | Data Source | Security | Catalog |
+------------------------------+-------------+-------------+-----------------+-----------------------+
| EnvironmentalAnalysisService | SQLNCLI11.1 | LAP123 | Integrated SSPI | EnvironmentalDataMart |
| PerformanceAnalysisService | SQLNCLI11.1 | LAP123 | Integrated SSPI | PerformanceDataMart |
+------------------------------+-------------+-------------+-----------------+-----------------------+
但是有没有办法识别键/列名并动态地删除它们呢?
最终结果将由SQL使用。因此,要么将数据作为单独的列返回,要么将逗号分隔都很好。
答案 0 :(得分:1)
ConverTo-Csv
应该能够做到这一点,只需将ConvertFrom-String
的输出传递给它即可。
> $in = "SSAS=PerformanceAnalysisService, Provider=SQLNCLI11.1;Data Source=LAP123;Integrated Security=SSPI;Initial Catalog=PerformanceDataMart:","SSAS=EnvironmentalAnalysisService, Provider=SQLNCLI11.1;Data Source=LAP123;Integrated Security=SSPI;Initial Catalog=EnvironmentalDataMart:"
> $in | ConvertFrom-String -Delimiter ";" -PropertyNames Server, Provider, Source, Security, Catalog | ConvertTo-Csv -NoTypeInformation -Delimiter ';'
"Server";"Provider";"Source";"Security"
"SSAS=PerformanceAnalysisService, Provider=SQLNCLI11.1";"Data Source=LAP123";"Integrated Security=SSPI";"Initial Catalog=PerformanceDataMart:"
"SSAS=EnvironmentalAnalysisService, Provider=SQLNCLI11.1";"Data Source=LAP123";"Integrated Security=SSPI";"Initial Catalog=EnvironmentalDataMart:"
如果要将内容存储到CSV文件中,也可以将ConverTo-Csv
替换为Export-Csv
,或者可以将ConvertTo-Csv
的输出通过管道传输到Out-File
>
希望有帮助