Async Powershell Remoting and Variables

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");

August 21st, 2015 5:37pm

Hi Tony,

As the issue you post is much more related to the C# code, in order to get your issue fixed effectively you can post you issue on the develop forum as below:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral

Best Regards,

Elaine

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 3:58am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics