Check VISA / Unpack .zip / Calculate age

Dear friends,

 

I have a training of 10 questions of PS for beginners. I’ve managed to solve 6 out of 10 but the following questions I couldn’t figure out, can anyone help?

<!–nextpage–>

  1. Create a function to check an account number (VISA)

  2. Create a function to calculate your age in seconds, minutes and hours.

  3. Unpack the library.zip file on the commandline. This is a folder containing data from a large collection of e-books. There is also a CSV file (comma seperated values) with an overview. Open it on the commandline:

  4. Try to make the following exercises based on the folder or on the CSV-file:Afbeelding2.png

  • Count all the books in this collection.
  • Select all JK Rowling's books
  • Select all books of a given author, write this overview to a text file.
  • Authors are written in different ways. Add all JK Rowling's folders together.
  • Find the largest and smallest cover image
  • Select the five most occurring authors.
  • Give the average number of books per author.
  • Find all the youth books.
  • Find the book that is about special gifts and open its cover.
  • Give the number of books per publisher
  • Give the average number of books per publisher
  • Move all books from an in-given author to a new folder
  • Give all books where a given word appears in the title.
  • Train yourself with this collection…

<hr />

Any help would be appreciated,

 

Thank you :slight_smile:

What have you tried? Maybe if I could see an attempt at a question I could give you some assistance.

[quote quote=207984]What have you tried? Maybe if I could see an attempt at a question I could give you some assistance.

[/quote]

Dear Mike,

 

For questions 8 & 9 I just solved them now!

 

But for question 7 (VISA) Verification I thought of using a “matching command” that goes ‘maybe’ like this:

([regex]::Matches($StringCount, "[####-####-####-####]" ))
 

Thanks for your reply :slight_smile:

I think you are on the right track for question 7. I would definitely use RegEx to check the VISA number. I don’t pretend to know the rules for those numbers but I’m sure they can be represented in RegEx (simple example below). I try to stick with PowerShell cmdlets/operators instead of .NET classes directly so I might do something like this instead:

function CheckVisa ($number)
{
   $regex = "^4[0-9]{12}(?:[0-9]{3})?$"
   If ($number | Select-String -Pattern $regex) {
      return $true
   }
   return $false
}

#or with -match operator below
function CheckVisa ($number)
{
    $regex = "^4[0-9]{12}(?:[0-9]{3})?$"
    If ($number -match $regex) {
        return $true
    }
    return $false
}

[quote quote=208125][/quote]

Dear Mike,

Sorry for my questions I’m really a newbie in progress.

 

Your idea is even better than mine, I thought of using the “IF” rules but I wasn’t sure how to get started now that you suggest using it, I have two questions to get a long with the function you made:

1) You wrote ((( $regex = “^4[0-9]{12}(?:[0-9]{3})?$” )))

Can I add also MasterCard - AmericanExpress - DinersClub next to the VISA regex? like the following

$regex = "^4[0-9]{12}(?:[0-9]{3})?$","^5[1-5][0-9]{14}$","^3[47][0-9]{13}$","^3(?:0[0-5]|[68][0-9])[0-9]{11}$"
And I'm not sure if I can add something to also verify if expiration date and ccv where also added or does the regex already include that?

 

2) If I want to link that function with a script that scans for example a ‘.txt’ file!

Should I be using a “Get-content | Where-Object” method? or Get-ChildItem?

to clarify my example let’s say I want to use the function on a ‘.txt’ file which contains the following

345-2354-6245-2313

1224-3523-3562-3444

2351-4365-3421-35

2632-34242-1434-1345

1352-1352-3523-4632

How can I use your function to verify the correct ones from the wrong ones? giving that my ".txt" file is located on the desktop "./Desktop/Accounts.txt"

 

Thanks a million for having the time to help me out !

It is really appreciated :slight_smile:

 

Regards,

  1. For the Select-String version, the -pattern parameter will accept an array of strings so that function would work. If you tried the -match operator route, you would have to iterate through the different expression or re-write the regex to allow for each type. Recommend looking at select-string https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-5.1 and about_comparison_operators https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-5.1
  2. To read the contents of a plain text file, I use the Get-Content command. By default each line of the text file will be a string element in an array, so once it is read in you can iterate over the array and call the function CheckVisa in each iteration. Recommend looking at Get-Content https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Management/Get-Content?view=powershell-5.1 and about_foreach https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-5.1
