0

I have a large Powershell script that checks multiple variables on VMs. The script consists of about 80 different functions that are named question1, question2, question3...

At first none of the functions needed parameters, so this code worked.

$number_of_questions = 1..75
foreach($num in $number_of_questions){
        Invoke-Expression question$num
    }

It iterates thru every question

But now i need to add parameters for when i run the functions. And that doesn't work. And i cant find a way to get it to work with arguments

Here´s a testversion of what im trying to do.

function test1($text){
    Write-host "Not argument"
    Write-host $text
}

function test2($text){
    Write-host "Not argument"
    Write-host $text

}

function test3($text){
    Write-host "Not argument"
    Write-host $text

}

function test4($text){
    Write-host "Not argument"
    Write-host $text

}

function test5($text){
    Write-host "Not argument"
    Write-host $text

}

$num = 1..5

foreach($number in $num){
    Invoke-Expression test$number -text "Argument"
}

Does anyone have a solution for running multiple functions with sequenced names that uses parameters.

1 Answer 1

2

Just replace:

Invoke-Expression test$number -text "Argument"

with:

Invoke-Expression "test$number -text `"Argument`""

to make it work.

Sign up to request clarification or add additional context in comments.

Comments

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.