this is great, can we copy posts like this to wiki?
Google didn't offer me this as a result, I went down the same path, but giving up sooner,
when `$newVar = New-Object -TypeName Ketarin.UrlVariable` did Error.
Ended up using .Clone, which was working, but only when app has at least one variable.
Then I found this post and used it in this function:
# Changes variable value: $app.variables.<var_name>, creates new if it doesn't exists
# usage: changeVar "my_var_name" "value"
function changeVar {
param(
[String]$Name,
[String]$Value,
[String]$Type = "Textual" #StartEnd or #Textual or #RegularExpression
)
# Derived access property, ie. app.variables.$varnm.$varcnt
$Prop = if ($Type -eq "Textual"){"TextualContent"}Else{"CachedContent"}
if (!$app.variables.$Name) { # create new variable
$newVar = New-Object -TypeName Ketarin.UrlVariable -ArgumentList $Name,$app.variables
$newVar.VariableType = $Type
$newVar.$Prop = $Value
$app.variables.Add($Name, $newVar)
$oldTC = $null
} else { # changing existing value
$oldTC = $app.variables.$Name.TextualContent
$oldCC = $app.variables.$Name.CachedContent
$app.variables.$Name.$Prop = $Value
}
# workaround 1 - Copy value to CachedContent property,
# so it will show under Column in main UI
if ($Type -eq "Textual") {$app.variables.$Name.CachedContent = $Value}
# workaround 2 - Clear the TextualContent property,
# I needed this once, but it was probably because of a broken jobs.db.
# No value was showing under Column, if TextualContent wasn't empty
#$app.variables.$Name.TextualContent = $null
$app.Save()
# --- Log
function shrt ($s) { # shorten if too long
$MAX_LENGTH = 30
if ($s.length -gt $MAX_LENGTH) {return $s.Substring(0,$MAX_LENGTH-3) + '...'
} else {return $s}
}
if ($oldTC -eq $null) {
Write-Host "* Created variable: $Name, of type [$Type],"
} else {
Write-Host "* Changed variable: $Name, of type [$Type],"
Write-Host (" from: " + (shrt $oldTC) + " | " + (shrt $oldCC))
}
Write-Host (" to: " + (shrt $app.variables.$Name.TextualContent) +
" | " + (shrt $app.variables.$Name.CachedContent))
Write-Host " (showing: TextualContent | CachedContent)"
}
<# To dot-source file, with this function in Ketarin script:
# avoid using absolute path, get Ketarin working folder
$pathInclude = Join-Path $app.variables.ReplaceAllInString("{startuppath}") "Include"
. "$pathInclude\Variables.ps1"
#>
to add date, as in Ambimind post, use:
$date = Get-Date -UFormat %d-%m-%y
changeVar "LastUpdate" $date
# or:
changeVar "LastUpdate" (Get-Date -UFormat %d-%m-%y)