簡體   English   中英

如何訪問Powershell二進制模塊FileList屬性中的文件

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

我有一個文件與Powershell二進制模塊捆綁在一起。 清單中有一行,詳細描述了附件。

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

僅作為注釋,此FileList的前置操作似乎毫無用處。

與此模塊一起打包的所有文件的列表。 與ModuleList一樣,FileList可以作為清單清單來幫助您,並且不會進行其他處理。

然后,如何從cmdlet相對於模塊根目錄訪問此文件?

僅當從腳本調用cmndlet時,才似乎評估以下內容。 因此,它實際上不是模塊的一部分,而是名稱所暗示的調用。

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

以下似乎是一個糟糕的選擇

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

FileList屬性在MyInvocation對象內部可用,清單中列出的文件具有絕對路徑。

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

這是IEnumerable,因此需要使用linq,循環或強制轉換從中獲取字符串。

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

以下對於形成相對路徑也很有用

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