我正在阅读csv文件(学生,学校,生日等列表),创建他们的登录名并将数据导出到另一个将导入另一个系统的csv。 除了我只能得到我的csv文件中的一行数据(最后一个用户名),一切都很好。我认为它每次都覆盖相同的行。在PowerShell中执行此操作。帮帮我。这里是我的代码:使用powershell -edited导出到csv
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行写另一行数据)
你能否提供一些示例数据行? –
我认为@EtanReisner是正确的:-) –