简体   繁体   中英

How to access files in a Powershell Binary Module FileList Property

I have a file bundled with a powershell binary module. It has a line in the manifest which details the attached file.

# List of all files packaged with this module
FileList = @(".\assets\MoonPhase.sqlite")

This FileList preoperty seems to be pretty useless, just as a note?

List of all files packaged with this module. As with ModuleList, FileList is to assist you as an inventory list, and is not otherwise processed.

How can I then access this file relative the the module root from a cmdlet?

The following seems to be evaluated only when called when the cmndlet is called from a script. Therefore it is not really part of the module but the invocation as the name would suggest.

string path = this.MyInvocation.PSScriptRoot + "\\assets\\MoonPhase.sqlite";
string path = this.MyInvocation.PSCommandPath + "\\assets\\MoonPhase.sqlite";

The following seems a poor choice

string path = @"C:\Users\Me\Path\Project\bin\Debug\netstandard2.0\assets\MoonPhase.sqlite";

The FileList Property is available inside the MyInvocation object and the files listed in the manifest have absolute paths.

string path = MyInvocation.MyCommand.Module.FileList
#"C:\\Users\\Me\\Path\\Project\\bin\\Debug\\assets\\MoonPhase.sqlite"

It's an IEnumerable so need to get a string out of it with linq, a loop or by casting.

string path = MyInvocation.MyCommand.Module.FileList.First();

The following is also useful for forming relative paths

string path = MyInvocation.MyCommand.Module.ModuleBase
#"C:\\Users\\Me\\Path\\Project\\bin\\Debug"

string path = MyInvocation.MyCommand.Module.ModuleBase + "\\OtherFile.txt"
#"C:\\Users\\Me\\Path\\Project\\bin\\Debug\\OtherFile.txt"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM