RESTAPI: Generate a template task with prefetch from URL - Windows
Log In or Register to download the BES file, and more.

0 Votes

Versioning - This is the latest version.

1RESTAPI: Generate a template task with prefetch from URL - Windows 1/29/2015 11:20:27 AM
2RESTAPI: Generate a template task with prefetch from URL - Windows 1/29/2015 1:19:28 PM
3RESTAPI: Generate a template task with prefetch from URL - Windows 1/30/2015 3:29:50 PM
4RESTAPI: Generate a template task with prefetch from URL - Windows 2/3/2015 11:38:39 AM

Description

This task will generate uninstall tasks for all of the MSIs on the target computer in the same site as this task is run from using the current console operator username and password and the REST API. This task uses secure parameters so the password is encrypted.

 

References:

http://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request

https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Tivoli%20Endpoint%20Manager/page/RESTAPI%20Task

https://github.com/bigfix/restapi-examples/tree/master/evaluate-relevance/cURL

 

Parameter Values:   (queried using JavaScript & console based session relevance)


Property Details

ID3973
StatusBeta - Preliminary testing ready for more
TitleRESTAPI: Generate a template task with prefetch from URL - Windows
DomainBESC
CategoryConfiguration
Download Size0
Source IDjgstew
Source Release Date1/26/2015 12:00:00 AM
Added by on 2/3/2015 11:38:39 AM
Last Modified by on 2/3/2015 11:38:39 AM
Counters 4284 Views / 17 Downloads
User Rating 1 star 2 star 3 star 4 star 5 star * Average over 0 ratings. ** Log In or Register to add your rating.

Relevance

Windows Only (Relevance 2997197)
Used in 6448 fixlets and 32 analyses   * Results in a true/false
Show indented relevance
/* Windows Only */ windows of operating system
Used in 158 fixlets and 17 analyses   * Results in a true/false
Show indented relevance
/* Windows XP or Higher */ version of operating system >= "5.1"
BES Client is version 9+ (Relevance 2999387)
Used in 20 fixlets   * Results in a true/false
Show indented relevance
version of client >= "9.0" as version
Used in 4 fixlets   * Results in a true/false
Show indented relevance
/* Only relevant if the endpoint has a public IP */ ( exists addresses whose(it as string does not start with "172." AND it as string does not start with "10." AND it as string does not start with "192.168." AND it as string does not start with "169.254." AND it as string does not start with "127." AND it as string does not start with "0.") of adapters of network )

Actions

Action 1 (default)

Action Link Click here to deploy this action.
Script Type BigFix Action Script
action parameter query "Download_URL" with description "Enter the download URL of the file:" and with default value "http://"

// DEBUGGING: The following is for debugging
// appendfile {parameter "RootServerURL"}
// appendfile {parameter "currentSiteName"}
// appendfile {parameter "currentSiteType"}
// appendfile {parameter "currentConsoleUser"}
// move __appendfile __Download\parameters.txt
// parameter "secret" of action

// -- START:DOWNLOADS -------------------------

// - This is the commandline 7zip console which is use to unpack installers
// - https://www.virustotal.com/en/file/2a3afe19c180f8373fa02ff00254d5394fec0349f5804e0ad2f6067854ff28ac/analysis
prefetch 7za.zip sha1:9ce9ce89ebc070fea5d679936f21f9dde25faae0 size:384846 http://softlayer-dal.dl.sourceforge.net/project/sevenzip/7-Zip/9.20/7za920.zip

// - This unzip is a compiled verion of unzip v5.52 fom http://info-zip.org/
// - https://www.virustotal.com/en/file/8d9b5190aace52a1db1ac73a65ee9999c329157c8e88f61a772433323d6b7a4a/analysis
prefetch unzip.exe sha1:e1652b058195db3f5f754b7ab430652ae04a50b8 size:167936 http://software.bigfix.com/download/redist/unzip-5.52.exe

// Download CURL http://curl.haxx.se/download.html
// - Analysis of the CURL.exe inside: https://www.virustotal.com/en/file/7e4379dc72cc25e8d546b3f4b4305b93b6521aaae47c1ea2ee49e4cc4ce8b033/analysis/
prefetch curl.cab sha1:3edb8cfb34ba149ca36b2540cf58a50430a3c8bc size:432989 http://home.arcor.de/skanthak/download/curl-7.38.0.cab

// -- END:DOWNLOADS ---------------------------


// ASSERT: URL ends with a filename
continue if{ exists (it as trimmed string) whose(length of it > 5 AND it contains ".") of following texts of lasts "/" of (parameter "Download_URL") }


// ADD TO UTILITY CACHE: unzip,7zip,curl
utility __Download\unzip.exe
utility __Download\7za.zip
utility __Download\curl.cab

