functions/Create-TaskHere.ps1
function Create-TaskHere { [CmdletBinding(SupportsShouldProcess=$true)] param( [Parameter(Mandatory=$false)][String] $Executable = $null, [Parameter(Mandatory=$false)][String] $Name = $null, [Parameter(Mandatory=$false)][pscredential] [System.Management.Automation.Credential()] $credential = [pscredential]::Empty, [switch] $Silent = $false, [switch][bool] $startAtBoot = $true, [Timespan] $repetitionInterval = [Timespan]::Zero, [DateTime] $startAt = [Datetime]::MinValue, [switch][bool] $force = $true, [string] $args = "" ) Push-Location try { $path = (Get-Location).Path $leaf = Split-Path $path -Leaf $taskName = $Name if ([string]::IsNullOrEmpty($taskName)) { # $parent = Split-Path $path -Parent # $grandparent = Split-Path $parent -Parent $taskName = $leaf if (!$Silent) { $userTaskName = Read-Host "Name of the task [$taskName]" } if ([string]::IsNullOrEmpty($userTaskName)) { $taskName = $taskName } else { $taskName = $userTaskName } } Write-Host "Name of the task = $taskName" $taskExe = $Executable if ([string]::IsNullOrEmpty($taskExe)) { $exeFiles = Get-ChildItem -Path $path -Filter "*.exe" $exe = $exeFiles | where { $_.Name -match $leaf } | select -First 1 if ($exe -eq $null) { $exe = $exeFiles | select -First 1 write-host "no exe files found that match directory name '$leaf'. selecting random exe: '$exe'" } if (!$silent) { $taskExe = Read-Host "task executable [$exe]" } if ($taskExe -eq $null -or $taskExe -eq "") { $taskExe = $exe; } $taskExe = Join-Path $path $taskExe } Write-Host "task executable = $taskExe" write-host "task arguments = $args" if ($credential -eq [pscredential]::Empty -and !$silent) { $credential = Get-Credential -Message "Please provide credentials for the task" } if ($repetitionInterval -ne [Timespan]::Zero -and !$startAtBoot.IsPresent) { # if repetition interval is set, assume user wanted to start the task as soon as possible $startAtBoot = $false if ($startat -eq [datetime]::MinValue) { $startat = [datetime]::Now.AddMinutes(2) } } $task = New-task -TaskName $taskName -Command $taskExe -Credential $credential -startatboot:$startAtBoot -repetitionInterval:$repetitionInterval -startAt:$startAt -force:$force -args:$args return $task } finally { Pop-Location } } |