简体   繁体   English

Powershell变量被赋予函数的结果和传递给函数的参数I.

[英]Powershell variable is assigned the result of a function AND the parameter I passed to the function

I'm running into this over and over in my script. 我在我的剧本中一遍又一遍地遇到这种情况。 I have this line of code: 我有这行代码:

$Errors = Get-DeploymentErrors $uniqueID

When this runs, $Errors is assigned the result of Get-DeploymentErrors and the value of $uniqueID. 运行时,会为$ Errors分配Get-DeploymentErrors的结果和$ uniqueID的值。 I just want $Errors to be assigned the result of Get-DeploymentErrors. 我只想为$ Errors分配Get-DeploymentErrors的结果。

Here is the Get-DeploymentErrors function: 以下是Get-DeploymentErrors函数:

Function Get-DeploymentErrors($uniqueID)
{
$Errors = @()

$conn = New-Object -TypeName System.Data.SqlClient.SqlConnection
$conn.ConnectionString = 'removed connection string'

$cmd = New-Object -TypeName System.Data.SqlClient.SqlCommand
$cmd.Connection = $conn
$cmd.CommandText = "removed sql statement"
$cmd.Parameters.AddWithValue("@uniqueID", $uniqueID)

$conn.Open()
$reader = $cmd.ExecuteReader()

if($reader.HasRows)
{
    While ($reader.Read())
    {
        $error = New-Object -TypeName PSObject

        $error | Add-Member -MemberType NoteProperty -Name StepID -Value $reader["StepID"]
        $error | Add-Member -MemberType NoteProperty -Name DeploymentID -Value $reader["DeploymentID"]
        $error | Add-Member -MemberType NoteProperty -Name MessageID -Value $reader["MessageID"]
        $error | Add-Member -MemberType NoteProperty -Name Severity -Value $reader["Severity"]
        $error | Add-Member -MemberType NoteProperty -Name Message -Value $reader["Message"]
        $error | Add-Member -MemberType NoteProperty -Name StepName -Value $reader["StepName"]
        $error | Add-Member -MemberType NoteProperty -Name CurrentStep -Value $reader["CurrentStep"]
        $error | Add-Member -MemberType NoteProperty -Name TotalSteps -Value $reader["TotalSteps"]
        $error | Add-Member -MemberType NoteProperty -Name CurrentTime -Value $reader["CurrentTime"]

        $Errors += $error
    }
}

return $Errors
}

$cmd.Parameters.AddWithValue() echoes the added parameter, and PowerShell functions return the entire non-captured output on the success output stream , not just the argument of the return keyword. $cmd.Parameters.AddWithValue()回显添加的参数,PowerShell函数返回成功输出流上的整个非捕获输出,而不仅仅是return关键字的参数。

Quoting from about_Return (emphasis mine): 引自about_Return (强调我的):

SHORT DESCRIPTION 简短的介绍
Exits the current scope, which can be a function, script, or script block. 退出当前范围,可以是函数,脚本或脚本块。

LONG DESCRIPTION 详细描述
The Return keyword exits a function, script, or script block. Return关键字退出函数,脚本或脚本块。 It can be used to exit a scope at a specific point, to return a value, or to indicate that the end of the scope has been reached. 它可用于退出特定点的范围,返回值或指示已达到范围的结尾。

Users who are familiar with languages like C or C# might want to use the Return keyword to make the logic of leaving a scope explicit. 熟悉C或C#等语言的用户可能希望使用Return关键字来制作明确保留范围的逻辑。

In Windows PowerShell, the results of each statement are returned as output, even without a statement that contains the Return keyword. 在Windows PowerShell中, 即使没有包含Return关键字的语句,每个语句的结果也会作为输出返回。 Languages like C or C# return only the value or values that are specified by the Return keyword. C或C#等语言仅返回Return关键字指定的值。

Use any of the following methods to suppress the undesired output: 使用以下任一方法来抑制不需要的输出:

  • [void]$cmd.Parameters.AddWithValue("@uniqueID", $uniqueID)
  • $cmd.Parameters.AddWithValue("@uniqueID", $uniqueID) | Out-Null
  • $cmd.Parameters.AddWithValue("@uniqueID", $uniqueID) > $null
  • $param = $cmd.Parameters.AddWithValue("@uniqueID", $uniqueID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM