private async Task RunPowerShellCommand(ServerManagementClient client, NodeResource node,
                                                SessionResource session,
                                                PowerShellSessionResource ps)
        {
            // run a command
            var result =
                await
                client.PowerShell.InvokeCommandAsync(ResourceGroup,
                                                     node.Name,
                                                     session.Name,
                                                     ps.SessionId,
                                                     "dir c:\\");

            Assert.NotNull(result);

            // did the command complete successfully?t
            Assert.True(result.Completed);

            // did we get some results?
            var found = false;

            foreach (var r in result.Results)
            {
                found = true;
                WriteLine(string.Format(" {0}", r.ToJson()));
            }
            Assert.True(found);
        }
        protected async Task RemoveAllNodes(ServerManagementClient client)
        {
            var nodes = client.Node.ListForResourceGroup(ResourceGroup).WhereNotNull();

            if (nodes != null)
            {
                foreach (var each in nodes)
                {
                    await RemoveNode(client, each.Name);
                }
            }
        }
 protected Task RemoveNode(ServerManagementClient client, string nodeName)
 {
     try
     {
         WriteLine(string.Format("Removing Node {0}/{1}", ResourceGroup, nodeName));
         return(client.Node.DeleteAsync(ResourceGroup, nodeName));
     }
     catch
     {
     }
     return(Task.FromResult(false));
 }
        protected async Task RemoveAllGateways(ServerManagementClient client)
        {
            var gateways = client.Gateway.ListForResourceGroup(ResourceGroup).WhereNotNull();

            if (gateways != null)
            {
                foreach (var each in gateways)
                {
                    if (each.Name != GatewayTwo)
                    {
                        await RemoveGateway(client, each.Name);
                    }
                }
            }
        }
        private static async Task <NodeResource> CreateNode(ServerManagementClient client, GatewayResource gateway, string userName, string password)
        {
            var node = await client.Node.CreateAsync(
                ResourceGroup,
                NodeName,
                connectionName : NodeName,
                gatewayId : gateway.Id,
                location : Location,
                userName : userName,
                password : password
                );

            Assert.NotNull(node);
            return(node);
        }
        private async Task RunLongPowerShellCommand(ServerManagementClient client, NodeResource node,
                                                    SessionResource session,
                                                    PowerShellSessionResource ps)
        {
            // run a command
            var result = await client.PowerShell.InvokeCommandAsync(ResourceGroup,
                                                                    node.Name,
                                                                    session.Name,
                                                                    ps.SessionId,
                                                                    "dir c:\\ ; sleep 20 ; dir c:\\windows");

            Assert.NotNull(result);

            // this should return false because 20 seconds is too long...
            // did the command complete successfully?
            Assert.False(result.Completed);

            // did we get some results?
            var found = false;

            foreach (var r in result.Results)
            {
                found = true;
                WriteLine(string.Format(" {0}", r.ToJson()));
            }
            Assert.True(found);

            PowerShellCommandStatus more;

            found = false;
            // go back for some more results.
            do
            {
                more = await client.PowerShell.GetCommandStatusAsync(ResourceGroup,
                                                                     node.Name,
                                                                     session.Name,
                                                                     ps.SessionId,
                                                                     PowerShellExpandOption.Output);

                foreach (var r in more.Results)
                {
                    found = true;
                    WriteLine(string.Format(" {0}", r.ToJson()));
                }
            } while (!(more.Completed ?? false));
            Assert.True(found);
            Assert.True(more.Completed);
        }
        private async Task ListPowerShellSessions(ServerManagementClient client, NodeResource node,
                                                  SessionResource session)
        {
            bool found;
            // list the powershell sessions open
            var sessions = await client.PowerShell.ListSessionAsync(ResourceGroup, node.Name, session.Name);

            Assert.NotNull(sessions);
            found = false;
            foreach (var s in sessions.Value)
            {
                found = true;
                WriteLine(string.Format(" {0}", s.ToJson()));
            }
            Assert.True(found);
        }
        protected async Task RemoveGateway(ServerManagementClient client, string gatewayName)
        {
            try
            {
                WriteLine(string.Format("Removing Gateway {0}/{1}", ResourceGroup, gatewayName));

                await client.Node.ListForResourceGroup(ResourceGroup)
                .WhereNotNull()
                .Where(node => node.GatewayId.MatchesName(gatewayName))
                .Select(node => RemoveNode(client, node.Name));

                await client.Gateway.DeleteAsync(ResourceGroup, gatewayName);
            }
            catch
            {
            }
        }
        private async Task <IPage <NodeResource> > ListNodesInSubscription(ServerManagementClient client)
        {
            // look at all the nodes in the subscription
            var nodes = await client.Node.ListAsync();

            Assert.NotNull(nodes);

            var found = false;

            foreach (var n in nodes)
            {
                found = true;
                WriteLine(string.Format("Found node in subscription: {0}", n.Name));
            }

            // make sure we got *some* back.
            Assert.True(found, "No nodes in collection");
            return(nodes);
        }
        private async Task <GatewayResource> CreateAndConfigureGateway(ServerManagementClient client, string GatewayName)
        {
            GatewayResource gateway;

            // create a gateway
            gateway = await client.Gateway.CreateAsync(
                ResourceGroup,
                GatewayName,
                upgradeMode : UpgradeMode.Automatic,
                location : Location
                );

            Assert.NotNull(gateway);

            WriteLine(string.Format("Created Gateway: {0}", gateway.Name));

            var profile = await client.Gateway.GetProfileAsync(ResourceGroup, GatewayName);

            if (TestingInteractively)
            {
                // stop the service
                StopGateway();

                // install the new profile
                WriteLine(string.Format("Profile:\r\n{0}", profile.ToJson()));
                var encrypted = ProtectedData.Protect(Encoding.UTF8.GetBytes(profile.ToJsonTight()),
                                                      null,
                                                      DataProtectionScope.LocalMachine);
                var path = Environment.ExpandEnvironmentVariables(@"%PROGRAMDATA%") + @"\ManagementGateway";
                Directory.CreateDirectory(path);
                File.WriteAllBytes(path + @"\GatewayProfile.json", encrypted);

                // start the service.
                StartGateway();

                // wait for service to initialize itself.
                Task.Delay(180 * 1000).Wait();
            }
            Assert.NotNull(gateway);

            return(gateway);
        }
        private async Task GetTabCompletionResults(ServerManagementClient client, NodeResource node,
                                                   SessionResource session,
                                                   PowerShellSessionResource ps)
        {
            bool found;
            // try to get tab command completion
            var results = await client.PowerShell.TabCompletionAsync(ResourceGroup,
                                                                     node.Name,
                                                                     session.Name,
                                                                     ps.SessionId,
                                                                     "dir c:\\");

            Assert.NotNull(results);
            found = false;
            foreach (var s in results.Results)
            {
                found = true;
                WriteLine(string.Format(" {0}", s.ToJson()));
            }
            Assert.True(found);
        }