-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_startup.ps1
156 lines (124 loc) · 3.27 KB
/
custom_startup.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
###
# SCRIPT VARIABLES
###
$STARTUP_DIR = [IO.Path]::Combine($env:OneDrive, 'Startup' )
$DATETIME_STR = Get-Date -UFormat '%Y%m%d_%H%M%S_%Z'
$LOGFILE = "${DATETIME_STR}_${env:COMPUTERNAME}.txt"
$LOGPATH = [IO.Path]::Combine($STARTUP_DIR, 'logs', $LOGFILE)
$INDENT_INCREMENT = 2
$SH = New-Object -ComObject WScript.Shell # shell object needed for evaluating lnk target
$PAUSE = 5
New-Variable -Scope global -Name INDENT -Value '' #globally writeable
# RUNTIME OPTIONS
$VERBOSE = $true
$DRYRUN = $false
#$DRYRUN = $true
###
# FUNCTIONS
###
function Log {
param(
[Parameter(Mandatory = $true)]
[string]$msg,
[Parameter()]
[string]$logfile = $LOGPATH
)
Add-Content -Path $logfile -Value $msg
}
function Info {
param(
[Parameter(Mandatory = $true)]
[string]$msg
)
$newmsg = "[INFO] ${INDENT}${msg}"
Log $newmsg
if ($VERBOSE) {
Write-Host $newmsg
}
}
function Log-F-Enter {
$fname = (Get-Variable MyInvocation -Scope 1).Value.MyCommand.Name
Info "> ${fname}"
$global:INDENT += ' ' * $INDENT_INCREMENT
}
function Log-F-Exit {
$global:INDENT = $INDENT.Substring(0, $INDENT.Length - $INDENT_INCREMENT)
$fname = (Get-Variable MyInvocation -Scope 1).Value.MyCommand.Name
Info "< ${fname}"
}
function Invoke-ShortcutIfExists {
param (
$lnk_path
)
Log-F-Enter
Info "lnk_path: '${lnk_path}'"
$shortcut = $SH.CreateShortcut($lnk_path)
$tgt = $shortcut.TargetPath
Info "target: $tgt"
$exists = Test-Path -Path $tgt
Info "target exists? $exists"
if ( $exists ) {
if ( $DRYRUN ) {
Info "DRYRUN enabled, skipping '${lnk_path}'"
}
else {
invoke-item $lnk_path
Start-sleep -s $PAUSE
}
}
Log-F-Exit
}
function IsBusinessHours {
Log-F-Enter
$now = Get-Date
$dayOfWeek = $now.DayOfWeek
$hourOfDay = $now.Hour
Info "DayOfWeek: '$dayOfWeek'"
Info "HourOfDay: '$hourOfDay'"
$rv = $true
if ($dayOfWeek.ToString().StartsWith('S')) {
$rv = $false
}
elseif ($hourOfDay -lt 7 -or $hourOfDay -ge 17) {
$rv = $false
}
Info "Return: ${rv}"
Log-F-Exit
return $rv
}
function Run-ShortcutsInDir {
param(
[Parameter(Mandatory = $true)]
[string]$path
)
Log-F-Enter
Info "path = '${path}'"
# must have a "\*" at the end of path when using -Exclude with gci
$search_path = ( [IO.Path]::Combine( $path, '*' ) )
gci $search_path -File -Filter *.lnk -Exclude 00* | foreach-object {
Invoke-ShortcutIfExists $_
}
Log-F-Exit
}
###
# MAIN WORK SECTION
###
# Ensure log dir exists
New-Item -ItemType Directory -Force -Path $(Split-Path -Path $LOGPATH -Parent)
# Create a hash of "directories" and "tests"
# Test should return a boolean; directory is included if "test" returns $true, otherwise it's skipped
# Passing code reference via scriptblock, see: https://stackoverflow.com/a/53147390/21010651
$dir_list = @{
Always = { $true } # curly braces make a scriptblock
Work = { IsBusinessHours } # this function will be called when the scriptblock is invoked
}
$dir_list.GetEnumerator() | ForEach-Object {
$dir = ( [IO.Path]::Combine( $STARTUP_DIR, $_.Key ) )
# Use this search path only if the scriptblock returns True
if ( $_.Value.InvokeReturnAsIs() ) {
Run-ShortcutsInDir $dir
}
else {
Info "Negative test result: skipping directory ${dir}"
}
}