// http://pic.dhe.ibm.com/infocenter/tivihelp/v26r1/index.jsp?topic=%2Fcom.ibm.tem.doc_8.2%2FPlatform%2FAction%2Fc_action_launch_preference_low_p.html
// launch exe's with low priority settings to help prevent disrupting the user
action launch preference low-priority

// EXTRACT using unzip: the 7zip command line utility
waithidden __Download\unzip.exe -o "{pathname of file "7za.zip" of folder "__Download" of client folder of current site}" 7za.exe -d "{pathname of folder "__Download" of client folder of current site}"

// EXTRACT using 7zip: CURL
waithidden "{ (pathname of file "7za.exe" of folder "__Download" of client folder of current site) }" e -i!Curl.EXE -y -o"{pathname of folder "__Download" of client folder of current site}" "{pathname of file "curl.cab" of folder "__Download" of client folder of current site}"


// VERIFY: Make sure the parameters are set
continue if { (parameter "currentSiteName" as trimmed string != "") AND (parameter "currentSiteType" as trimmed string != "") AND (parameter "RootServerURL" as trimmed string != "") }

// CURL -> REST API
// - Download all tasks in current site
waithidden "{ (pathname of file "Curl.EXE" of folder "__Download" of client folder of current site) }" -k -o "{pathname of folder "__Download" of client folder of current site}\Tasks_{parameter "currentSiteName"}.xml" --user {parameter "currentConsoleUser"}:{parameter "secret"} https://{parameter "RootServerURL"}/api/tasks/{parameter "currentSiteType"}/{parameter "currentSiteName"}


// only continue if the task does not already exist.
continue if{ not exists node values whose( it contains (parameter "Download_URL") ) of xpaths "BESAPI/Task/Name/text()" of xml documents of files whose(name of it starts with "Tasks_" AND name of it as lowercase ends with ".xml" AND name of it contains (parameter "currentSiteName") ) of folder "__Download" of client folder of current site }

// CURL
// - Download the file to generate the prefetch for
waithidden "{ (pathname of file "Curl.EXE" of folder "__Download" of client folder of current site) }" -k -o "{pathname of folder "__Download" of client folder of current site}\prefetch_item" {parameter "Download_URL"}

// Manually cache the CURL'd download in the client download folder
// Only if not already cached
// Need to add a check to only do this if the downloaded file is much smaller than the maximum download cache size
if{not exists files whose(name of it contains ( sha1 of file "prefetch_item" of folder "__Download" of client folder of current site ) ) of folders "__Global\__Cache\Downloads" of ( (if (exists properties whose(it as string starts with "data folder of " AND it as string contains "client")) then (data folder of client) else NULL)|(folder "/var/opt/BESClient/__BESData")|(folder "/Library/Application Support/BigFix/BES Agent/__BESData")|(folder "__BESData" of parent folder of client) ) }
    copy "{pathname of file "prefetch_item" of folder "__Download" of client folder of current site}" "{pathname of folders "__Global\__Cache\Downloads" of ( (if (exists properties whose(it as string starts with "data folder of " AND it as string contains "client")) then (data folder of client) else NULL)|(folder "/var/opt/BESClient/__BESData")|(folder "/Library/Application Support/BigFix/BES Agent/__BESData")|(folder "__BESData" of parent folder of client) ) }\{sha1 of file "prefetch_item" of folder "__Download" of client folder of current site}"
endif


if{ ((it as lowercase as trimmed string ends with ".msi") of following text of last "/" of (parameter "Download_URL")) }
// create msiGetProperty.vbs
delete __createfile
delete __Download\msiGetProperty.vbs

createfile until END_OF_FILE

' msiGetProperty.vbs
' Written by James Stewart ( @jgstew )
' Public Domain - use at your own risk
Const msiOpenDatabaseModeReadOnly = 0
Dim msiLib, db, view, msiPath, msiProperty, objCurrentFolder, myRegExp, matches

msiProperty = "ProductName"

'Prepare a regular expression object
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Pattern = "msiGetProperty(\w).vbs"
Set matches = myRegExp.Execute( Wscript.ScriptName )

' set property retrieved based upon name of script
If 1 = matches.Count Then
    If 1 = matches.Item(0).SubMatches.Count Then
        msiProperty = myRegExp.Execute( Wscript.ScriptName ).Item(0).SubMatches(0)
    End If
End If

' http://blogs.technet.com/b/heyscriptingguy/archive/2006/04/05/how-can-i-determine-the-path-to-the-folder-where-a-script-is-running.aspx
' http://stackoverflow.com/questions/4200028/vbscript-list-all-pdf-files-in-folder-and-subfolders
Set objCurrentFolder = CreateObject("Scripting.FileSystemObject").GetFolder( CreateObject("Wscript.Shell").CurrentDirectory )

