Increment Registry value on every run

I am trying to increase a reg key every time the action runs but it keeps failing or it runs but then does not change the value. Any suggestions. Sorry if I posted this wrong, not sure how to post the action…

if {exists value whose (name of it is “RunCount”) of key “HKEY_LOCAL_MACHINE\SOFTWARE\CORP” of native registry}
parameter “CurrentKeyValue” = “{value whose (name of it is “RunCount”) of key “HKEY_LOCAL_MACHINE\SOFTWARE\CORP” of native registry}”
regset64 “[HKEY_LOCAL_MACHINE\SOFTWARE\CORP]” “RunCount”={(parameter “CurrentKeyValue” of action)+1}
elseif {not exists value whose (name of it is “RunCount”) of key “HKEY_LOCAL_MACHINE\SOFTWARE\CORP” of native registry}
regset64 “[HKEY_LOCAL_MACHINE\SOFTWARE\CORP]” “RunCount”=“0”
else
regset64 “[HKEY_LOCAL_MACHINE\SOFTWARE\CORP]” “RunCount”=“0”
endif

You don’t need to do all that. Much easier options. I am curious what your use case is though.

Also, if you want to get the run count of a bigfix action, that is already available in the client actionhistory.db sqlite database. You don’t have to separately track it. You can just query it from there.

I recommend extending the scope of the actionhistory.db to 5 years from the default of 1 year so that it encompases the lifecycle of the device. See here: https://github.com/jgstew/bigfix-content/blob/master/fixlet/clientsettings/Set%20__BESClient_ActionManager_HistoryKeepDays_%20to%20_1825_%20-%205%20years%20-%20Universal.bes

I think all of the actionhistory.db in my environment are all less than 2MB even though they have been set to 5 years and actually have 5 years worth of history in them.

Example:

1 Like

If you really want to go the route of incrementing an integer in the registry, then just do:

regset64 "[HKEY_LOCAL_MACHINE\SOFTWARE\CORP]" "RunCount"="{unique value of (it + 1) of (it as string as integer) of values "RunCount" of keys "HKEY_LOCAL_MACHINE\SOFTWARE\CORP" of native registry | 1}"

You don’t need any if/then/else checks or anything. This will return 1 if the value doesn’t already exist, otherwise it returns the current value + 1

if done right, you can avoid almost all if/then/else checks by writing the relevance differently, and end up with something that is more efficient, more compact, and I think easier to follow:

unique value of (it + 1) of (it as string as integer) of values "RunCount" of keys "HKEY_LOCAL_MACHINE\SOFTWARE\CORP" of native registry | 1

it is the | 1 at the end that ensures it always returns 1 no matter what.

That said, you don’t technically need to do this, also this method only works on windows, while the ActionHistory.DB method is cross platform.

If you want some help with the ActionHistory.db method, then let me know. I recommend using the DB Browser for SQLite to examine it and help write the needed SQLite query:

The hardest part of this is you have to do a join on 2 different tables to get the name of the action and the active count of the same action.

To get the active count of all actions:

SELECT "ActionID", "ActiveCount" FROM "main"."ACTION_STATE_HISTORY"

To get the ActionID to Name mapping:

SELECT "ActionID", "ActionName" FROM "main"."ACTION_HISTORY"

The hard part is joining these 2 queries with either relevance or SQLite query. I’m not a SQL expert. I would have to find an example.

These example have a JOIN already that is similar to what is needed:

1 Like

sorry, getting hammered. We need it in that reg key, we pull data from that key for other sources. That key contains multiple entries related to BigFix and external scripts that read and use that data. We also want to pull that and other data into an analysis for reports. Our external scripts will not be able to access the BigFix DB

I originally wrote that in batch which worked in the OS but when I put it in BigFix, it didn’t work, after adding debug code, I found BigFix was changing the errorlevel. It is what it is.

I will try what you gave me and go from there. I appreciate all of the help.

The first option worked great. The second one products a syntax error when I tried to save the task. I am sure it has to do with the placement of { and }.

Thanks for the help

1 Like

interesting. which worked? which didn’t?

This did not work, I think it has to do with the curlies.

Oh, that 2nd was meant for pure relevance evaluation in fixlet debugger / qna / registry

I did this about a year ago for testing actions, etc.

This is what i used as actionscript in a task:

// Create registy key if not exist
if {not exists key "HKLM\SOFTWARE\Company\Test-BigFixDev" of native registry}
regset64 “[HKEY_LOCAL_MACHINE\SOFTWARE\Company\Test-BigFixDev]” “testtask1”=“0”
endif

// Create registy value if not exist
if {exists key "HKLM\SOFTWARE\Company\Test-BigFixDev" whose (not exists values whose (name of it = “testtask1”) of it) of native registry}
regset64 “[HKEY_LOCAL_MACHINE\SOFTWARE\Company\Test-BigFixDev]” “testtask1”=“0”
endif

// If key exists but not value, it appears the Action Script triggers a false error.
// Need a pause here before next steps, but since pause doesn’t exist, adding a continueif seems to work
continue if {exists key "HKLM\SOFTWARE\Company\Test-BigFixDev" whose (exists values whose (name of it = “testtask1”) of it) of native registry}

// Get registry value, +1, Set new registry value
parameter “param1” = "{value “testtask1” of key “HKLM\SOFTWARE\Company\Test-BigFixDev" of native registry as integer}”
parameter “param2” = “{(parameter “param1” as integer) + 1}”

regset64 “[HKEY_LOCAL_MACHINE\SOFTWARE\Company\Test-BigFixDev]” “testtask1”=“{parameter “param2” as integer}”