Fix removing all lines from crontab
This commit is contained in:
parent
0be252ec0e
commit
ab5bda18e0
|
@ -1,9 +1,26 @@
|
|||
# Use the crontab module available at ./modules/CronTab
|
||||
|
||||
if (!($env:PSMODULEPATH -split ';' | Where-Object { $_.StartsWith($PSScriptRoot) }))
|
||||
{
|
||||
$env:PSMODULEPATH += ";$PSScriptRoot/modules"
|
||||
}
|
||||
|
||||
Import-Module CronTab
|
||||
|
||||
# Get the existing cron jobs
|
||||
Get-CronJob
|
||||
|
||||
# New cron job to clean out tmp every day at 1am
|
||||
New-CronJob -Command 'rm -rf /tmp/*' -Hour 1
|
||||
|
||||
# New cron job to start a build
|
||||
New-CronJob -Command 'powershell -c "cd ~/src/PowerShell; ipmo ./build.psm1; Start-PSBuild"' -Hour 2 -DayOfWeek 1-5
|
||||
|
||||
# Sort them by data
|
||||
|
||||
# Create a new one using a DateTime object
|
||||
Get-CronJob | Sort-Object Command
|
||||
|
||||
# Show in bash that the new cron job exists
|
||||
crontab -l
|
||||
|
||||
# Remove a cron job
|
||||
Get-CronJob | Where-Object { $_.Command -match '^powershell.*' } | Remove-CronJob
|
||||
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
|
||||
using namespace System.Collections.Generic
|
||||
|
||||
$crontabcmd = "/usr/bin/crontab"
|
||||
|
||||
class CronJob {
|
||||
|
@ -15,7 +18,7 @@ function Get-CronTab ([String] $user) {
|
|||
$crontab = Invoke-CronTab -user $user -arguments "-l" -noThrow
|
||||
if ($crontab -is [System.Management.Automation.ErrorRecord]) {
|
||||
if ($crontab.Exception.Message.StartsWith("no crontab for ")) {
|
||||
$crontab = $null
|
||||
$crontab = @()
|
||||
}
|
||||
else {
|
||||
throw $crontab.Exception
|
||||
|
@ -85,7 +88,7 @@ function Remove-CronJob {
|
|||
process {
|
||||
|
||||
[string[]] $crontab = Get-CronTab -user $UserName
|
||||
[string[]] $newcrontab = $null
|
||||
$newcrontab = [List[string]]::new()
|
||||
$found = $false
|
||||
|
||||
foreach ($line in $crontab) {
|
||||
|
@ -93,7 +96,7 @@ function Remove-CronJob {
|
|||
if ((Compare-object $cronjob.psobject.properties $Job.psobject.properties) -eq $null) {
|
||||
$found = $true
|
||||
} else {
|
||||
$newcrontab += $line
|
||||
$newcrontab.Add($line)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue