Last active
September 23, 2021 20:28
-
-
Save fernandozamoraj/d709a92ad2a8f695c510a6f880fac744 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Added comments to the script |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This sample power shell was written purely from the motivation of learning powershell | |
# As there were some goals in mind here is a list of them | |
# 1. Basic variable declarations | |
# 2. Creating functions - this goal will require further digging since I could get the parse args function working | |
# 3. How to get and parse command line arguments - I was able to parse the arguments and use them | |
# 4. How write output to the display | |
# 5. How to set color for output on the display | |
# 6. How to read data from a file | |
# 7. How to write data to a file | |
# 8. How to do condition logic - if, gt, lt and -and and -or | |
# 9. How to do for loops | |
# 10. How to do some basic string manipulation: split, contains, indexOf | |
# | |
# Have fun and enjoy | |
$verbose = $false | |
$inputFile = "" | |
$outputFile = "" | |
#local funtion | |
#displays usage if it can properly parse the values | |
function Display-Usage{ | |
Write-Host "Usage: " -fore red | |
Write-Host "$($MyInvocation.ScriptName) --input-file=myfile.txt --outputfile=outfile.txt" -fore yellow | |
Write-Host "Options: " -fore green | |
Write-Host "-c display to screen" | |
Write-Host "-v display debug info" | |
} | |
#local function to process the files | |
function Run-Process($inputFile, $outputFile){ | |
Write-Host "Processing Files $($inputFile) $($outputFile)" -fore green | |
$file_data = Get-Content $inputFile | |
$records = New-Object -TypeName 'System.Collections.ArrayList'; | |
For($i = 0; $i -lt $file_data.Length; $i++){ | |
WriteConsole "Readline line from file $($file_data[$i]) wtf!!!" | |
$tokens = $file_data[$i] -split "," | |
$name = $tokens[0] | |
$hours = [int]$tokens[1] | |
$rate = [int]$tokens[2] | |
[int]$wages = $rate * $hours | |
$line = "$($name),$($hours),$($rate),$($wages)" | |
Write-Host "THIS FRIIGGILE line $($line) WTF!!!!" | |
$records.Add($line) | |
WriteConsole($line) | |
Write-Host "THIS SECOND FRIIGGILE line $($line) WTF!!!!" | |
} | |
Out-File -FilePath $outputFile -InputObject $records -Encoding ASCII -Width 50 | |
} | |
#writes to Console only if $verbose got tripped up to true -v | |
function WriteConsole($line){ | |
if($verbose -eq $true){ | |
Write-Host $line | |
} | |
} | |
#it does not work | |
function Parse-Args($args){ | |
Write-Host "Parse-Args called $($args.Count)" | |
For( $i =0; $i -lt $args.count; $i++){ | |
Write-Host $args[$i] | |
if($args[$i] -eq "-v"){ | |
$verbose = $true | |
} | |
else{ | |
if($args[$i].Length -lt 4 -or | |
$args[$i].IndexOf("--") -ne 0 -or | |
$args[$i].Contains("=") -eq $false){ | |
Write-Warning "Invalid Argument: $($args[$i])" | |
continue | |
} | |
$tokens = $args[$i] -split "=" | |
if($tokens[0] -eq "--inputfile"){ | |
$inputFile = $tokens[1] | |
} | |
if($tokens[0] -eq "--outputfile"){ | |
if($verbose){ | |
} | |
WriteConsole "output file found $($tokens[1])" | |
$outputFile = $tokens[1] | |
} | |
} | |
} | |
} | |
#$args is a variable that is available with every | |
#script and is what came through the command line | |
WriteConsole "$($args.count)" | |
#min number o arguments is 2 --inputfile and --outputfile | |
if($args.count -lt 2){ | |
Write-Host "args.count $($args.count)" | |
Display-Usage | |
} | |
else{ | |
Write-Host "Parsing Args" | |
Write-Host "Parse-Args called $($args.Count)" | |
For( $i =0; $i -lt $args.count; $i++){ | |
Write-Host $args[$i] | |
if($args[$i] -eq "-v"){ | |
$verbose = $true | |
} | |
else{ | |
#if the argument has length less than 4 e.g. --a=b ... | |
#should probably be 5 but 4 is enough for use to verify the -- and = | |
//take good notice of the less than and or operators | |
//as well as the equal and not equal | |
//also $false is defined by powershell | |
if($args[$i].Length -lt 4 -or | |
$args[$i].IndexOf("--") -ne 0 -or | |
$args[$i].Contains("=") -eq $false){ | |
Write-Warning "Invalid Argument: $($args[$i])" | |
continue | |
} | |
#example arg would be --inputfile=myfile.txt | |
#so.. split left from the right side or the = charater | |
$tokens = $args[$i] -split "=" | |
if($tokens[0] -eq "--inputfile"){ | |
WriteConsole "input file found $($tokens[1])" | |
$inputFile = $tokens[1] | |
} | |
if($tokens[0] -eq "--outputfile"){ | |
WriteConsole "output file found $($tokens[1])" | |
$outputFile = $tokens[1] | |
} | |
} | |
} | |
Write-Host "Parsed Args $($verbose) $($inputFile) $($outputFile)" | |
if(inputFile -eq ""){ | |
Display-Usage | |
} | |
else{ | |
Run-Process $inputFile $outputFile | |
} | |
} | |
if( $args.count -gt 0){ | |
WriteConsole "Hello, $($args[0])!" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made gist public