forked from Vector-BCO/PowerShell.Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelfVerification.ps1
More file actions
94 lines (89 loc) · 3.94 KB
/
SelfVerification.ps1
File metadata and controls
94 lines (89 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
$ErrorActionPreference = 'Stop'
Function Get-MDHash {
param (
[string]$Question,
[string]$Answer
)
$string = $Question + $Answer
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$utf8 = New-Object -TypeName System.Text.UTF8Encoding
[System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($string)))
}
function Get-Answer {
param (
[string]$question,
[string[]]$answers,
[string]$correctAnswer
)
$answerFound = $false
do {
Clear-Host
$questionNumber = 0
Write-Host "$question"
Write-Host '-----------------------------'
foreach ($answer in $answers){
$questionNumber++
Write-Host "[$questionNumber] $($answer -split '\n' | Where-Object {$_ -notmatch '^\s*$'} | Out-String)"
}
$answerNumber = Read-Host -Prompt "Select answer 1 - $questionNumber"
if ($answerNumber -in @(1..$questionNumber)) {
$answerFound = $true
} else {
Write-Host "Provided incorrect answer. Please try again" -foreground Yellow
Start-Sleep -s 2
}
} until ( $answerFound )
if ($answerFound){
if (! [string]::IsNullOrEmpty($correctAnswer)){
[psobject]@{
isAnswerCorrect = $correctAnswer -eq $(Get-MDHash -Question $question -Answer $answers[$answerNumber - 1]);
question = $question;
answer = $answers[$answerNumber - 1]
}
} else{
$answers[$answerNumber - 1]
}
}
}
try{
$QuestionPath = "$PSScriptRoot\questions.json"
if (!(Test-Path $QuestionPath)){
$TmpPath = New-TemporaryFile
$url = 'https://raw.githubusercontent.com/Vector-BCO/PowerShell.Learning/master/questions.json'
Start-BitsTransfer -Source $url -Destination $TmpPath
$QuestionPath = $TmpPath
}
if (Test-Path $QuestionPath){
$QuestionsWithLanguages = (Get-Content $QuestionPath -Encoding UTF8 | ConvertFrom-Json).Languages
$languages = $QuestionsWithLanguages | Select-Object -ExpandProperty Language -Unique
$SelectedLanguage = $languages | Select-Object -First 1
if (($languages | Measure-Object).count -gt 1){
Get-Answer -Qusetion "Select language from the list" -Answers $languages
}
$QuestionsWithLanguages = $QuestionsWithLanguages | Where-Object {$_.Language -eq $SelectedLanguage} | Select-Object -ExpandProperty questionCategories
$answers = @()
foreach ($Category in $QuestionsWithLanguages){
$CategoryName = $Category.category
Write-Host "Category started: '$CategoryName'"
foreach ($question in $Category.questions){
$ReceivedAnswer = Get-Answer -question $Question.question -answers $Question.answers -correctAnswer $Question.correctAnswer
if ($ReceivedAnswer.isAnswerCorrect){
Write-Host "Correct answer" -ForegroundColor Green
} else {
Write-Host "Incorrect answer" -ForegroundColor Red
}
$answers += $ReceivedAnswer
Start-Sleep -s 2
}
}
$answers = $answers | Foreach-Object {$_ | Select-Object @{n='question'; e={$_.question}}, @{n='answer'; e={$_.answer}}, @{n='IsAnswerCorrect'; e={$_.isAnswerCorrect}}}#, @{n='hash'; e={$_.hash}}}
$correctAnswerPercentage = ($($answers | Where-Object {$_.IsAnswerCorrect -eq $true} | Measure-Object).count * 100) / $($answers | Measure-Object).count
$correctAnswerPercentage = [math]::Round($correctAnswerPercentage, 2)
Write-Host "Correct answers: $correctAnswerPercentage%"
$answers | Out-GridView
} else {
Throw "Questions.xml not be found, place questions.xml from GitHub to the same folder as this script is."
}
} Catch {
Write-Host "[SelfVerification] Script failed with error: $_"
}