考虑一下这个Powershell代码:
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
Function MyFunction {
ShowMessageBox "Hello World" "Test"
return "Somevalue"
}
Function ShowMessageBox {
param (
[string] $message,
[string] $title
)
[Windows.Forms.MessageBox]::Show("$message", "$title", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)
return $null
}
$variable = MyFunction
Write-Host "The value of my variable is: $variable."
我赋值变量$ variable,由函数“MyFunction”返回,这是字符串“Somevalue” .
在返回此字符串之前,我会显示一个消息框 .
然后我打印$ variable的值 . 这应该是“Somevalue”,但我得到的结果是:
好的Somevalue
这个额外的“OK”来自哪里?
1 回答
在PowerShell中,您未分配或管道到cmdlet的所有内容都将被放入管道 . return语句仅退出函数,在您的情况下,您可以省略它 .
要解决您的问题,请将
Show
方法的结果传递给Out-Null
: