exporting to csv with powershell -edited -
i'm reading in csv file (list of students, school, birthdays, etc) creating login names , exporting data csv imported system. working great except can 1 line of data in csv file (the last user name). assume overwriting same line each time. doing in powershell. help. here's code:
add-pssnapin quest.activeroles.admanagement #import list of students hourly ic extract $users = import-csv c:\users\edge.brandon\desktop\enrollment\mbcextract.csv | {$_.'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 , initial null variable not cary on next student if 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 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 , middle initial. sets mmdd of birth $fi= $first.substring(0,1) $mi= $middle.substring(0,1) $mmdd =$birth.substring(0,4) #sets username , makes sure truncates 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 mistake changed $users
$u ($u | select $id,$un,$first,$last,$schoolid,$grade,$upn,"1"," ","0" | export-csv mbc.csv -notypeinformation)
and added break point step thru code , if open csv file code running first line of csv file populates correctly each pass. continues write on first line...i tried append statement didn't help...how u make go next line in csv file (ie write line of data on row 1, go row 2 write line of data)
i can't provide full answer (i don't have csv data test offhand , don't know relevant bits of powershell enough memory comment specifically) @ least part of problem using values of variables in select statement , can't imagine meant that.
i imagine meant grab properties names instead (select id,un,first,last,...
instead of select $id,$un,$first,$last,...
).
Comments
Post a Comment