Macos Get进程在类[PowerShell]中不起作用
我试图在一个类中的函数中运行进程,但它什么也不做。看起来编译器忽略了它,但是,在类之外,它工作得很好Macos Get进程在类[PowerShell]中不起作用,macos,powershell,class,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
class Test
{
[void]TestFunction()
{
<# DOES NOT WORK HERE #>
Get-Process
}
}
[Test]$object = [Test]::new()
$object.TestFunction()
<# Works here #>
#Get-Process
#获取过程
课堂测试
{
[void]TestFunction()
{
获取过程
}
}
[Test]$object=[Test]::new()
$object.TestFunction()
#获取过程
另外,我在macOS上使用带VS代码的PowerShell,这种情况会发生,因为工作方式与普通PowerShell不同
在类方法中,除了那些
在返回语句中提到
因此,需要一个存储函数结果的成员变量。然后可以通过成员访问输出,或者说,使用write host
打印输出
class Test
{
$p =@()
TestFunction()
{
$this.p=Get-Process # or get-process|write-host
}
}
[Test]$object = [Test]::new()
$object.TestFunction()
$object.p
# Prints process list
它什么也不做,因为你说过什么也不做
[void]TestFunction()
void
指示抑制任何输出对象class Test
{
[System.Diagnostics.Process[]]TestFunction()
{
return Get-Process
}
}
并且不需要将结果存储在中间变量中