PowerShell: RunSpace.SessionStateProxy.SetVariable is not setting variable
-
07-02-2021 - |
Question
ConsoleApplication:
class Program
{
static void Main()
{
using (var runSpace = RunspaceFactory.CreateRunspace())
{
runSpace.Open();
runSpace.SessionStateProxy.SetVariable("Var1", "Alex");
using (var pipeline = runSpace.CreatePipeline("C:\\P.ps1"))
{
pipeline.Invoke();
}
}
}
}
P.ps1
$logFile = "C:\MyLog.txt"
function Write-Log ([string] $message)
{
Add-Content -Path $logFile -Value $message
}
Write-Log "Var1: " + $Var1
I expect "Var1: Alex" is written into my log file, but I get "Var1: ".
What did I wrong?
Edit:
My original problem is on the following site: http://nugetter.codeplex.com/workitem/31555
Solution
Because of the space in write-log it sees them as multiple parameters.
Try this
Write-Log "Var1: $Var1"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow