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;
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.