将Powershell数组作为变量和脚本块传递

将Powershell数组作为变量和脚本块传递,powershell,powershell-3.0,Powershell,Powershell 3.0,我正在测试一个将项目数组传递到powershell的过程,但我在使用ScriptBlock时遇到了困难。我创建了一个测试函数: function TEST { $ScriptBlock = { param ( $BackupPath ="Z:\1\2\", [array]$DBN, #= @("1", "2", "3"), $ServerInstance = "10.10.10.10" ) Foreach ($DBName in $DBN) { write-host "$($DBName

我正在测试一个将项目数组传递到powershell的过程,但我在使用ScriptBlock时遇到了困难。我创建了一个测试函数:

function TEST
{
$ScriptBlock =
{
param (
$BackupPath ="Z:\1\2\",
[array]$DBN, #= @("1", "2", "3"),
$ServerInstance  = "10.10.10.10"
)



Foreach ($DBName in $DBN)
{
write-host "$($DBName)" 
}}}
$DBN = @("1", "2", "3")
TEST -ArgumentList (,$DBN)
然后我调用这个函数:

function TEST
{
$ScriptBlock =
{
param (
$BackupPath ="Z:\1\2\",
[array]$DBN, #= @("1", "2", "3"),
$ServerInstance  = "10.10.10.10"
)



Foreach ($DBName in $DBN)
{
write-host "$($DBName)" 
}}}
$DBN = @("1", "2", "3")
TEST -ArgumentList (,$DBN)

我尝试过各种方法,但它无法循环并返回结果。类似这样的函数中有关ScriptBlock的任何帮助都将非常有用。谢谢

这应该满足您的需求:

# Declare the function
function Test-Array {
    [CmdletBinding()]
    param (
        [string[]] $DBN
    )

    foreach ($DBName in $DBN) {
        Write-Host -Object $DBName;
    }
}

# Call the function
$DBN = @('1', '2', '3');
Test-Array -DBN $DBN;

谢谢,我将在我的主代码中快速尝试该方法。据我所知,我需要使用一个参数列表,以使其与powershell remote配合使用,但这正是我所听到的。现在开始测试!