1

Okay this is a weird one but so image my code is,

$string = "Function Get-Something {Write-host 'You got something'}"

now i want to load that function from the string into my PS memory so i can call it... so Invoke-Expression "$string" will do this but i need to use the invoke line within another function. functception..

function Validate-something {

$string = "Function Get-Something {Write-host 'You got something'}"
invoke-expression $string
}
Validate-Something
Get-something

Any ideas on how to make this work?

note: I cant write the string to a file first.

2 Answers 2

1

Dot-source the defining function to have the inner function accessible in the calling scope subsequently:

. Validate-Something
Get-Something
Sign up to request clarification or add additional context in comments.

Comments

1

okay, worked it out...

So I needed to set the scope of the fuctions I'm calling e.g.

function Validate-something {
$string = "Function script:Get-Something {Write-host 'You got something'}"
invoke-expression $string
}
Validate-Something
Get-something

Hope this helps anyone in the future...

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.