简体   繁体   English

使用Powershell导出到CSV

[英]exporting to csv with powershell -edited

I'm reading in an csv file (list of students, school, birthdays, etc) creating their login names and exporting the data to another csv that will be imported into another system. 我正在读取一个csv文件(学生列表,学校,生日等),以创建他们的登录名,并将数据导出到另一个将导入到另一个系统的csv中。 Everything is working great except i can only get one line of data in my csv file (the last user name). 除了我只能在csv文件(最后一个用户名)中获取一行数据之外,其他所有东西都运行良好。 i assume it is overwriting the same line each time. 我认为它每次都覆盖同一行。 Doing this in powershell. 在powershell中执行此操作。 help. 救命。 Here's my code: 这是我的代码:

Add-PSSnapin Quest.ActiveRoles.ADManagement

#import list of students from hourly IC extract
$users = Import-Csv C:\Users\edge.brandon\Desktop\enrollment\mbcextract.csv | 
  where {$_.'grade' -ne 'PK' -and $_.'name' -ne 'Ombudsman' -and $_.'name' -ne 'Ombudsman MS' -and $_.'name' -ne 'z Transition Services' -and $_.'name' -ne 'z Home Services'}

if ($users) {
  foreach ($u in $users) {

    # sets middle name and initial to null so that variable does not cary over to next student if they have no middle name
    $middle= $null
    $mi= $null

    $first= ($u.'firstname')
    $last= ($u.'lastname')
    $middle= ($u.'middlename')
    $birth= ($u.'birthdate')
    $grade= ($u.'grade')
    $id= ($u.'studentNumber')
    $schoolid= ($u.'sch.number') 

    #Removes spaces, apostrophes, hyphens, periods, commas from name
    $first=$first.Replace(" ", "")
    $middle=$middle.Replace(" ", "")
    $last=$last.Replace(" ", "")
    $first=$first.Replace("'", "")
    $middle=$middle.Replace("'", "")
    $last=$last.Replace("'", "")
    $first=$first.Replace("-", "")
    $middle=$middle.Replace("-", "")
    $last=$last.Replace("-", "")
    $first=$first.Replace(".", "")
    $middle=$middle.Replace(".", "")
    $last=$last.Replace(".", "")
    $first=$first.Replace(",", "")
    $middle=$middle.Replace(",", "")
    $last=$last.Replace(",", "")

    # sets 1st and middle initial. also sets mmdd of birth
    $fi= $first.substring(0,1)
    $mi= $middle.substring(0,1)
    $mmdd =$birth.substring(0,4)

    #sets username and then makes sure it truncates anything after 20 characters
    $un= ($last + $fi + $mi +$mmdd)
    $un= $un.substring(0,20)
  }
}  **$users | 
  select $id,$un,$first,$last,$schoolid,$grade,$upn,"1"," ","0" | export-csv MBC.csv**

Remove-PSSnapin Quest.ActiveRoles.ADManagement

i found a mistake i changed the $users to $u ($u | select $id,$un,$first,$last,$schoolid,$grade,$upn,"1"," ","0" | export-csv mbc.csv -NoTypeInformation) 我发现了一个错误,我将$users更改为$u ($u | select $id,$un,$first,$last,$schoolid,$grade,$upn,"1"," ","0" | export-csv mbc.csv -NoTypeInformation)

and added a break point step thru the code and if i open the csv file as the code is running the first line of the csv file populates correctly with each pass. 并通过代码添加了一个断点步骤,如果我在代码运行时打开csv文件,则每次通过csv文件的第一行都会正确填充。 it continues to write on the first line...I tried the append statement but it didn't help...how do u make it go to the next line in the csv file (ie write a line of data on row 1, go to row 2 write another line of data) 它继续在第一行上写...我尝试了append语句,但没有帮助...如何使它转到csv文件的下一行(即,在第1行上写一行数据,转到第二行写入另一行数据)

I can't provide a full answer (I don't have csv data to test with offhand and don't know the relevant bits of powershell well enough from memory to comment specifically) but at the very least part of the problem is likely that you are using the values of variables in your select statement and I can't imagine you actually meant to do that. 我无法提供完整的答案(我没有要进行临时测试的csv数据,也无法从内存中充分了解Powershell的相关位以进行具体评论),但至少有一部分问题可能是您在select语句中使用变量的值,但我无法想象您实际上打算这样做。

I imagine you meant to grab the properties with those names instead ( select id,un,first,last,... instead of select $id,$un,$first,$last,... ). 我想您打算用这些名称代替属性( select id,un,first,last,...而不是select $id,$un,$first,$last,... )。

I know this is nearly two years old, but others might find this topic. 我知道这已经快两年了,但是其他人可能会找到这个话题。

The problem here is that the export-csv will recreate the file each time it is called. 这里的问题是,export-csv会在每次调用时重新创建文件。 Combine that with the fact that you are sending export-csv the variable values calculated for the last record in $users, and that would explain your results. 结合使用以下事实:您将向export-csv发送为$ users中的最后一条记录计算的变量值,这将解释您的结果。

I found an article that outlines how to do what you're intending to do (I have a similar project that I'm starting on). 我找到了一篇概述您打算如何做的文章(我有一个类似的项目正在开始)。 Working with Custom Objects: https://technet.microsoft.com/en-us/library/ff730946.aspx 使用自定义对象: https : //technet.microsoft.com/zh-cn/library/ff730946.aspx

As in the article, you would create a array of custom objects, then load each object with property values from your calculated variable values, as you read through the objects in $users 如本文中所述,您将创建一个自定义对象数组,然后在读取$ users中的对象时,使用计算出的变量值中的属性值加载每个对象

At the end, you would export your array of objects (not $users or $un) using the export-csv command. 最后,您将使用export-csv命令导出对象数组(而不是$ users或$ un)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM