将包含PowerShell对象符号的文本文件转换为CSV
我有一个文本文件(psonobjects.txt),其中包含PowerShell对象表示法,如下所示:
@{Computer=Dektop123; ServiceA=Running; ServiceB=Running; RunspaceId=1a-1b}
@{Computer=Dektop456; ServiceA=Stopped; ServiceB=Stopped; RunspaceId=2b-2c}
@{Computer=Laptop123; ServiceA=NotFound; ServiceB=Running; RunspaceId=3c-4c}
@{ServiceA=NotFound; Computer=Laptop456; ServiceB=Running; RunspaceId=4d-5d}
我想将此PSON文件转换为CSV文件,但在整个尝试中都无法转换。
问题有时是对象的属性的顺序与以前的对象不同。就像上面示例中的最后一行一样。因此,简单地将半隆(';')转换为逗号(',')并删除'@{}'字符不会产生正确订购的CSV文件。
是否可以将此PSON(PowerShell对象符号)文件转换为CSV文件的简单方法?
我尝试了几种不同的方法,但是我一直在我的CSV输出文件中获取字符串的长度或HashTable输出的长度(ISReadOnly,IsFixedSize,IssyChronized,IssyChronized,IssyChronized,issychronized,issychronized,issychronized,issychronized,issychronized,issychronized,syncroot,count)。
非工作代码:
$inputFile = PsonObjects.txt
$CsvFileName = CsvOutput.csv
foreach($line in (Get-Contnet $inputfile)){
$newline = $line | convertFrom-StringData
$newline | Export-CSV $CsvFileName -Append
}
对此问题的任何帮助或建议将不胜感激。
I have a text file (PsonObjects.txt) that contains PowerShell Object Notation as follows:
@{Computer=Dektop123; ServiceA=Running; ServiceB=Running; RunspaceId=1a-1b}
@{Computer=Dektop456; ServiceA=Stopped; ServiceB=Stopped; RunspaceId=2b-2c}
@{Computer=Laptop123; ServiceA=NotFound; ServiceB=Running; RunspaceId=3c-4c}
@{ServiceA=NotFound; Computer=Laptop456; ServiceB=Running; RunspaceId=4d-5d}
I would like to convert this Pson file into a Csv file, but have been unable to throughout my attempts.
The problem is sometimes an object's properties are in a different order than the previous object. Like the last line in the above example. Therefor simply converting the semicolon (';') to a comma (',') and removing the '@{}' characters does not produce a properly ordered Csv file.
Is there an easy way to convert this Pson (Powershell object notation) file into a CSV file?
Ive tried a few different methods, but I keep getting either the length of the string, or hashtable output (IsReadOnly, IsFixedSize, IsSychronized, keys, Values, SyncRoot, Count) in my CSV output file.
Non Working code:
$inputFile = PsonObjects.txt
$CsvFileName = CsvOutput.csv
foreach($line in (Get-Contnet $inputfile)){
$newline = $line | convertFrom-StringData
$newline | Export-CSV $CsvFileName -Append
}
Any assistance or suggestions with this problem are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
convertfrom-stringdata
输出一个未分类hashtable
,其中元素可以按任何顺序为单位,因此在这里没有用。这是使用
-split
运算符和分类步骤的可能解决方案,使用订购Hashtable
:输出:
ConvertFrom-StringData
outputs an unsortedHashtable
, where the elements can be in any order, so it is of no use here.Here is a possible solution, using the
-split
operator and a sorting step, using an orderedHashtable
:Output:
这是 zett42的有用答案使用使用 href =“ https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.language.parser.parser?view = powershellsdk-7.0.0.0” class :
如果值在哪里,代码将更简单引用,因为我们可以选择
.safegetValue()
,正如您现在拥有的那样,解析器将它们解释为命令而不是字符串。Here is another alternative to zett42's helpful answer using the
Parser
Class:Code would be much simpler if the Values where quoted, since we would have the option to use
.SafeGetValue()
, as you have them right now the parser is interpreting them as commands instead of strings.