Here is an example:
$myFile = ".\Desktop\accounts.txt"
$CreditCards = Get-Content -Path $myFile
foreach ($Card in $CreditCards)
{
   If (CheckVisa -number $Card) { "$Card is good" }
   else {"$Card is not valid"}
}

Dear Mike,

I have tested the complete script after merging the Get-Content command with the previously suggested function, but it gave “$Card is not valid” for all of the random account numbers in the text file even though a few were correctly matching the regex :confused:

I thought it could be the spacing between the numbers or the dashes ’ - ’ or such. But it was not the case as I added more different and random numbers with different structures but it kept giving “NOT VALID” for all of them.

Otherwise the function works perfectly, I still believe it is something that has to do the the regex here’s how I copy pasted the code to the “.PS1” file that I ran every time to test the credit card numbers.

# Check-VISA Function #
#############################################
functionCheckVisa ($number)
{
$regex = "^4[0-9]{12}(?:[0-9]{3})?$"
If ($number -match $regex) {
return $true
}
return $false
}
#############################################
# Targeted File #
#############################################
$myFile = "./Desktop/Accounts.txt"
$CreditCards = Get-Content -Path $myFile
foreach ($Card in $CreditCards)
{
If (CheckVisa -number $Card) { "$Card is valid" }
else {"$Card is not valid"}
}
#############################################
I've tried to change the regex to the MasterCard regex and included my personal MC account in the text file just to test if that would work. But no luck either :/ no errors in the coding but result not as expected.
Side testing:
I am using what you inspired me to do but this time involving the Luhn Algorithm to validate as well
here's the script
$myFile = "./Desktop/Accounts.txt"
$CreditCards = Get-Content -Path $myFile
function Test-LuhnNumber([int[]]$digits){
[int]$sum=0
[bool]$alt=$false
for($i = $digits.length - 1; $i -ge 0; $i--){
if($alt){
$digits[$i] *= 2
if($digits[$i] -gt 9) { $digits[$i] -= 9 }
}
$sum += $digits[$i]
$alt = !$alt
}
return ($sum%10) -eq 0
}
foreach ($Card in $CreditCards)
{
If (Test-LuhnNumber -number $Card) { "$Card is valid" }
else {"$Card is not valid"}
}
But this one does the complete opposite it listed everything as VALID haha? I am losing my mind.
Sorry to bother you with this !
Regards,
<!--more-->

Agree your issue is RegEx not PowerShell. The RegEx I provided in my example “^4[0-9]{12}(?:[0-9]{3})?$” was very simple here’s the breakdown:

^ Start of string
4 Literal number four
[0-9] {12} Any number 0-9 twelve times
(?: Positive look ahead
[0-9]{3} Any number 0-9 three times
)? Match 1 or 0 times
$ End of string

Your sample data has dashes in them and none of them began with a 4. I don’t use RegEx often so every time I find the need to use it, I have to re-learn it. Does your training provide the rules for the credit card numbers? If so, I would focus on creating the appropriate RegEx to match those rules. Also, consider passing numbers without dashes unless those are important to the validity of the number. To remove the dashes in a string, PowerShell has a really simple operator -replace that would help. Example:

"345-2354-6245-2313" -replace "-",""

 

[quote quote=208773]The RegEx I provided in my example “^4[0-9]{12}(?:[0-9]{3})?$” was very simple here’s the breakdown:


(?: Positive look ahead
…[/quote]

Shouldn’t there be an equal sign involved in a poistive look ahead? If I remember right your syntax represents a non capturing group.

Some info to read up:
https://www.regular-expressions.info/lookaround.html
https://www.regular-expressions.info/brackets.html

You’re right Olaf. The correct description is “Grouping-only parenthesis, no capturing” for (?: …) Not “Positive look ahead” which would be (?= …)

The purpose of this in the example is that apparently (simple google search) Visa card numbers could be 13 or 16 digits so the (?:[0-9]{3})? allows for 16 digits.

I really suck at RegEx because I don’t find it all that intuitive and I don’t use it often enough.

Obviously you don’t. :wink: … it’s not important to know how it’s named. It’s important to know how it works. :wink: :smiley: