Skip to content

Commit ec1369f

Browse files
committed
Add argument completion for -UserName
1 parent ab5bda1 commit ec1369f

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

demos/modules/CronTab/CronTab.psd1

36 Bytes
Binary file not shown.

demos/modules/CronTab/CronTab.psm1

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
using namespace System.Collections.Generic
3+
using namespace System.Management.Automation
34

45
$crontabcmd = "/usr/bin/crontab"
56

@@ -16,7 +17,7 @@ class CronJob {
1617

1718
function Get-CronTab ([String] $user) {
1819
$crontab = Invoke-CronTab -user $user -arguments "-l" -noThrow
19-
if ($crontab -is [System.Management.Automation.ErrorRecord]) {
20+
if ($crontab -is [ErrorRecord]) {
2021
if ($crontab.Exception.Message.StartsWith("no crontab for ")) {
2122
$crontab = @()
2223
}
@@ -82,8 +83,16 @@ function Remove-CronJob {
8283
#>
8384
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")]
8485
param (
85-
[Alias("u")][Parameter(Mandatory=$false)][String] $UserName,
86-
[Alias("j")][Parameter(Mandatory=$true,ValueFromPipeline=$true)][CronJob] $Job
86+
[ArgumentCompleter( { $wordToComplete = $args[2]; Get-CronTabUser | Where-Object { $_ -like "$wordToComplete*" } | Sort-Object } )]
87+
[Alias("u")]
88+
[Parameter(Mandatory=$false)]
89+
[String]
90+
$UserName,
91+
92+
[Alias("j")]
93+
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
94+
[CronJob]
95+
$Job
8796
)
8897
process {
8998

@@ -141,7 +150,12 @@ function New-CronJob {
141150
#>
142151
[CmdletBinding()]
143152
param (
144-
[Alias("u")][Parameter(Mandatory=$false)][String] $UserName,
153+
[ArgumentCompleter( { $wordToComplete = $args[2]; Get-CronTabUser | Where-Object { $_ -like "$wordToComplete*" } | Sort-Object } )]
154+
[Alias("u")]
155+
[Parameter(Mandatory=$false)]
156+
[String]
157+
$UserName,
158+
145159
[Alias("mi")][Parameter(Position=1)][String[]] $Minute = "*",
146160
[Alias("h")][Parameter(Position=2)][String[]] $Hour = "*",
147161
[Alias("dm")][Parameter(Position=3)][String[]] $DayOfMonth = "*",
@@ -188,3 +202,33 @@ function Get-CronJob {
188202
}
189203
}
190204
}
205+
206+
function Get-CronTabUser {
207+
<#
208+
.SYNOPSIS
209+
Returns the users allowed to use crontab
210+
#>
211+
[CmdletBinding()]
212+
[OutputType([String])]
213+
param()
214+
215+
$allow = '/etc/cron.allow'
216+
if (Test-Path $allow)
217+
{
218+
Get-Content $allow
219+
}
220+
else
221+
{
222+
$users = Get-Content /etc/passwd | ForEach-Object { ($_ -split ':')[0] }
223+
$deny = '/etc/cron.deny'
224+
if (Test-Path $deny)
225+
{
226+
$denyUsers = Get-Content $deny
227+
$users | Where-Object { $denyUsers -notcontains $_ }
228+
}
229+
else
230+
{
231+
$users
232+
}
233+
}
234+
}

0 commit comments

Comments
 (0)