powershell - 从自定义 cmdlet (Powershell ISE) 进行管道传输后,Intellisense 不起作用

标签 powershell

如果我在 PowerShell ISE 编辑器中键入以下行,我会在 $_ 中的点运算符之后获得 Intellisense多变的:

Get-ChildItem ATextFile.txt | foreach { $_.FullName }

在这种情况下,$_System.IO.FileSystemInfo 的一个实例.编辑器将正确列出该对象的所有可访问成员。

现在,如果我写:
function GetFile {
  return [System.IO.FileInfo]::new(".\ATextFile.txt")
}

GetFile | foreach { $_.FullName }

脚本运行良好,但智能感知在 $_ 中的点运算符后不起作用.

我是否缺少使 IntelliSense 正常工作的语法?也许是“记录”返回值的注释?

最佳答案

您正在寻找 OutputType Param 部分上方的属性:

function GetFile {
    [OutputType([System.IO.FileInfo])]
    Param(

    )

  return [System.IO.FileInfo]::new(".\ATextFile.txt")
}

请考虑重命名您的文件以反射(reflect)批准的动词 e。 G。 Get-File .另请注意 return PowerShell 中不需要语句,因此您的函数应如下所示:
function Get-File 
{
    [OutputType([System.IO.FileInfo])]
    Param
    (
    )

    [System.IO.FileInfo]::new(".\ATextFile.txt")
}

关于powershell - 从自定义 cmdlet (Powershell ISE) 进行管道传输后,Intellisense 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44114119/

相关文章:

powershell - 通过 powershell 脚本在 WSL 中运行 bash 脚本

c# - 使用 System.Management.Automation 调用 powershell 时如何从远程命令传递警告和详细流?

powershell - 具有Name.Contains的Powershell开关语句

powershell - NuGet Uninstall.ps1 - 删除项目引用

windows - WinRM 连接问题?

powershell - Powershell脚本中的进度条可能吗?

powershell - 在Write-Host中间运行命令

performance - Powershell Get-ChildItem 进度问题

linux - 执行脚本后,PowerShell 窗口始终会被清理

powershell - -包含或匹配多个值