本文介绍了PowerShell变量分配了一个函数的结果和我传递给该函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在剧本里反复碰到这个问题。我有这样的代码行:

$ $ p $ blockquote>

简短描述

退出当前作用域,它可以是一个函数,脚本或脚本块。



详细说明

Return关键字退出一个函数,脚本或脚本块。它可用于在特定点退出范围,返回值或指示范围的末端已到达。



熟悉C或C#等语言的用户可能希望使用Return关键字来使得范围明确的逻辑成为可能。



在Windows PowerShell中,即使没有包含Return关键字的语句,每个语句的结果也会作为输出返回。像C或C#这样的语言只返回一个或多个值由Return关键字指定。


使用以下任何方法来抑制不需要的输出:


  • [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)


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. I just want $Errors to be assigned the result of Get-DeploymentErrors.

Here is the Get-DeploymentErrors function:

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.

Quoting from about_Return (emphasis mine):

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)

这篇关于PowerShell变量分配了一个函数的结果和我传递给该函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 07:19