2

This lambda function executes as expected:

$WriteServerName = {

param($server)

Write-Host $server

}

$server = "servername"

$WriteServerName.invoke($server)

servername

However, using the same syntax, the following script prompts for credentials and then exits to the command line (running like this: .\ScriptName.ps1 -ConfigFile Chef.config), implying that the lambda functions aren't executing properly (for testing, each should just output the server name).

Why does the former lambda function return the server name, but the ones in the script don't?

Param(

$ConfigFile

)

Function Main {

#Pre-reqs: get credential, load config from file, and define lambda functions.

$jobs = @()
$Credential = Get-Credential
$Username = $Credential.username
$ConvertedPassword = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.password)
$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($ConvertedPassword)
$Config = Get-Content $ConfigFile -Raw | Out-String | Invoke-Expression

#Define lambda functions

$BootStrap = {

    param($Server)     

    write-host $server

}

$RunChefClient = {
param($Server)     

    write-host $server

}

$SetEnvironment = {

    param($Server)     

    write-host $server

}    

#Create bootstrap job for each server and pass lambda functions to Scriptblock for execution.

if(($Username -ne $null) -and ($Password -ne $null))
{
ForEach($HashTable in $Config)    

    {

        $Server = $HashTable.Server
        $Roles = $HashTable.Roles
        $Tags = $HashTable.Tags
        $Environment = $HashTable.Environment       

        $ScriptBlock = {

            param ($Server,$BootStrap,$RunChefClient,$SetEnvironment)              

                $BootStrap.invoke($Server)

                $RunChefClient.invoke($Server)  

                $SetEnvironment.invoke($Server)                             

        } 

        $Jobs += Start-Job -ScriptBlock $ScriptBlock -ArgumentList @($Server,$BootStrap,$RunChefClient,$SetEnvironment)
    }

}

else {Write-Host "Username or password is missing, exiting..." -ForegroundColor Red; exit}

}

Main

1 Answer 1

1

Without testing, I am going to go ahead and say it's because you are putting your scriptblock executions in PowerShell Jobs and then not doing anything with them. When you start a job, it starts a new PowerShell instance and executes the code you give it along with the parameters you give it. Once it completes, the completed PSRemotingJob object sits there and does nothing until you actually do something with it.

In your code, all the jobs you start are assigned to the $Jobs variable. You can also get all your running jobs with Get-Job:

Get-Job -State Running

If you want to get any of the data returned from your jobs, you'll have to use Receive-Job

# Either
$Jobs | Receive-Job
# Or
Get-Job -State Running | Receive-Job
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.