简体   繁体   English

如何访问Powershell二进制模块FileList属性中的文件

[英]How to access files in a Powershell Binary Module FileList Property

I have a file bundled with a powershell binary module. 我有一个文件与Powershell二进制模块捆绑在一起。 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? 仅作为注释,此FileList的前置操作似乎毫无用处。

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. 与ModuleList一样,FileList可以作为清单清单来帮助您,并且不会进行其他处理。

How can I then access this file relative the the module root from a cmdlet? 然后,如何从cmdlet相对于模块根目录访问此文件?

The following seems to be evaluated only when called when the cmndlet is called from a script. 仅当从脚本调用cmndlet时,才似乎评估以下内容。 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. FileList属性在MyInvocation对象内部可用,清单中列出的文件具有绝对路径。

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. 这是IEnumerable,因此需要使用linq,循环或强制转换从中获取字符串。

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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