1

我希望能够通过管道为我正在构建的模块传递多个值。该模块是围绕特定的 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
    }
}

这很好用,但我想要的是能够通过管道传递 $serverAddress 和 $rValue 变量。这样我就可以像这样运行其他命令:

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

而不是我目前所拥有的:

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

我也尝试将服务器地址放入 rValue 对象中,但我必须更改所有其他脚本以使用服务器地址 rValue.ServerAddress 才能正常工作。

删除-TRISDeadLinks 参数块:

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
4

1 回答 1

3

通常,一个process脚本块允许输出多个对象,就像任何其他脚本块一样;例如:

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

请记住,这return <object>只是用于从脚本块输出<object> return返回的语法糖,您不需要输出对象。


但是,鉴于您的特定要求 -通过管道将另一个函数的两个参数与您的函数输出绑定- 您需要:

  • 使Get-TRISDatabases输出一个自定义对象,其属性是要传递给两个参数的参数。

  • 定义您的其他函数以通过属性名称绑定管道中的这些属性。

# 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 }
    }

}

# ...

然后Remove-TRISDeadLinks param()按如下方式定义您的块(PSv3+;请注意,默认情况下参数是非强制性的,并且它们的位置隐含在它们的声明顺序中):

# Remove-TRISDeadLinks ...
[CmdletBinding()]
param (
    [Parameter(ValueFromPipelineByPropertyName)]
    $ServerAddress = "localhost"
    ,   
    [Parameter(ValueFromPipelineByPropertyName)]
    $Databases
)
于 2018-10-23T03:15:40.460 回答