导出csv格式。电源外壳 [英] Export-csv formatting. Powershell

查看:181
本文介绍了导出csv格式。电源外壳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们开始说,我是Powershell的新手,而不是最伟大的工作与它的代码和脚本,但试图学习。现在的问题!

Let's start off by saying that I'm quite new to Powershell and not the greatest one working with it's code and scripts but trying to learn. And now to the problem!

我正在使用一个脚本从网络中的计算机获取信息。我有一些代码,为我的目的工作相当好。但是我遇到一些问题,当涉及到一些信息,主要是包含多个对象,如服务的信息。

I'm working on a script that fetches information from computers in the network. I've got some code that works quite well for my purposes. But I'm having some problem when it comes to some information, mostly information that contains multiple objects, like service.

#This application will pull information from a list of devices. The devices list is
sent with C:\Users\test.txt and output is pushed to a file C:\Users\devices.csv


function kopiera
{
param
(
    [Parameter(ValueFromPipeline=$true)]
    [string[]]$Computer=$env:computername
)    
Process
{
    $computer | ForEach-Object{

        $service=Get-WmiObject -Class Win32_Service -Computername $_


        $prop= [ordered]@{
                    Service =$service.caption 
                }
        New-Object PSCustomObject -Property   $prop
    }
}
}

Get-Content C:\Users\test.txt | kopiera | Export-csv c:\Users\devices.csv

当我导出csv文件时像这样:

When I export the csv file it looks like this:

TYPE System.Management.Automation.PSCustomObject

TYPE System.Management.Automation.PSCustomObject

服务

System.Object []

"System.Object[]"

因此,它不会获取service.caption(因为有太多?
但是如果我用out-file C:\Users\devices.txt替换export-csv C:\Users\device.csv,它看起来像这样:

So it doesn't fetch the service.caption (Because there are too many?). But If I replace the export-csv C:\Users\device.csv with out-file C:\Users\devices.txt it looks like this instead:

{Adobe Acrobat Update Service,Adobe Flash Player Update Service,Andrea ADI Filters Service,Application Experience ...}

{Adobe Acrobat Update Service, Adobe Flash Player Update Service, Andrea ADI Filters Service, Application Experience...}

更好,但它不会得到他们所有(仍然,因为有太多的服务?)。

So it's starting to look better, but it doesn't get them all (Still because there are too many services?). What I'd like to do with this export/out-file is to get the information to appear vertically instead of horizontal.

(想要的结果)

Adob​​e Acrobat服务

Adobe Acrobat Service

Adob​​e Flash Player更新服务

Adobe Flash Player Update Service

on ..

而不是:
(实际结果)

instead of: (Actual result)

Adob​​e Acrobat更新服务Flash Player更新服务,等等...

Adobe Acrobat Update Service, Adobe Flash Player Update Service, and so on...

有一种方法使这可能,一段时间,不能包围我的大脑这周围。

Is there a way to make this possible, been trying for a while and can't wrap my brain around this.

感谢任何帮助。

推荐答案

CSV通常不是是导出包含多值或复杂属性的对象的不错选择。对象属性将被转换为单个字符串值。存储值数组的唯一方法是将它转换为分隔字符串。

CSV is not usually a good choice for exporting objects that contain multi-valued or complex properties. The object properties are going to be converted to a single string value. The only way to store an array of values is to convert it to a delimited string.

$prop= [ordered]@{
                  Service =($service.caption) -join ';'
                 }

将创建一个分号分隔的字符串,您必须处理

will create a semi-colon delimited string, and you'll have to deal with splitting it back out in whatever appication is using the csv later.

如果要保存并重新导入具有属性的原始对象作为数组,您可以切换到Export-CLIXML而不是Export-CSV。

If you want to save and re-import the original object with the property as an array, you can switch to Export-CLIXML instead of Export-CSV.

这篇关于导出csv格式。电源外壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