简体   繁体   English

通过管道传递多个值

[英]Pass multiple values through pipeline

I want to be able to pass multiple values through the pipeline for a module I am building. 我希望能够通过管道为我正在构建的模块传递多个值。 The module is built around a specific SQL Database Schema so i designed this function to return all databases of that schema: 该模块是围绕特定的SQL数据库模式构建的,所以我设计了这个函数来返回该模式的所有数据库:

function Get-TRISDatabases {
    param (
        [parameter(mandatory=$false)]
        [string]$ServerAddress = "localhost",
        [parameter(mandatory=$false)]
        [switch]$Name
    )
    Begin {
        $Query_GetDatabases = "
            SELECT Name, create_date, user_access_desc, state_desc, recovery_model_desc FROM   sys.databases WHERE  CASE
                WHEN state_desc = 'ONLINE' THEN OBJECT_ID(QUOTENAME(name) + '.[dbo].[CRISFiles]', 'U')
            END IS NOT NULL
        "
    }

    Process {
        ## Get Data
        $rValue = Invoke-Sqlcmd2 -Query $Query_GetDatabases -ServerInstance $ServerAddress

        ## Return values
        if ($Name) {
            Return $rValue.Name
        }
        else {
            Return $rValue
        }
    }

    End {
        ## Purge Variables
        Remove-Variable Query_GetDatabases, rValue
    }
}

This works fine but what I want is to be able to pass both the $serverAddress and the $rValue variables through the pipeline. 这工作正常,但我想要的是能够通过管道传递$ serverAddress和$ rValue变量。 This is so i can run other commands like so: 这样我可以运行其他命令:

Get-TRISDatabases -name -ServerAddress "server1" | Remove-TRISDeadLinks

Instead of what i have at the moment which would be: 而不是我现在所拥有的:

Get-TRISDatabases -name -ServerAddress "server1" | Remove-TRISDeadLinks -ServerAddress "server1"

I tried putting the server address in the rValue object as well but i would have to change every other script to user the server address rValue.ServerAddress for that to work. 我尝试将服务器地址放在rValue对象中,但是我必须更改每个其他脚本以使用服务器地址rValue.ServerAddress来使其工作。

Remove-TRISDeadLinks Param block: 删除-TRISDeadLinks Param块:

param (
    [parameter(
        Mandatory=$false, Position=1,ValueFromPipeline=$false
    )]
    $ServerAddress = "localhost",

    [parameter(
        Mandatory=$false, Position=2,ValueFromPipeline=$true
    )]
    $Databases
)

- -

.EXAMPLE
Remove-TRISBrokenLinks -ServerAddress "localhost" -Databases "Database1"

    Manually entering the server address and database

.EXAMPLE 
Remove-TRISBrokenLinks -ServerAddress "localhost" -Databases "Database1", "Database2"

    Runs against an array of databases names

.EXAMPLE 
Get-TRISDatabases -ServerAddress "Localhost" -name | Remove-TRISBrokenLinks

    Uses the Get-TRISDatabases function to pass all TRIS databases to the function

Generally, a process script block allows outputting multiple objects just like any other script block; 通常, process脚本块允许像任何其他脚本块一样输出多个对象; eg: 例如:

PS> 'one', 'two' | & { param([Parameter(ValueFromPipeline)] $str) process { $str; 'hi' } } 
one
hi
two
hi

Remember that return <object> is just syntactic sugar for outputting <object> and returning from the script block, and that you don't need return to output an object. 请记住, return <object>只是用于输出<object> 从脚本块返回的语法糖,并且您不需要return 输出对象。


Given your specific requirements, however - binding two parameters of another function with your function's output via the pipeline - you need to: 但是,根据您的具体要求 - 通过管道将函数输出的另一个函数的两个参数绑定 - 您需要:

  • make Get-TRISDatabases output a custom object whose properties are the arguments to pass to both parameters. make Get-TRISDatabases输出一个自定义对象,其属性是传递给这两个参数的参数。

  • define your other function to bind those properties from the pipeline by property name . 定义您的其他函数以通过属性名称绑定管道中这些属性。

# Get-TRISDatabases ...

Process {
    ## Get Data
    $rValue = Invoke-Sqlcmd2 -Query $Query_GetDatabases -ServerInstance $ServerAddress

    # Output a custom object with both $rValue and the server address
    [pscustomobject] @{
      ServerAddress = $ServerAddress
      Databases = if ($Name) { $rValue.Name } else { $rValue }
    }

}

# ...

Then define your Remove-TRISDeadLinks param() block as follows (PSv3+; note that parameters are non-mandatory by default, and that their position is implied by their declaration order): 然后按如下方式定义Remove-TRISDeadLinks param()块(PSv3 +;请注意,默认情况下参数是非强制性的,并且它们的位置由其声明顺序隐含):

# Remove-TRISDeadLinks ...
[CmdletBinding()]
param (
    [Parameter(ValueFromPipelineByPropertyName)]
    $ServerAddress = "localhost"
    ,   
    [Parameter(ValueFromPipelineByPropertyName)]
    $Databases
)

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

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