Hi All -
I am working on an app that operates primarily over PowerShell. It sends commands across powershell and then processes the result.
I am running into a challenge because, when working with asynchronous PowerShell, it appears that variables can step on each other. In the follow code example, I want V1 and V2 to have different values but they actually have the same. How can I either modify my C# code or the PowerShell script so that the $foo is different?
Thanks for the help!
var Connection = new AllCommander.Plugins.Servers.Servers.Settings.PowershellServerDefinition(); Connection.Hostname = "LocalHost"; Connection.Port = 5985; var C = Connection.GetConnectionInfo(); var Runspace = RunspaceFactory.CreateRunspace(C); Runspace.Open(); var PS1 = PowerShell.Create(); PS1.Runspace = Runspace; var PS2 = PowerShell.Create(); PS2.Runspace = Runspace; var Command1 = @" $foo = 'Test' $foo "; var Command2 = @" Start-Sleep 5 $foo "; PS1.AddScript(Command1); PS2.AddScript(Command2); var T1 = PS1.BeginInvoke(); var T2 = PS2.BeginInvoke(); while(!(T1.IsCompleted && T2.IsCompleted)) { System.Threading.Thread.Sleep(1000); } var V1 = PS1.EndInvoke(T1); var V2 = PS2.EndInvoke(T2); Assert.AreNotEqual (V1[0].ToString(), V2[0].ToString(), "Incorrect Scope");