-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRaffle.ps1
69 lines (54 loc) · 2.15 KB
/
Raffle.ps1
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
# Import the ImportExcel module
# If not installed, install it using `Install-Module -Name ImportExcel`
# Get the directory of the current script
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
# Path to the Excel file in the same directory as the script
$excelPath = Join-Path -Path $scriptPath -ChildPath "raffle.xlsx"
# Check if the Excel file exists
if (-not (Test-Path $excelPath)) {
Write-Host "Error: The file raffle.xlsx does not exist in the script's directory." -ForegroundColor Red
exit
}
# Import the data from the Excel file
$data = Import-Excel -Path $excelPath
# Create a list to hold all tickets
$allTickets = @()
# Loop through the rows of the Excel file
foreach ($row in $data) {
$name = $row.Name # Column 1: Name
$ticketCount = $row.Tickets # Column 2: Number of tickets purchased (now "Tickets")
# Skip rows with missing or invalid data without warning
if (-not $name -or -not $ticketCount -or $ticketCount -le 0) {
continue
}
# Add the name to the ticket list as many times as the number of tickets
for ($i = 1; $i -le $ticketCount; $i++) {
$allTickets += $name
}
}
# Draw a random winner from the list
if ($allTickets.Count -eq 0) {
Write-Host "Error: No tickets available for the draw. Please check the Excel file." -ForegroundColor Red
exit
}
$winner = $allTickets | Get-Random
# Get the current date and time in German format
$currentDateTime = Get-Date -Format "dd.MM.yyyy HH:mm"
# Create the results data with English column names
$results = @(
[PSCustomObject]@{
DateTime = $currentDateTime
Winner = $winner
}
)
# Append or create results in the Excel file
if (Test-Path $excelPath) {
$results | Export-Excel -Path $excelPath -WorksheetName "Results" -Append
} else {
$results | Export-Excel -Path $excelPath -WorksheetName "Results"
}
# Release potential locks on the file by closing it properly
Start-Sleep -Seconds 1
# Output the winner and draw time in German format to the console
Write-Host "Winner: $winner"
Write-Host "Winner drawn at $currentDateTime"