Home > Software design >  Trying to extract version info and save to INI file on Windows 11 with PowerShell
Trying to extract version info and save to INI file on Windows 11 with PowerShell

Time:12-01

I am not familiar with PowerShell and I am struggling at the outset.

My task I want to achieve:

  1. Get version number from EXE file.

I know I can get that:

(Get-Item "MeetSchedAssistSetup.exe").VersionInfo.ProductVersionRaw

In the console window it displays:

Major  Minor  Build  Revision
-----  -----  -----  --------
23     0      3      0
  1. Open a INI file. I installed the PSIni module but this line fails:
$ini = Get-IniContent version_meetschedassist2.ini

It says:

Get-IniContent : The 'Get-IniContent' command was found in the module 'PsIni', but the module could not be loaded. For
more information, run 'Import-Module PsIni'.
At line:1 char:8
  $ini = Get-IniContent version_meetschedassist2.ini
         ~~~~~~~~~~~~~~
      CategoryInfo          : ObjectNotFound: (Get-IniContent:String) [], CommandNotFoundException
      FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

How do I load the INI?

My task it to update this bit in the INI:

[MeetSchedAssist Update]
LatestVersion=23.03
LatestVersionString=23.0.3

So I want to:

  • Open INI
  • Extract Version from EXE
  • Update the two INI values from the Version
  • Save INI

I tried using Import-Module:

Import-Module : File C:\Program Files\WindowsPowerShell\Modules\PsIni\3.1.3\PsIni.psm1 cannot be loaded because running scripts is disabled on
this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
  Import-Module PsIni
  ~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : SecurityError: (:) [Import-Module], PSSecurityException
      FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand

No joy.

CodePudding user response:

Here's a very quick and very dirty way of doing this just as an example:

$exe_info = Get-Item -Path '.\MeetSchedAssistSetup.exe'
$ini_path = '.\version_meetschedassist2.ini'
$ini = Get-IniContent -FilePath $ini_path

$ini['MeetSchedAssist Update']['LatestVersion'] = 
    '{0}.{1}{2}' -f $exe_info.VersionInfo.FileMajorPart, 
                    $exe_info.VersionInfo.FileMinorPart, $exe_info.VersionInfo.FileBuildPart
$ini['MeetSchedAssist Update']['LatestVersionString'] =
    '{0}.{1}.{2}' -f $exe_info.VersionInfo.FileMajorPart, 
                    $exe_info.VersionInfo.FileMinorPart, $exe_info.VersionInfo.FileBuildPart

Out-IniFile -FilePath $ini_path -InputObject $ini -Force

Since Get-IniContent gets saved into memory via $ini, you can replace the values with what you need them to be and pass it back out with Out-IniFile specifying $ini as the -InputObject for it. The values are updated using string concatenating of the version info.

  • Related