Dictionaries/Dict.Linux/Dict.Linux.psm1

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<#
 
  ###### ####### ## ## ######## ## ## ######## ######## ########
 ## ## ## ## ### ### ## ## ## ## ## ## ## ##
 ## ## ## #### #### ## ## ## ## ## ## ## ##
 ## ## ## ## ### ## ######## ## ## ## ###### ########
 ## ## ## ## ## ## ## ## ## ## ## ##
 ## ## ## ## ## ## ## ## ## ## ## ## ##
  ###### ####### ## ## ## ####### ## ######## ## ##
 
#>


<#
.SYNOPSIS
Know if computer was booted using old legacy BIOS mode or new UEFI.
 
.DESCRIPTION
To make next boot efficient, we need to know in what mode the computer booted to this stage.
This function will tell us that.
If every tests fail, or if we can't determined the boot mode, it default to "BIOS" mode.
 
.EXAMPLE
$bootMode = Get-ComputerBootMode
 
.NOTES
General notes
#>

function Get-ComputerBootMode {
    [CmdletBinding()]
    [OutputType([String])]
    Param (
        # [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][string]$string
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        $bootMode = "BIOS"
        if (Test-DirExist "/sys/firmware/efi") { $bootMode = "UEFI" }

        return $bootMode
    }

    End {
        Write-LeaveFunction
    }
}

function Get-ComputerFirmwareType {
    [CmdletBinding()]
    [OutputType([String])]
    Param (
        [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][switch]$Localhost
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        if ($Localhost) {
            return Get-ComputerBootMode
        }
    }

    End {
        Write-LeaveFunction
    }
}

<#
.SYNOPSIS
Configure computer to boot on user-asked partition
 
.DESCRIPTION
This function configure the boot flag on the correct partition
 
.PARAMETER Force
Force things
 
.PARAMETER Label
Label of the partition to configure
 
.PARAMETER MountPoint
Mountpoint of the target partition
 
.PARAMETER Disk
Disk device name of the partition
 
.PARAMETER PartNum
Partition number (can be several strings char since nvme disks. e.g. 'p1')
 
.PARAMETER Device
Full device adress like in '/dev/sda1'
 
.NOTES
General notes
#>

function Set-ComputerNextBootBIOS {
    [CmdletBinding()]
    [OutputType([Boolean])]
    Param (
        [switch]$Force,
        [Parameter(Mandatory = $true, ParameterSetName = 'LABEL')][string]$Label,
        [Alias('Volume', 'Letter', 'DriveLetter')]
        [Parameter(Mandatory = $true, ParameterSetName = 'MOUNTPOINT')][string]$MountPoint,
        [AllowNull]
        [Parameter(Mandatory = $false, ParameterSetName = 'DISKPART')][string]$Disk,
        [Parameter(Mandatory = $true, ParameterSetName = 'DISKPART')][string]$PartNum,
        [Parameter(Mandatory = $true, ParameterSetName = 'DEVICE')][string]$Device
        )
    Begin {
        Write-EnterFunction
    }

    Process {
        $diskpart = $null
        switch ($PSCmdlet.ParameterSetName) {
            'LABEL' {
                $diskpart = blkid -t PARTLABEL="$Label" -o device
            }
            'MOUNTPOINT' {
                $diskpart = mount | grep " on $MountPoint " | awk '{ printf $1 }' | head -n 1
            }
            'DISKPART' {
                $disk = $Disk.Split('/')[-1]
                $d = find /dev -name "$disk"
                $diskpart = "$d$PartNum"
            }
            'Device' {
                $diskpart = $Device
            }
        }
        if ([string]::IsNullOrEmpty($diskpart)) { return $false }

        $rc = Execute-Command -exe parted -args "-s $diskpart set boot on"
        return $rc
    }

    End {
        Write-LeaveFunction
    }
}

<#
 
 ######## ### ###### ## ## ###### ###### ## ## ######## ######## ## ## ## ######## ########
    ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
    ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
    ## ## ## ###### ##### ###### ## ######### ###### ## ## ## ## ## ###### ########
    ## ######### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
    ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
    ## ## ## ###### ## ## ###### ###### ## ## ######## ######## ####### ######## ######## ## ##
 
#>


<#
.SYNOPSIS
Create a new folder in the Operating System's task scheduler
 
.DESCRIPTION
Create a new folder in the Operating System's task scheduler daemon.
 
.PARAMETER FolderName
The name of the folder to create
 
.PARAMETER Root
An optional root folder to be the parent of the folder to create
 
.EXAMPLE
New-PwShFwScheduledTaskFolder -FolderName "MyDaemon"
 
.NOTES
On linux, this function just create an empty file under /etc/cron.d by default
#>

function New-PwShFwScheduledTaskFolder {
    [CmdletBinding()]
    [OutputType([Boolean])]
    [Alias('New-CronTaskFile')]
    Param (
        [Alias('Name')]
        [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][string]$FolderName,
        [string]$Root = '/etc/cron.d'
    )
    Begin {
        Write-EnterFunction
    }

    Process {
         $ErrorActionPreference = "stop"
        Try {
           $null = New-Item -Path $Root -Name $FolderName -ItemType File
        } Catch {
        } Finally {
            $ErrorActionPreference = "continue"
        }

        # test to return correct value
        if (Test-FileExist "$Root/$FolderName") {
            return $true
        } else {
            return $false
        }
    }

    End {
        Write-LeaveFunction
    }
}

