Jump to content
Ketarin forum
  • Announcements

Sign in to follow this  
Ambimind

Creating new application job variable, with powershell

Recommended Posts

Ambimind   

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

Share this post


Link to post
Share on other sites
floele   

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.

Share this post


Link to post
Share on other sites
Ambimind   

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 

Share this post


Link to post
Share on other sites
floele   

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.

Share this post


Link to post
Share on other sites
Ambimind   

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..)?

Share this post


Link to post
Share on other sites
Ambimind   

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

Share this post


Link to post
Share on other sites
g00p3k   

this is really cool! 

I've found this app to be very useful as a resource to setup my dev machine easily. This type of functionality just makes it even easier to do some really cool things with it. :-) Thanks

Share this post


Link to post
Share on other sites
papo   
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)

 

Share this post


Link to post
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

Sign in to follow this  

×