User Tools

Site Tools


en:powershell:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:powershell:start [2021/03/07 22:22] lonclegren: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 CSV file, loop across the lines.
  
 Here is an example of CSV input file. Here is an example of CSV input file.
  
 +<csv>
 +Samaccountname,Mail
 +abc123,abc123@domain.com
 +def123,def123@domain.com
 +ghi123,ghi123@test.com
 +</csv>
  
 +<file csv input.csv>
 +Samaccountname,Mail
 +abc123,abc123@domain.com
 +def123,def123@domain.com
 +ghi123,ghi123@test.com
 +</file>
  
-<file powershell csv.ps1>+ 
 +<file powershell csvLevel1.ps1>
 Import-Csv -Path input.csv | foreach { Import-Csv -Path input.csv | foreach {
  
-  $currentLine = $_+    # save current object into a local variable 
 +    $currentLine = $_
  
-}+    # deal with columns 
 +    $mail = $currentLine.Mail 
 + 
 +    # test mail domain name 
 +    # we add a property with the result of our test 
 +    if ($mail -match "domain.com$") { 
 +        $currentLine | Add-Member -MemberType NoteProperty -Name "domainCheck" -Value "valid" 
 +    else { 
 +        $currentLine | Add-Member -MemberType NoteProperty -Name "domainCheck" -Value "invalid" 
 +    } 
 +    # 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
 </file> </file>
 +
 +This script will output CSV file with a new column.
 +
 +<file csv preCheck.csv>
 +"Samaccountname","Mail","domainCheck"
 +"abc123","abc123@domain.com","valid"
 +"def123","def123@domain.com","valid"
 +"ghi123","ghi123@test.com","invalid"
 +</file>
 +
 +<csv>
 +"Samaccountname","Mail","domainCheck"
 +"abc123","abc123@domain.com","valid"
 +"def123","def123@domain.com","valid"
 +"ghi123","ghi123@test.com","invalid"
 +</csv>
en/powershell/start.1615173777.txt.gz · Last modified: 2021/03/07 22:22 by lonclegr