# How to code recipe to look for version number and automatically install package

**URL:** <https://discourse.chef.io/t/how-to-code-recipe-to-look-for-version-number-and-automatically-install-package/19439>\
**Category:** Chef Infra (archive)\
**Created:** [January 13, 2021, 1:50pm UTC](https://discourse.chef.io/t/how-to-code-recipe-to-look-for-version-number-and-automatically-install-package/19439 "2021-01-13T13:50:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hunt](https://avatars.discourse-cdn.com/v4/letter/h/71e660/32.png) [@hunt](https://discourse.chef.io/u/hunt)\
**Post date:** [January 13, 2021, 1:50pm UTC](https://discourse.chef.io/t/how-to-code-recipe-to-look-for-version-number-and-automatically-install-package/19439/1 "2021-01-13T13:50:35Z")

</div>

Community Members,

(Windows Installation)  
Does anyone have any code for a recipe that when it runs will:

1. check a repo/share/etc... for a file version
2. if its greater than the existing version
3. install

I could use some help... mainly with the #1 and #2. Let me know.

Thank you,

---

<div class="post-metadata">

**Author:** ![joejoe19](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@joejoe19](https://discourse.chef.io/u/joejoe19)\
**Post date:** [January 13, 2021, 2:42pm UTC](https://discourse.chef.io/t/how-to-code-recipe-to-look-for-version-number-and-automatically-install-package/19439/2 "2021-01-13T14:42:57Z")

</div>

If the file is generated with the same name regardless of the version you can...

Use the remote\_file resource to download the installer and the installation should happen idempotently by the package resource if the display name is different.

If the display name is not different then you can use a subscription to kickoff the install when a new file is downloaded.

---

<div class="post-metadata">

**Author:** ![hunt](https://avatars.discourse-cdn.com/v4/letter/h/71e660/32.png) [@hunt](https://discourse.chef.io/u/hunt)\
**Post date:** [January 13, 2021, 10:58pm UTC](https://discourse.chef.io/t/how-to-code-recipe-to-look-for-version-number-and-automatically-install-package/19439/3 "2021-01-13T22:58:48Z")

</div>

How do you get Chef to read a config file for versioning stored on a remote storage? I would like to setup a way for chef to read a file for version and run through the logic of whether its greater than the current version installed.

---

<div class="post-metadata">

**Author:** ![craigontour27](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/craigontour27/32/3502_2.png) [@craigontour27](https://discourse.chef.io/u/craigontour27)\
**Post date:** [January 18, 2021, 4:40pm UTC](https://discourse.chef.io/t/how-to-code-recipe-to-look-for-version-number-and-automatically-install-package/19439/4 "2021-01-18T16:40:36Z")

</div>

Hi

I do this in Windows quite often, but look it as defining the version i want, so I don't check the version in the repository. If the version of installer is not available then there's an in issue so Chef run fails.

I don't think the package\_resource is idempotent for version though, so once downloaded - say v 2.0 - if v1.0 is installed you will need to remove it first.

The way we do this is to define 'version' attribute, e.g.  
`myapp_version = '1.0'`

Then during for install recipe, get current version:

```
current_version = powershell_out("(Get-Childitem -Path `
                          'HKLM:\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall') | `
                          Get-ItemProperty | `
                          where-object {$_.DisplayName -eq 'MyApp'} | `
                          Select -ExpandProperty DisplayVersion").stdout.chomp

```

Next, if it is installed and current version is different from desired version, uninstall it:

```
if is_package_installed?('MyApp') &&
  current_version != myapp_version
  windows_package 'MyApp' do
    options "/L*V \"C:\\Program Files\\MyApp\\MyApp_#{current_version}_uninstall.log\""
  installer_type :msi
  action :remove
  end
end

```

Finally, use the windows\_package again to install the version you do want.

Hope it helps.
