en:powershell:start
                Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:powershell:start [2021/03/07 22:31] – lonclegr | en:powershell:start [2021/03/16 21:48] (current) – lonclegr | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| ===== Basics ===== | ===== Basics ===== | ||
| - | According to me, the first thing we need to deal with is CSV file. First let's read one CSV file, loop over it. | + | To me, the first thing we need to learn is to deal with is CSV file. First let's read a CSV file, loop across the lines. | 
| Here is an example of CSV input file. | Here is an example of CSV input file. | ||
| Line 11: | Line 11: | ||
| abc123, | abc123, | ||
| def123, | def123, | ||
| + | ghi123, | ||
| </ | </ | ||
| Line 17: | Line 18: | ||
| abc123, | abc123, | ||
| def123, | def123, | ||
| + | ghi123, | ||
| </ | </ | ||
| - | <file powershell | + | <file powershell | 
| Import-Csv -Path input.csv | foreach { | Import-Csv -Path input.csv | foreach { | ||
| - |  | + |  | 
| - | $currentLine = $_ | + | $currentLine = $_ | 
| - | + | ||
| - | # deal with columns | + | |
| - | $sam = $currentLine.Samaccountname | + | |
| - | + | ||
| - | # let's see if the user exists into AD | + | |
| - | try { | + | |
| - | $user = Get-ADUser -identity $sam | + | |
| - | # user found | + | |
| - | } catch { | + | |
| - | # user not found, maybe user is disabled for instance ? | + | |
| - | } | + | |
| - | } | + | # deal with columns | 
| + | $mail = $currentLine.Mail | ||
| + | |||
| + | # test mail domain name | ||
| + | # we add a property with the result of our test | ||
| + | if ($mail -match " | ||
| + | $currentLine | Add-Member -MemberType NoteProperty -Name " | ||
| + |  | ||
| + | $currentLine | Add-Member -MemberType NoteProperty -Name " | ||
| + | } | ||
| + | # return updated currentLine with new column | ||
| + | $currentLine | ||
| + | } | | ||
| + | # export result line by line to CSV | ||
| + | # -NoTypeInformation prevents metadata from being exported | ||
| + | Export-Csv -Path preCheck.csv -NoTypeInformation | ||
| </ | </ | ||
| + | |||
| + | This script will output CSV file with a new column. | ||
| + | |||
| + | <file csv preCheck.csv> | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | </ | ||
| + | |||
| + | <csv> | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | </ | ||
en/powershell/start.1615174302.txt.gz · Last modified:  by lonclegr
                
                