Salvatore Di Fazio
Salvatore Di Fazio

Reputation: 715

Parameter has a function

In Powershell can I pass a function as a param of a function?

Test (!((Get-SPSolution $name).Deployed))

Function Test {
    param ($extFunc)

    do { Start-Sleep -Seconds 30 } while ($extFunc)
}

Upvotes: 0

Views: 92

Answers (2)

Shay Levy
Shay Levy

Reputation: 126842

You should be able to do that without a problem. One thing though, you need to call the test function AFTER it has been declared, otherwise you'll get an error (The term 'Test' is not recognized as the name of a cmdlet).

Here's an example that demonstrate it, you should see messages written to the console every second.

function Test-SPSolution
{
    New-Object PSObject -Property @{Deployed=$false}
}

Function Test {
    param ($extFunc)

    do { 
        Start-Sleep -Seconds 1
        Write-Host "extFunc = $extFunc"
    } while ($extFunc)
}

Test (!(Test-SPSolution foo).Deployed)

Upvotes: 2

CB.
CB.

Reputation: 60938

You can try like this:

Test "(!((Get-SPSolution $name).Deployed))"

Function Test {
    param ($extFunc)

    do { Start-Sleep -Seconds 30 } while ((iex $extFunc))
}

Pass a [string] to your Test function and use invoke-expression cmdlet

Upvotes: 2

Related Questions