/
Get-BarNight.ps1
50 lines (50 loc) · 1.93 KB
/
Get-BarNight.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
function Get-BarNight{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$BarNightFile
)
BEGIN{
Write-Verbose "[$(Get-Date)] $($MyInvocation.MyCommand) has started"
if(Test-Path $BarNightFile){
Write-Verbose "Setting variables"
$ContentsBars = Get-Content $BarNightFile
$location = Get-Location
$flag = $true
}
else{
Write-Error "$BarNightFile is not a valid file."
exit
}
}
PROCESS{
do{
Write-Verbose "Getting random bar from $BarNightFile"
$bar = Get-Random -InputObject $ContentsBars
if(Test-Path "$location\RecentBars.txt"){
Write-Verbose "Getting recent bar night locations"
$ContentsRecent = Get-Content "$location\RecentBars.txt"
if($ContentsRecent -contains $bar){
Write-Verbose "Bar night was already held at $bar in the last five(5) weeks"
} else{
Write-Verbose "Updating file holding recent bars"
Add-Content "$location\RecentBars.txt" "$bar"
if($ContentsRecent.Count -ge 5){
Get-Content "$location\RecentBars.txt" | Select-Object -Skip 1 | Set-Content "$location\RecentBars-temp.txt"
Move-Item "$location\RecentBars-temp.txt" "$location\RecentBars.txt" -Force
}
break
}
} else{
Write-Verbose "Creating $location\RecentBars.txt"
New-Item -Path "$location\RecentBars.txt" -ItemType File -Force | Out-Null
Set-Content -Path "$location\RecentBars.txt" -Value "$bar"
break
}
} while ($flag)
}
END{
Write-Verbose "[$(Get-Date)] $($MyInvocation.MyCommand) has started"
Write-Output $bar
}
}