Exemplo n.º 1
0
        protected void ExecuteClientActionInOCS(object input, string operationDescription, Action <string> action)
        {
            IContextChannel contextChannel = null;

            try
            {
                contextChannel = Channel.ToContextChannel();
            }
            catch (Exception)
            {
                // Do nothing, proceed.
            }

            if (contextChannel != null)
            {
                object context = null;

                using (new OperationContextScope(contextChannel))
                {
                    Operation operation = null;

                    WriteVerboseWithTimestamp(string.Format(Resources.ServiceManagementExecuteClientActionInOCSBeginOperation, operationDescription));

                    try
                    {
                        RetryCall(action);
                        operation = GetOperation();
                    }
                    catch (ServiceManagementClientException ex)
                    {
                        WriteExceptionDetails(ex);
                    }

                    WriteVerboseWithTimestamp(string.Format(Resources.ServiceManagementExecuteClientActionInOCSCompletedOperation, operationDescription));

                    if (operation != null)
                    {
                        context = new ManagementOperationContext
                        {
                            OperationDescription = operationDescription,
                            OperationId          = operation.OperationTrackingId,
                            OperationStatus      = operation.Status
                        };
                    }
                }

                if (context != null)
                {
                    WriteObject(context, true);
                }
            }
            else
            {
                RetryCall(action);
            }
        }
Exemplo n.º 2
0
        protected void ExecuteClientAction(object input, string operationDescription, Action <string> action)
        {
            Operation operation = null;

            WriteVerboseWithTimestamp(string.Format(Resources.ServiceManagementExecuteClientActionBeginOperation, operationDescription));

            RetryCall(action);
            operation = GetOperation();

            WriteVerboseWithTimestamp(string.Format(Resources.ServiceManagementExecuteClientActionCompletedOperation, operationDescription));

            if (operation != null)
            {
                var context = new ManagementOperationContext
                {
                    OperationDescription = operationDescription,
                    OperationId          = operation.OperationTrackingId,
                    OperationStatus      = operation.Status
                };

                WriteObject(context, true);
            }
        }
        protected override void ExecuteCommand()
        {
            base.ExecuteCommand();
            if (CurrentDeploymentNewSM == null)
            {
                throw new ArgumentException(Resources.NoCloudServicePresent);
            }

            ManagementOperationContext context;
            string rdpFilePath = LocalPath ?? Path.GetTempFileName();
            WriteVerboseWithTimestamp(string.Format(Resources.AzureRemoteDesktopBeginOperation, CommandRuntime));
            var desktopFileResponse = this.ComputeClient.VirtualMachines.GetRemoteDesktopFile(this.ServiceName, CurrentDeploymentNewSM.Name, Name + "_IN_0");
            using (var stream = new MemoryStream(desktopFileResponse.RemoteDesktopFile))
            {
                using (var file = File.Create(rdpFilePath))
                {
                    int count;
                    byte[] buffer = new byte[1000];

                    while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        file.Write(buffer, 0, count);
                    }
                }

                var operation = GetOperation(desktopFileResponse.RequestId);

                WriteVerboseWithTimestamp(string.Format(Resources.AzureRemoteDesktopCompletedOperation, CommandRuntime));

                context = new ManagementOperationContext
                {
                    OperationDescription = CommandRuntime.ToString(),
                    OperationStatus = operation.Status.ToString(),
                    OperationId = operation.Id
                };
            }

            if (Launch.IsPresent)
            {
                var startInfo = new ProcessStartInfo
                {
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden
                };
                        
                if (LocalPath == null)
                {
                    string scriptGuid = Guid.NewGuid().ToString();

                    string launchRDPScript = Path.GetTempPath() + scriptGuid + ".bat";
                    using (var scriptStream = File.OpenWrite(launchRDPScript))
                    {
                        var writer = new StreamWriter(scriptStream);
                        writer.WriteLine("start /wait mstsc.exe " + rdpFilePath);
                        writer.Flush();
                    }

                    startInfo.FileName = launchRDPScript;
                }
                else
                {
                    startInfo.FileName = "mstsc.exe";
                    startInfo.Arguments = rdpFilePath;
                }

                Process.Start(startInfo);
            }

            WriteObject(context, true);
        }