Powershell MessageBox将不需要的数据添加到我的变量

Powershell MessageBox将不需要的数据添加到我的变量,powershell,Powershell,考虑以下Powershell代码: [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) Function MyFunction { ShowMessageBox "Hello World" "Test" return "Somevalue" } Function ShowMessageBox { param ( [string] $message,

考虑以下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”,但我得到的结果是:

好的,一些值


这个额外的“确定”从何而来?

在PowerShell中,您没有分配或通过管道传递给cmdlet的所有内容都将被放入管道。return语句只退出一个函数,在您的情况下可以忽略它

要解决您的问题,请将
Show
方法的结果导入
Out Null

[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

Function MyFunction {
    ShowMessageBox "Hello World" "Test"
    "Somevalue"
}

Function ShowMessageBox {
    param (
        [string] $message,
        [string] $title
    )
    [Windows.Forms.MessageBox]::Show("$message", "$title", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information) | Out-Null
}


$variable = MyFunction
Write-Host "The value of my variable is: $variable."

很想知道。在这件案子之前,我从来没有遇到过任何问题。谢谢。您的函数正在从
[MessageBox]::Show
$null
输出结果。