Get-Process 在类中不起作用 [PowerShell]

PHP*_*PHP 3 macos powershell class

我试图在类中的函数中运行进程,但它什么也不做。看起来编译器忽略了它,但是,在类之外它完美地工作。

<# Works here #>
#Get-Process

class Test
{
    [void]TestFunction()
    {
        <# DOES NOT WORK HERE #>
        Get-Process
    }
}

[Test]$object = [Test]::new()
$object.TestFunction()


<# Works here #>
#Get-Process
Run Code Online (Sandbox Code Playgroud)

PS 我在 macOS 上使用 PowerShell 和 VS Code

Cry*_*t32 6

它什么都不做,因为你说什么都不做。

[void]TestFunction()
Run Code Online (Sandbox Code Playgroud)
  1. void 指示抑制任何输出对象。
  2. 方法中没有定义 return 语句。

这是工作示例:

class Test
{
    [System.Diagnostics.Process[]]TestFunction()
    {
        return Get-Process
    }
}
Run Code Online (Sandbox Code Playgroud)

并且无需将结果存储在中间变量中。