<#
.SYNOPSIS
Create a new scheduled task / cron job
 
.DESCRIPTION
New-PwShFwScheduledTask function is a cross-platform wrapper for Scheduled Task / cron jobs.
It creates a full scheduled task / cron job within one command whatever the underlying OS is.
 
.PARAMETER Folder
Optional Folder name into which to create the task
 
.PARAMETER Name
Name of the task
 
.PARAMETER Command
Command or script to run
 
.PARAMETER Parameters
Arguments to pass to the command
 
.PARAMETER Description
Optional description of the task
 
.PARAMETER Username
Username to use to launch the task
default = root
 
.PARAMETER Minutes
Set the minutes number to trigger job (can be '*' to run every minutes).
Default = '*'
 
.PARAMETER Hour
Set the hour number to trigger job (can be '*' to run every hours).
Default = '*'
 
.PARAMETER DayOfMonth
Set the day of the month number to trigger job (can be '*' to run every days).
Default = '*'
 
.PARAMETER DayOWeek
Set the day of week number to trigger job (can be '*' to run every days of week).
Default = '*'
 
.PARAMETER Month
Set the month number to trigger job (can be '*' to run every months).
Default = '*'
 
.PARAMETER Year
Set the year number to trigger job (can be '*' to run every years).
Default = '*'
 
.PARAMETER EveryMinutes
Equals to -Minute '*'
 
.PARAMETER Hourly
Equals to -Minute 0 -Hours '*'
 
.PARAMETER Daily
Equals to -Minute 0 -Hours 0 -DayOfMonth '*'
 
.PARAMETER Weekly
Equals to -Minute 0 -Hours 0 -DayOfWeek 1 -Week '*'
 
.PARAMETER Monthly
Equals to -Minute 0 -Hours 0 -DayOfMonth 1 -Month '*'
 
.PARAMETER Yearly
Equals to -Minute 0 -Hours 0 -DayOfMonth 1 -Month 1 -Year '*'
 
.PARAMETER AtStartup
Trigger job on system startup
 
.PARAMETER AtLogon
Trigger job on session logon
 
.EXAMPLE
An example
 
.NOTES
General notes
#>

function New-PwShFwScheduledTask {
    [CmdletBinding(DefaultParameterSetName = 'EXACT_TRIGGER')]
    [OutputType([String])]
    Param (
        [string]$Folder,
        [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][string]$Name,
        [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][string]$Command,
        [Parameter(Mandatory = $false, ValueFromPipeLine = $true)][string]$Parameters,
        [string]$Description,
        [string]$Username = $env:USER,
        [Parameter(ParameterSetName = 'EXACT_TRIGGER')][string]$Minute = '*',
        [Parameter(ParameterSetName = 'EXACT_TRIGGER')][string]$Hour = '*',
        [Parameter(ParameterSetName = 'EXACT_TRIGGER')][string]$DayOfMonth = '*',
        [Parameter(ParameterSetName = 'EXACT_TRIGGER')][string]$DayOfWeek = '*',
        # [Parameter(ParameterSetName = 'EXACT_TRIGGER')][string]$Week = '*',
        [Parameter(ParameterSetName = 'EXACT_TRIGGER')][string]$Month = '*',
        [Parameter(ParameterSetName = 'EXACT_TRIGGER')][string]$Year = '*',
        [Parameter(ParameterSetName = 'ALIAS_TRIGGER')][switch]$EveryMinutes,
        [Parameter(ParameterSetName = 'ALIAS_TRIGGER')][switch]$Hourly,
        [Parameter(ParameterSetName = 'ALIAS_TRIGGER')][switch]$Daily,
        [Parameter(ParameterSetName = 'ALIAS_TRIGGER')][switch]$Weekly,
        [Parameter(ParameterSetName = 'ALIAS_TRIGGER')][switch]$Monthly,
        [Parameter(ParameterSetName = 'ALIAS_TRIGGER')][switch]$Yearly,
        [Parameter(ParameterSetName = 'ALIAS_TRIGGER')][switch]$AtStartup,
        [Parameter(ParameterSetName = 'ALIAS_TRIGGER')][switch]$AtLogon
    )
    Begin {
        Write-EnterFunction
        $Filename = $Folder ? "/etc/cron.d/$Folder-$Name" : "/etc/cron.d/$Name"
    }

    Process {
        if ([string]::IsNullOrEmpty($Description)) { $Description = "$Name - $Command $Parameters" }
        $trigger = ""
        switch ($PSCmdlet.ParameterSetName) {
            'EXACT_TRIGGER' {
                $trigger = "$Minute $Hour $DayOfMonth $Month $DayOfWeek"
            }
            'ALIAS_TRIGGER' {
                if ($EveryMinutes)    { $trigger = "* * * * *" }
                if ($Hourly)        { $trigger = "0 * * * *" }
                if ($Daily)            { $trigger = "0 0 * * *" }
                if ($Weekly)        { $trigger = "0 0 * * 0" }
                if ($Monthly)        { $trigger = "0 0 1 * *" }
                if ($Yearly)        { $trigger = "0 0 1 1 *" }
                if ($AtStartup)        { $trigger = "@reboot" }
            }
        }

        $task = @"
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# $Description
$trigger $Username $Command $Parameters
"@

        try {
            $task | Out-File -FilePath $Filename -Encoding utf8
            $rc = $?
        } catch {
            eerror $_
            $rc = $false
        }

        return $rc
    }

    End {
        Write-LeaveFunction
    }
}