0

I have a .csv file that uses 2 columns that both have headings: Teacher,Channel

Below the teacher column sits the teachers username and below the Channel column sits the teachers assigned Channel.

I am trying to get a script working that will check through the .csv for the logged on teachers username and then reply with their channel and if their username doesn't exist then append it and assign it a channel that is 1 number higher than the previous teachers channel. Once the script has added the new username of the logged on user it should use their newly assigned channel.

So far I have this:

$strCSV = "\\SEVERNAME\SHARE\FOLDER"
$strCurrentUser = Get-WmiObject -Class Win32_ComputerSystem
$strChannel = Import-Csv $strCSV\teachers.csv | where {$_.Teacher -eq $strCurrentUser.UserName.TrimStart("DOMAIN\")} | % channel

It's not much, but I am really new to Powershell, so any help would be amazing please.

1 Answer 1

3

something like this would work. it imports the CSV checks for the existing user. If found do stuff else if not found it gets last channel adds one and exports the new teacher to the end of the file.

$file = "\\SEVERNAME\SHARE\FOLDER\teachers.csv"
$teachers = Import-Csv $file
$strCurrentUser = (Get-WmiObject -Class Win32_ComputerSystem).UserName.TrimStart("DOMAIN\")
$strChannel = $teachers | where {$_.Teacher -eq $strCurrentUser} | % channel
if($strChannel){
#dostuff with channel
}
else{
$lastChannel = [int]$teachers[$teachers.length-1].channel + 1
"{0},{1}" -f $strCurrentUser,$lastChannel | add-content -path $file
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Dane, Could you explain to me please what the "{0},{1}" -f part does please, I have seen this around before but don't really understand why it's there and would rather know for sure than guess please.
"{0},{1}" -f is just a format operator. so basically saying format the the output as "$strCurrentUser,$lastChannel" then add it to the end of the file. Since your file is just a two column comma separated csv it should match up just fine. If you wanted it in different format like var1-var2 you could do "{0}-{1}" -f see this ms article for a bit more info if you like: social.technet.microsoft.com/wiki/contents/articles/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.