Gordon
Gordon

Reputation: 6863

Powershell Set FileVersion

I have a script with associated modules, and I would like to add file versioning. Basically I want to update the file versioning when I sign the code, and the code will get the file version of the PS1 file, then check it against the file version of all the PSM1 files and log an error if they don't match.

That said, I can use

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("somefilepath").FileVersion

to get the info, but I can't find an y information in how to set the version. Is that because file versions are the bailiwick of compiled EXEs and DLLs and I don't really have a way to add file versioning to script files? Or am I just missing something?

Upvotes: 3

Views: 9805

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

I can't find an y information in how to set the version. Is that because file versions are the bailiwick of compiled EXEs and DLLs and I don't really have a way to add file versioning to script files?

You already almost answered your own question, that is exactly it! :)

FileVersionInfo.GetVersionInfo() wraps a call to a native api function (version.dll!GetFileVersionInfo).

The current version of the version API supports a wide range of file formats with PE files (.exe,.dll) being the obvious ones.

Other formats like OLE controls (.ocx), screensavers (.scr), drivers (.drv,.sys), installers (.msi,.msu) and MUI language pack resources (.mui) can all have embedded version information that can be parsed using the same library, so it's not just executables.

Your .ps1 file on the other hand is just a text file - the operating system wouldn't know where in it's content to look for and parse version information.

For this, you'd have to either tack a comment onto the last line of the file and store the version there, store the version info in the file name, abuse a comment-based help keyword field (like .REMARKS for example), or store it in an alternate data stream (assuming all machines in your build/signing tool chain are running NTFS):

# write version info to an alternate data stream
Set-Content -Value "1.4.884.3" -Path script.ps1 -Stream myVersion

# read it back during build
$versionString = Get-Content script.ps1 -Stream myVersion

Upvotes: 4

Related Questions