' https://technet.microsoft.com/en-us/library/ee156618.aspx
If 1 = WScript.Arguments.Unnamed.Count Then
    msiPath = WScript.Arguments.Unnamed.Item(0)
Else
    ' operating on an MSI in the current folder ( if there are multiple, the last one will be used )
    For Each objFile in objCurrentFolder.Files
        If UCase( CreateObject("Scripting.FileSystemObject").GetExtensionName(objFile.name)) = "MSI" Then
            msiPath = objFile.Name
        End If
    Next
    
    If IsEmpty( msiPath ) Then
        Wscript.Echo
        Wscript.Echo "-ERROR-"
        Wscript.Echo " Usage: msiGetProductVersion.vbs ""PATH\TO\FILE.MSI"" [/Property:NAME_OF_MSI_PROPERTY]"
        Wscript.Quit
    End If
End If

' http://stackoverflow.com/questions/4100506/check-if-an-object-exists-in-vbscript
'If WScript.Arguments.Named.Count = 1 Then
If NOT IsEmpty( WScript.Arguments.Named.Item("Property") ) Then
    msiProperty = WScript.Arguments.Named.Item("Property")
End If

Set msiLib = CreateObject("WindowsInstaller.Installer")
Set db = msiLib.OpenDataBase(msiPath, msiOpenDatabaseModeReadOnly)
Set view = db.OpenView("SELECT `Value` FROM `Property` WHERE `Property` = '" & msiProperty & "'")
view.Execute()

Wscript.Echo view.Fetch().StringData(1)

END_OF_FILE

move __createfile __Download\msiGetProperty.vbs

// Get ProductName & ProductVersion
// https://www.ibm.com/developerworks/community/forums/html/topic?id=7e61b914-74a6-4cf2-93b2-a191b1bd0b4d
waithidden cmd /c cscript /nologo "{ pathname of file "msiGetProperty.vbs" of folder "__Download" of client folder of current site }" /Property:ProductName "{ pathname of file "prefetch_item" of folder "__Download" of client folder of current site }" > "{ pathname of folder "__Download" of client folder of current site }\msiProductName.txt"
waithidden cmd /c cscript /nologo "{ pathname of file "msiGetProperty.vbs" of folder "__Download" of client folder of current site }" /Property:ProductVersion "{ pathname of file "prefetch_item" of folder "__Download" of client folder of current site }" > "{ pathname of folder "__Download" of client folder of current site }\msiProductVersion.txt"

endif
// END OF if{ ((it as lowercase as trimmed string ends with ".msi") of following text of last "/" of (parameter "Download_URL")) }


parameter "ProductName" = "{ (unique value of lines whose(length of it > 2) of files "msiProductName.txt" of folders "__Download" of client folders of current sites ) | "ENTER_PRODUCT_NAME_HERE"}"
parameter "ProductVersion" = "{ (unique value of lines whose(length of it > 2) of files "msiProductVersion.txt" of folders "__Download" of client folders of current sites ) | "0.0.0.0" }"


// generate template task
delete __createfile
delete __Download\Task_prefetch.bes

// CREATEFILE
createfile until END_OF_FILE
="1.0" encoding="UTF-8"?>
="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BES.xsd">
    
        Install/Upgrade: <span class='actionscriptRelevance'>{ parameter "Download_URL" }</span> - Windows
        This task will download: { parameter "Download_URL" }

This task was automatically generated using the task: "RESTAPI: Generate a template task with prefetch from URL - Windows"

 

]]>

        /* Windows Only */ windows of operating system
        = "5.1"]]>
        not exists keys whose( (exists values "DisplayName" whose(it as string as lowercase starts with "{parameter "ProductName"}" as lowercase) of it) AND (exists values "DisplayVersion" whose(it as string as version >= "{parameter "ProductVersion"}") of it) ) of keys "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" of ( x32 registries;x64 registries )
        Software Sharing
        { (it as string) of size of file "prefetch_item" of folder "__Download" of client folder of current site }
        RESTAPI: Generate a template task with prefetch from URL - Windows
        { parameter "currentConsoleUser" }
        { ( ((year of it as string &"-"& month of it as two digits &"-"& day_of_month of it as two digits) of date (local time zone) of it) of now ) }
        
        
        
        
            x-fixlet-modification-time
            {now as string}
        
        BESC
        ="Action1">
            Click here to install/upgrade "{parameter "ProductName"}".
            ="application/x-Fixlet-Windows-Shell">

