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