0

I am testing a process on passing in an array of items to powershell and I'm having a hard time with the ScriptBlock. I created a test function:

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)" 
}}}

I then call this function:

$DBN = @("1", "2", "3")
TEST -ArgumentList (,$DBN)

I've tried various methods but it can't loop through and give me back the results. Any help on ScriptBlock inside a function like this will be useful. Thanks!

1 Answer 1

2

This should do what you're looking for:

# 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;
1
  • Thanks, I'm going to try that method real quick in my main code. Per my understanding, I need to use an argument list to make it work nice with powershell remote but that's just what I've heard. Testing now! Apr 11, 2014 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.