// Download: { (parameter "Download_URL") }
{ ("prefetch " & ( following text of last "/" of (parameter "Download_URL") ) & " sha1:" & (sha1 of it as string) & " size:" & (size of it as string) & " " & (parameter "Download_URL") ) of file "prefetch_item" of folder "__Download" of client folder of current site }

{ if ((it as lowercase as trimmed string ends with ".zip") of following text of last "/" of (parameter "Download_URL")) then "prefetch unzip.exe sha1:e1652b058195db3f5f754b7ab430652ae04a50b8 size:167936 http://software.bigfix.com/download/redist/unzip-5.52.exe" else "" }
{ if ((it as lowercase as trimmed string ends with ".msi") of following text of last "/" of (parameter "Download_URL")) then "waithidden msiexec /i %22%7B(pathname of file whose(name of it as lowercase ends with %22.msi%22) of folder %22__Download%22 of client folder of current site)%7D%22 /qn /norestart" else "" }
{ if ((it as lowercase as trimmed string ends with ".exe") of following text of last "/" of (parameter "Download_URL")) then ("waithidden %22%7B(pathname of file %22" & it & "%22 of folder %22__Download%22 of client folder of current site)%7D%22 /S") of following text of last "/" of (parameter "Download_URL") else "" }
{ if ((it as lowercase as trimmed string ends with ".zip") of following text of last "/" of (parameter "Download_URL")) then ("waithidden __Download\unzip.exe -o %22%7Bpathname of client folder of current site%7D\__Download\" & it & "%22 -d %22%7Bpathname of client folder of current site%7D\__Download%22") of following text of last "/" of (parameter "Download_URL") else "" }
// Prefetch Block Download Option:
// { ("add prefetch item name=" & ( following text of last "/" of (parameter "Download_URL") ) & " sha1=" & (sha1 of it as string) & " size=" & (size of it as string) & " url=" & (parameter "Download_URL") ) of file "prefetch_item" of folder "__Download" of client folder of current site }
            
            ="OriginalRelevance">
        
    

END_OF_FILE

move __createfile __Download\Task_prefetch.bes

// Import UninstallMSI_Tasks.bes into custom site using API if there is at least one task inside (use secure parameter for user password)
if{ (exists files "Task_prefetch.bes" whose(exists contents whose(it contains "") of it) of folder "__Download" of client folder of current site) }
// IMPORT
    waithidden "{ (pathname of file "Curl.EXE" of folder "__Download" of client folder of current site) }" -k -X POST --data-binary @"{pathname of file "Task_prefetch.bes" whose(exists contents whose(it contains "") of it) of folder "__Download" of client folder of current site}" --header "Content-Type:text/xml" -o "{(pathname of folder "__BESData\__Global\Logs" of parent folder of client)}\RESTAPI_ImportedTasks_{parameter "currentSiteName"}.log" --user {parameter "currentConsoleUser"}:{parameter "secret"} https://{parameter "RootServerURL"}/api/tasks/{parameter "currentSiteType"}/{parameter "currentSiteName"}
endif

// VERIFY: Check if tasks were actually imported
// TODO: Need to add a check to see if the log file was created after the action started running (See adobe CC template for an example)
continue if{exists files whose((name of it = ("RESTAPI_ImportedTasks_" & (parameter "currentSiteName") & ".log")) AND exists lines whose(it contains "") of it) of folder "__BESData\__Global\Logs" of parent folder of client}

// NOT BEING USED:

// query custom site for all existing uninstall tasks
// starts with "Uninstall: MSI - "
// https://github.com/bigfix/restapi-examples/tree/master/evaluate-relevance/cURL
// curl --data-urlencode "relevance=RELEVANCE" --user username:password https://server:port/api/query
// (preceding text of last " - Windows" of following text of first "Uninstall: MSI - " of it) of names of custom bes fixlets whose( (name of it starts with "Uninstall: MSI - ") AND (name of it contains " - Windows") AND (source of it = "Configuration: Generate uninstall tasks for all MSI applications on target computer - Windows") )
// (preceding%20text%20of%20last%20%22%20-%20Windows%22%20of%20following%20text%20of%20first%20%22Uninstall%3A%20MSI%20-%20%22%20of%20it)%20of%20names%20of%20custom%20bes%20fixlets%20whose(%20(name%20of%20it%20starts%20with%20%22Uninstall%3A%20MSI%20-%20%22)%20AND%20(name%20of%20it%20contains%20%22%20-%20Windows%22)%20AND%20(source%20of%20it%20%3D%20%22Configuration%3A%20Generate%20uninstall%20tasks%20for%20all%20MSI%20applications%20on%20target%20computer%20-%20Windows%22)%20)
Success Criteria

This action will be considered successful when the applicability relevance evaluates to false.


Sharing

Social Media:
Share this page on Yammer

Comments

Log In or Register to leave comments!