1

我正在读取一个 csv 文件(学生、学校、生日等列表),创建他们的登录名并将数据导出到另一个 csv,该 csv 将被导入另一个系统。一切都很好,除了我只能在我的 csv 文件中获取一行数据(最后一个用户名)。我认为它每次都覆盖同一行。在 powershell 中执行此操作。帮助。这是我的代码:

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

我发现了一个错误,我将其更改$users$u ($u | select $id,$un,$first,$last,$schoolid,$grade,$upn,"1"," ","0" | export-csv mbc.csv -NoTypeInformation)

并通过代码添加了一个断点步骤,如果我在代码运行时打开 csv 文件,则 csv 文件的第一行会在每次传递时正确填充。它继续写在第一行...我尝试了 append 语句,但没有帮助...你如何让它进入 csv 文件的下一行(即在第 1 行写入一行数据,转到第 2 行写入另一行数据)

4

2 回答 2

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.

I imagine you meant to grab the properties with those names instead (select id,un,first,last,... instead of select $id,$un,$first,$last,...).

于 2013-08-23T19:17:03.850 回答
0

我知道这已经快两年了,但其他人可能会发现这个话题。

这里的问题是每次调用 export-csv 都会重新创建文件。结合您发送 export-csv 为 $users 中的最后一条记录计算的变量值这一事实,这将解释您的结果。

我发现一篇文章概述了如何做你打算做的事情(我有一个类似的项目,我正在开始)。使用自定义对象:https ://technet.microsoft.com/en-us/library/ff730946.aspx

如文章中所述,您将创建一个自定义对象数组,然后在读取 $users 中的对象时,从计算的变量值中加载每个对象的属性值

最后,您将使用 export-csv 命令导出对象数组(不是 $users 或 $un)。

于 2015-02-24T21:16:36.583 回答