[英]I have a function that returns a PSCustomObject with specific named properties. How can I get these named properties to appear in autocomplete?
這是一個簡單的 function ,它將文件路徑拆分為各個組件並將它們分配給一些屬性:
function Get-FilePathComponents {
[CmdletBinding()]
param (
[Parameter(Mandatory,Position=0,ValueFromPipeline)]
[String[]]
$Path
)
begin {}
process {
foreach ($P in $Path) {
[PSCustomObject]@{
ContainingFolder = Split-Path $P -Parent
FileBaseName = Split-Path $P -LeafBase
FileFullName = Split-Path $P -Leaf
FileExtension = Split-Path $P -Extension
FullPathNoExtension = [IO.Path]::Combine((Split-Path $P -Parent),(Split-Path $P -LeafBase))
CompletePath = $P
ParentFolder = Split-Path (Split-Path $P -Parent) -Parent
}
}
}
}
然后我像這樣使用 function:
$DestFile = "C:\Images\Wallpapers\SomeCoolWallpaper.jpg"
$Components = Get-FilePathComponents $DestFile
$Components.FileFullName # Outputs SomeCoolWallpaper.jpg
$Components.ContainingFolder # Outputs C:\Images\Wallpapers
我真正需要完成的是以某種方式為返回的 object 及其重要屬性啟用自動完成功能。
下面這張圖正是我想要的:
我能夠通過定義自定義 types.ps1xml 文件並使用與我的參數對應的<ScriptProperty>
成員填充它來使其工作:
<Types>
<Type>
<Name>VSYSFileOps.Object.FilePathComponents</Name>
<ScriptProperty>
<Name>ContainingFolder</Name>
<GetScriptBlock>
$this.ContainingFolder
</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>FileBaseName</Name>
<GetScriptBlock>
$this.FileBaseName
</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>FileFullName</Name>
<GetScriptBlock>
$this.FileFullName
</GetScriptBlock>
</ScriptProperty>
... Etc...
然后將[OutputType('VSYSFileOps.Object.FilePathComponents')]
添加到我的 function 的開頭。 令人驚訝的是,雖然這似乎可行,但一些知識淵博的人告訴我,這是一個非常糟糕的主意,而不是 ps1xml 文件的用途。
我真的很想讓自動完成功能為此工作。 我怎樣才能做到這一點?
任何幫助都將受到極大的歡迎。
編輯:我正在使用帶有官方 PowerShell 擴展和 Powershell Pro Tools 的 VSCode。
If you want to go the custom class route, you can add a custom class as a type definition or by loading the class code in a script file/console:
# class definition
$class = @'
public class FilePathComponents
{
public string ContainingFolder { get;set; }
public string FileBaseName { get;set; }
public string FileFullName { get;set; }
public string FileExtension { get;set; }
public string FullPathNoExtension { get;set; }
public string CompletePath { get;set; }
public string ParentFolder { get;set; }
}
'@
# add class to PowerShell session
Add-Type -TypeDefinition $class
# define function
function Get-FilePathComponents {
[CmdletBinding()]
param (
[Parameter(Mandatory,Position=0,ValueFromPipeline)]
[String[]]
$Path
)
begin {}
process {
foreach ($P in $Path) {
$obj = [FilePathComponents]::new()
$obj.ContainingFolder = Split-Path $P -Parent
$obj.FileBaseName = Split-Path $P -LeafBase
$obj.FileFullName = Split-Path $P -Leaf
$obj.FileExtension = Split-Path $P -Extension
$obj.FullPathNoExtension = [IO.Path]::Combine((Split-Path $P -Parent),(Split-Path $P -LeafBase))
$obj.CompletePath = $P
$obj.ParentFolder = Split-Path (Split-Path $P -Parent) -Parent
$obj
}
}
}
# call your function
$file = Get-FilePathComponents C:\temp\test1\a.csv
# use $file. to auto-populate the properties
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.