PowerShell的 - 麻烦导出为CSV文件 [英] PowerShell - Trouble exporting to CSV file

查看:1246
本文介绍了PowerShell的 - 麻烦导出为CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有从一个PowerShell脚本将数据导出到一个CSV文件中的一个问题。这里的code:

Ok, I'm having an issue with exporting data from a PowerShell script into a CSV file. Here's the code:

$UserArray = @(Import-CSV "C:\UserProfiles.csv")
$DeleteArray = @()

Foreach ($user in $userArray)
    {
    $useremail = $user.email
    $Domain1Account = Get-ADUser -filter {mail -eq $useremail} -server dc1.domain.local
    If ($Domain1Account -eq $Null)
        {
        $Domain2Account = Get-ADUser -filter {mail -eq $useremail} -server dc2.domain2.local
        If ($Domain2Account -eq $null)
        {
        "User " + $user.email + " does not exist."
        $DeleteArray = $DeleteArray + $user.email
        }
    }
Else {"User found!!! " + $user.email + " exists in AD!!!"}
}

从本质上讲,我试图把现有的CSV文件,看看用户在两个不同的领域之一搜索其电子邮件地址。如果他们没有在任何领域存在,想我到了出口的电子邮件地址的列表($ DeleteArray清单)回为一个不同的.CSV文件。

Essentially, I'm trying to take an existing CSV file and see if the user exists in one of two different domains by searching for their email address. If they don't exist in either domain, I'd like to export that list of email addresses (the $DeleteArray list) back out as a different .CSV file.

问题是,每次我用这样的:

The problem is, every time I use something like this:

$DeleteArray | Export-CSV "C:\DeletedUsers.csv"

所有我回来是一个CSV文件:

all I get back is a CSV file with:

Length
32
31
29...

而不是我想要的:

instead of what I want:

email1@domain.com
email2@domain5.com
email5@domain4.com...

我是什么洞错了?

What am I dong wrong?

推荐答案

导出-CSV 是为自己有点太聪明在这种情况下。

Export-Csv is a little too smart for itself in this instance.

它试图发现输入对象有什么样的属性,以构建有意义的列名。

It tries to discover what properties the input objects have, in order to construct meaningful column names.

由于输入对象是所有类型的字符串和一个字符串只拥有(即长度 1财产字符串),导出-CSV 最终输出的是,由于您的CSV唯一列。

Since the input objects are all of type String and a string only has 1 property (the Length of the string), Export-Csv ends up exporting that as the only column in your CSV.

您可以使用选择对象来控制后续的列创建

You can use Select-Object to control subsequent "column creation":

$DeleteArray |Select-Object @{Name="Email";Expression={$_}} |Export-CSV "C:\DeletedUsers.csv"

另外,因为你的不是真的的兴趣在一个CSV,而只是一个字符串列表,使用出文件而不是:

Alternatively, since you're not really interested in a CSV, but just a list of strings, use Out-File instead:

$DeleteArray | Out-File C:\DeletedUsers.txt

这篇关于PowerShell的 - 麻烦导出为CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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