So if I’m reading this correctly… essentially what you’re trying to do is have a function that takes a script path and simply executes it. Or something along those lines.
If you need to read the contents of a script, you want to read it as a text file:
Get-Content -Path $FilePath
If you want to define an input string as an executable script block, you can do so like this:
$Code = [ScriptBlock]::Create()
$Code.Invoke()
If you want to log what a script is doing, though, you’re better off using Start-Transcript and Stop-Transcript
function Test-It {
#Scope is function only
$myVar = "Powershell"
}
function Test-AnotherThing {
param (
$MyVar
)
"Your variable is $myVar"
}
Test-It
Test-AnotherThing
The $MyVar is only in the scope of the Test-It function, so the result is:
Test-It
Test-AnotherThing
Your variable is
You need to understand how to pass information around in your functions. This really where you should be reading a book to understand how to build functions because there are a lot of concepts to understand. A simple fix is something like this:
function Test-It {
#Scope is function only
$myVar = "Powershell"
#Return the value
$myVar
}
function Test-AnotherThing {
param (
$MyVar
)
"Your variable is $myVar"
}
#Get the results of Test-It
$results = Test-It
#Pass the results of Test-It to Test-AnotherThing
Test-AnotherThing -MyVar $results
Output:
$results = Test-It
Test-AnotherThing -MyVar $results
Your variable is Powershell
There are other things you can do like change the variable to be global, but that is almost ALWAYS the wrong way to do things.