Jump to content
Ketarin forum

Creating new application job variable, with powershell


Ambimind
 Share

Recommended Posts

Wondering if this is possible?

 

What I tried :

 

The "$app.variables" object has the following method :

void Add(string key, Ketarin.UrlVariable value)

 

This tells me I need to create an instance of "Ketarin.UrlVariable":

I try this : New-Object -TypeName Ketarin.UrlVariable

Error message results : "A constructor was not found .."

 

So I thought maybe the constructor requires some parameters:

I tried to find them : ([Type]"Ketarin.UrlVariable").GetConstructors() | ForEach {$_.GetParameters()} | Select *

No output in log.

 

So does this mean we don't have access to the "Ketarin" namespace?

 

Thx

Link to comment
Share on other sites

  • 4 weeks later...

The constructor currently is "internal", so cannot be used other than by Ketarin itself.

 

The question would be: What exactly do you plan with the added variable? If you can provide me with a full script (mockup) I can think about if and how a solution would work.

Link to comment
Share on other sites

Thanks for the feedback floele.

 

Use case :

 

I want to create a custom column which records the last time an application was updated.

The purpose is to detect abandonware and website structure changes(and thus silently failing regex matches).

 

With more then 100 items in my database, I would need to manually create an - eg. "Updated" variable - for each job, and for all those new.

I'd prefer to have a global post-download Powershell script, which checks for the job-specific "Updated" variable, and if not found create and update it.

 

Example script :

 

# Parameters

$varnm = "Updated"
$vartp = "Textual"
#
# Derived access property
$varcnt = if($vartp -eq "Textual"){"TextualContent"}Else{"CachedContent"}

# Check if variable exists in "variables" object

$exists = $app.variables | Get-Member -name $varnm
#
# If it doesn't exist, then create a new one, also set date
if(-Not $exists){
     $newvar = New-Object -TypeName Ketarin.UrlVariable -ArgumentList $vartp
     $app.variables.Add($varnm, $newvar)
     #
     # Set it to the current date, short
     $app.variables.$varnm.$varcnt = Get-Date -UFormat %d-%m-%y
}

# END 

Link to comment
Share on other sites

OK, I will make the following constructor public:

 

public UrlVariable(string name, ApplicationJob.UrlVariableCollection collection)

 

Second argument requires you to pass in the Variables property of an application. Also, you have to call the Save() method on the application after doing that, otherwise the variable will be lost.

Link to comment
Share on other sites

OK, I will make the following constructor public:

 

public UrlVariable(string name, ApplicationJob.UrlVariableCollection collection)

 

Second argument requires you to pass in the Variables property of an application. Also, you have to call the Save() method on the application after doing that, otherwise the variable will be lost.

Thank you.

What will be the type of urlVariable created(ie. Textual..)?

Link to comment
Share on other sites

As an example of the new feature, following the release of Ketarin 1.8.6 beta 3 / 1.8.6.700, please find below the working version of the prior mock-script : 

 

#### ADD DATE VARIABLE IF NOT EXISTING
# Parameters
$varnm
= "LastUpdate"
$vartp = "Textual"   #StartEnd or #Textual or #RegularExpression
#
# Derived access property, ie. app.variables.$varnm.$varcnt
$varcnt = if($vartp -eq "Textual"){"TextualContent"}Else{"CachedContent"}

# If it doesn't exist, then create a new one
if(-Not $app.variables.$varnm){
    #
    ECHO "========== $varnm NOT FOUND, ADDING IT =========="
    #
    # Add var to job/app
    $newvar = New-Object -TypeName Ketarin.UrlVariable -ArgumentList $varnm,$app.variables
    $newvar.VariableType = $vartp 
    $app.variables.Add($varnm, $newvar)
    $app.Save()
}

 

#Set it to the current date, short format(should be sortable)
$app.variables.$varnm.$varcnt = Get-Date -UFormat %d-%m-%y

#
####

 

Usage Notes :

 

Step 1 : Add a custom volumn. The value, variable name, corresponds to $varnm in the script

Step 2 : Input the script to run for every job/app, after download is complete

 

kzUIBNG.png

Link to comment
Share on other sites

  • 10 months later...
  • 2 months later...
On 8/14/2016 at 5:55 AM, Ambimind said:

As an example of the new feature...

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)

 

Link to comment
Share on other sites

  • 4 years later...

I've been using the `LastUpdated` and a modified variation on it for `LastVersion` based on this for years. I'm going through ALL of my apps right now and it's painful to have to add the current info variables I need for my external tools, so I decided to wrap it all in a function and force it across my database. This is how I'm doing it:

####
function NewAppVariable {
	Param(
		[Parameter(Mandatory)]
		$sVarName,
		
		$sVarValue = "",

		[ValidateSet("RegularExpression", "StartEnd", "Textual")]
		$sVarType = "Textual"
	)

	# Derived access property, ie. app.variables.$sVarName.$varcnt
	$varcnt = if($sVarType -eq "Textual"){"TextualContent"}Else{"CachedContent"}

	# If it doesn't exist, then create a new one
	if(-Not $app.variables.$sVarName){
		ECHO "=== $sVarName NOT FOUND, ADDING IT ==="
		# Add var to job/app
		$newvar = New-Object -TypeName Ketarin.UrlVariable -ArgumentList $sVarName,$app.variables
		$newvar.VariableType = $sVarType
		$app.variables.Add($sVarName, $newvar)
		$app.Save()
	}

	# If it was passed a specific value, assign it
	if( $sVarValue -ne "" ){
		$app.variables.$sVarName.$varcnt = $sVarValue
	}
}
####

This means I can then use the following much simpler lines to add my new variables:

#### ADD MISSING VARIABLES & UPDATE Last* Cache
NewAppVariable "sbits"
NewAppVariable "schangelog"
NewAppVariable "sdownload"
NewAppVariable "snotes"
NewAppVariable "splatform"
NewAppVariable "swebsite"
NewAppVariable "svendor"
NewAppVariable "version"
NewAppVariable "LastUpdate" {Get-Date -UFormat %Y%m%dT%H%M}
NewAppVariable "LastVersion" $sversion
####

The way it's written it will never overwrite or destroy an existing variable or its contents, other than LastUpdate and LastVersion which are specifically intended for that purpose. If you need another variable it's as simple as NewAppVariable <variable name> <newvalue> <type>. Thank you again @Ambimind!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.