JavaScript is disabled on your browser.
using namespace System.Management.Automation;
set-strictmode -version 2.0
## NOTE - remove dups from start-psec-v4 TODO
function isDir([string]$path) {
if (-not (test-path -path $path)) {return $FALSE};
if (test-path -path $path -PathType Container) {return $TRUE};
return $False;
}
function fmtLocn() {
$csf = get-PSCallStack
$scr = $csf.scriptname[2]
if ($null -ne $scr) {
$lix = $scr.lastIndexOf("\");
if ($lix -gt 0) {$scr = $scr.substring($lix + 1)}
$scr = $scr -replace '\.(psm1|ps1)',''
$pos = $csf.scriptlinenumber[2]
} else {
return ""
}
return "[$($scr)@$($pos)] "
}
function hlogReal {
if (($host.Name -match 'consolehost')) {
$locn = fmtLocn
$Host.UI.WriteLine("$($locn) $($args[0])")
}
}
<#.md
.hfunc
.desc
writeErrorLine $args[0] directly to host logger
.lev1
.notes
When called from a script it prefixes the message with the script location as a debugging aid.
#>
function hlogErr {
$locn = fmtLocn
$Host.UI.WriteErrorLine("$($locn) $($args[0])")
}
<#.md
.hfunc
.desc
Create a runlog record for timesheet creation
.lev1
.notes
Used to create a runlog record and append to the file defined by ENV:RUNLOG
This is used to create logging records so the timestamps can be used to determine
approximate activity on a system and therefore a project.
#>
function runlog($desc) {
[string]$log = $ENV:RUNLOG
if ($log -eq "") {return}
$sys = $ENV:COMPUTERNAME
$date = Get-Date
[string]$dateStr = $date.ToString("yyyyMMdd hh:mm:ss")
$str = "$sys $datestr $PWD $desc";
hilite -yellow "runlog:$str"
add-content $log "$($str)"
}
function userNotify([string]$msg) {
$balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
$balloon.BalloonTipText = $msg
$balloon.BalloonTipTitle = "Attention $Env:USERNAME"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
}
#@ hilite args; print highted lines on PowerShell console. hilite ? for help
<#.md
.hfunc
.desc
print highted lines on PowerShell console. hilite ? for help
.lev1
.notes
When called from a script it prefixes the message with the script location as a debugging aid.
#>
function hilite {
# DO NOT SPECIFY param(...)
# we parse colors ourselves.
$allColors = ("-Black", "-DarkBlue","-DarkGreen","-DarkCyan","-DarkRed","-DarkMagenta","-DarkYellow","-Gray",
"-Darkgray","-Blue", "-Green", "-Cyan", "-Red", "-Magenta", "-Yellow", "-White",
"-Foreground")
$color = "Foreground"
$nonewline = $false
if ($args.length -eq 1 -and $args[0] -eq "?") {
hilite -cyan "--------- hilite help -----------------"
hilite -cyan "colors:" -white " $allColors"
hilite -cyan "switches:" -white " -nonewline"
return
}
foreach($arg in $args) {
if ($arg -eq "-nonewline") {
$nonewline = $true
} elseif ($allColors -contains $arg) {
$color = $arg.substring(1)
} else {
if ($color -eq "Foreground") {
Write-Host $arg -nonewline
} else {
Write-Host $arg -foreground $color -nonewline
}
}
}
if (($host.Name -match 'consolehost')) {
Write-Host -nonewline:$nonewline
}
}
<#.md
.hfunc
.desc
return elapsed time
.lev1
.notes
Create the $sw stopwatch using $sw = [Diagnostics.Stopwatch]::StartNew()
Display the value using gblShowElapsed $sw
#>
function gblShowElapsed($sw) {
$sw.Stop()
$ans = ""
if ($sw.Elapsed.minutes -gt 0) {$ans = $ans + " " + $sw.Elapsed.minutes +"min(s)"}
if ($sw.Elapsed.seconds -gt 0) {$ans = $ans + " " + $sw.Elapsed.seconds +"sec(s)"}
"=============== Elapsed $ans"
}
#@ pandoc options; run pandoc program passing options
<#.md
.hfunc
.desc
executes pandoc with the specified args.
.notes
Requires pandoc to be installed.
pandoc -r docx -t rtf -s -o H:\psec\flows\psec-demo\tasks\demo-java\demo-java.rtf H:\psec\flows\psec-demo\tasks\demo-java\demo-java.docx
#>
function pandoc() {
$opts = $args[0..$args.count]
#hilite -yellow "pandoc $opts"
$execLocn = "$env:PSEC_V4_EXEC_DIR";
& "$($execLocn)\pandoc\pandoc.exe" @opts
}
function hlog {
$locn = fmtLocn
#$locn=""
#$Host.UI.WriteLine("$($locn) $($args[0])")
try {
$dlog = $GLOBAL:DLOG;
$dlog.info("$( $locn ) $( $args[0] )");
} catch {
hlogReal("$($locn) $($args[0])")
}
}
function hlogNow {
$locn = fmtLocn
$dlog.info("$($locn) $($args[0])");
$dlog = $GLOBAL:DLOG;
$dlog.flush();
}
<#.md
.hfunc
.desc
logs to the console and to the dlog file
.notes
Runs under the GUI context where a dlog file exists.
#>
function hlogBoth {
$locn = fmtLocn
if (($host.Name -match 'consolehost')) {
$Host.UI.WriteLine("both $($locn) $($args[0])")
}
try {
$dlog.info("$($locn) $($args[0])");
$dlog = $GLOBAL:DLOG;
$dlog.flush();
} catch {
hlogReal("$($locn) $($args[0])")
}
}
function x {exit 0} # exit $lastExitCode set to 0
function exists($obj,$prop) {
try {
if ($null -ne $obj[$prop]) {return $true}
return $false
} catch {
return $false
}
return $false
}
function notExists($obj,$prop) {
if (exists $obj $prop) {return $false;}
return $true;
}
<#.md
.hfunc
.desc
set file modified date to current time
.lev1
.notes
#>
function touch($filePath) {
(Get-Item $filePath).lastwritetime = $( Get-Date )
}
function easyType($obj) {
#param($obj)
if ($null -eq $obj) {return "NULL";}
$str = "$($obj.getType())";
$ix = $str.lastIndexOf(".");
if ($ix -gt 0) {return $str.substring($ix + 1,$str.length - $ix -1)}
return $str
}
# avoid test-path failure when null
function testPath($path) {
if ($null -eq $path) {return $false;}
return (& test-path $path)
}
function getMandEnvVbl([string]$vbl) {
$val = [Environment]::GetEnvironmentVariable($vbl)
if ("$val" -eq "") {
[void](& ErrorPanel "Environment variable $vbl not defined")
return $null;
}
return $val;
}
function resolveClassPath([string]$path) {
hlog("resolveClassPath $path");
$outStr = "";
forEach($str in ($path -split ';')) {
if ($str -eq "") {continue;}
hlog("resolveClassPath str $str");
if ($str -match '^[$]env:(?[A-Za-z_][A-Za-z0-9_]*)') {
$envVbl = "$($matches.vbl)";
hlog("resolveClassPath vbl $envVbl");
$envVal = getMandEnvVbl($envVbl);
if ($envVal -eq $null) {"return env $envVbl vbl not defined"}
$str = $envVal+"$($str.substring($envVbl.length + 5))"
}
if (test-path -path $str) {
$fileType = (& get-item $str);
hlog("resolveClassPath dir $str");
if ($fileType -is [System.IO.DirectoryInfo]) {
forEach($item in (& expandJarsLib $str)) {
$outStr += "$($item);";
}
} else {
$outStr += "$($str)xx;";
}
} else {
[void](& ErrorPanel "Path folder $str does not exist. Skipped")
}
#$outStr += $str +";";
}
return $outStr;
}
function expandJarsLib([string]$dirPath) {
$list = @()
$list += $dirPath # add this incase there are resources
forEach($file in (get-childitem $dirPath | sort-object -descending -Property LastModifiedTime,Name)) { # sort to get a stable logical, sequence
if ($file.name -match '[.]((jar)|(zip))$') {
$list += "$dirPath/$($file.name)"
}
}
return $list;
}