functions/Create-TaskHere.ps1
function Create-TaskHere ( [Parameter(Mandatory=$false)][String] $Executable = $null, [Parameter(Mandatory=$false)][String] $Name = $null, [Parameter(Mandatory=$false)][String] $Username = $null, [Parameter(Mandatory=$false)][String] $Password = $null, [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 $parent = Split-Path $parent -Parent $cfg = (split-path -leaf $parent).Replace("www", "").Trim("-") $taskName = $leaf if (-not [string]::IsNullOrEmpty($cfg)) { $taskName = "$cfg-$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 ($Username -eq $null -and !$silent) { $Username = Read-Host "task user" } if ($Password -eq $null -and !$silent) { $Password = Read-Host "task user password" } 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 -UserName $Username -pass $Password -startatboot:$startAtBoot -repetitionInterval:$repetitionInterval -startAt:$startAt -force:$force -args:$args return $task } finally { Pop-Location } } |