/// <inheritdoc/>
        public void OnImport()
        {
            var settings  = Settings.GetSettings();
            var azContext = new AzContext();

            azContext.UpdateContext();
            var telemetryClient    = new AzPredictorTelemetryClient(azContext);
            var azPredictorService = new AzPredictorService(settings.ServiceUri, telemetryClient, azContext);
            var predictor          = new AzPredictor(azPredictorService, telemetryClient, settings, azContext);

            SubsystemManager.RegisterSubsystem <ICommandPredictor, AzPredictor>(predictor);
        }
        /// <summary>
        /// Gets the user account id if the user logs in, otherwise empty string.
        /// </summary>
        private string GetUserAccountId()
        {
            try
            {
                var output = AzContext.ExecuteScript <string>("(Get-AzContext).Account.Id");
                return(output.FirstOrDefault() ?? string.Empty);
            }
            catch (Exception)
            {
            }

            return(string.Empty);
        }
        /// <inheritdoc/>
        protected override void EndProcessing()
        {
            TelemetryClient telemetryClient         = TelemetryUtilities.CreateApplicationInsightTelemetryClient();
            var             settings                = Settings.GetSettings();
            var             nestedPowerShellRuntime = new PowerShellRuntime();
            var             azContext               = new AzContext(nestedPowerShellRuntime)
            {
                IsInternal = (settings.SetAsInternal == true) ? true : false,
            };

            var invocation      = MyInvocation;
            var command         = invocation.MyCommand;
            var boundParameters = new StringBuilder("{");

            foreach (var p in invocation.BoundParameters)
            {
                boundParameters.Append($"{p.Key}: {p.Value.ToString()},");
            }

            if (boundParameters.Length > 1)
            {
                // There is a ',' at the end. We need to remove it.
                boundParameters.Remove(boundParameters.Length - 1, 1);
            }

            boundParameters.Append("}");

            var properties = TelemetryUtilities.CreateCommonProperties(azContext);

            properties.Add("CommandName", command.Name);
            properties.Add("BoundParameters", boundParameters.ToString());

            if (AdditionalTelemetryProperties != null)
            {
                foreach (var property in AdditionalTelemetryProperties)
                {
                    properties.TryAdd(property.Key, property.Value);
                }
            }

            telemetryClient.TrackEvent($"{TelemetryUtilities.TelemetryEventPrefix}/Cmdlet", properties);

#if DEBUG
            WriteDebug($"command name: {command.Name}, parameters: {boundParameters.ToString()}");
#endif

            base.EndProcessing();
        }
Exemple #4
0
        /// <inheritdoc/>
        public void OnImport()
        {
            var settings  = Settings.GetSettings();
            var azContext = new AzContext()
            {
                IsInternal = (settings.SetAsInternal == true) ? true : false,
                SurveyId   = settings.SurveyId?.ToString(CultureInfo.InvariantCulture) ?? string.Empty,
            };

            azContext.UpdateContext();
            var telemetryClient    = new AzPredictorTelemetryClient(azContext);
            var azPredictorService = new AzPredictorService(settings.ServiceUri, telemetryClient, azContext);
            var predictor          = new AzPredictor(azPredictorService, telemetryClient, settings, azContext);

            SubsystemManager.RegisterSubsystem <ICommandPredictor, AzPredictor>(predictor);
        }
        /// <inheritdoc/>
        protected override void BeginProcessing()
        {
            TelemetryClient telemetryClient = TelemetryUtilities.CreateApplicationInsightTelemetryClient();
            var             settings        = Settings.GetSettings();
            var             azContext       = new AzContext()
            {
                IsInternal = (settings.SetAsInternal == true) ? true : false,
                SurveyId   = settings.SurveyId?.ToString(CultureInfo.InvariantCulture) ?? string.Empty,
            };

            var invocation      = MyInvocation;
            var command         = invocation.MyCommand;
            var boundParameters = new StringBuilder("{");

            foreach (var p in invocation.BoundParameters)
            {
                boundParameters.Append($"{p.Key}: {p.Value.ToString()},");
            }

            if (boundParameters.Length > 1)
            {
                // There is a ',' at the end. We need to remove it.
                boundParameters.Remove(boundParameters.Length - 1, 1);
            }

            boundParameters.Append("}");

            var properties = TelemetryUtilities.CreateCommonProperties(azContext);

            properties.Add("CommandName", command.Name);
            properties.Add("BoundParameters", boundParameters.ToString());
            telemetryClient.TrackEvent($"{TelemetryUtilities.TelemetryEventPrefix}/Cmdlet", properties);

#if DEBUG
            WriteDebug($"command name: {command.Name}, parameters: {boundParameters.ToString()}");
#endif

            base.BeginProcessing();
        }
Exemple #6
0
        /// <summary>
        /// Constructs a new instance of <see cref="AzPredictor"/> to use in PowerShell's prediction subsystem.
        /// </summary>
        public AzPredictor()
        {
            // To make import-module fast, we'll do all the initialization in a task.
            // Slow initialization may make opening a PowerShell window slow if "Import-Module" is added to the user's profile.
            Task.Run(() =>
            {
                _settings     = Settings.GetSettings();
                var azContext = new AzContext()
                {
                    IsInternal = (_settings.SetAsInternal == true) ? true : false,
                    SurveyId   = _settings.SurveyId?.ToString(CultureInfo.InvariantCulture) ?? string.Empty,
                };

                RegisterDisposableObject(azContext);

                _azContext = azContext;

                _azContext.UpdateContext();
                _telemetryClient = new AzPredictorTelemetryClient(_azContext);
                _service         = new AzPredictorService(_settings.ServiceUri, _telemetryClient, _azContext);
                _isInitialized   = true;
            });
        }
        /// <summary>
        /// Gets the latest version from the loaded Az modules.
        /// </summary>
        private Version GetAzVersion()
        {
            Version latestAz = DefaultVersion;

            try
            {
                var outputs = AzContext.ExecuteScript <PSObject>("Get-Module -Name Az -ListAvailable");
                foreach (PSObject obj in outputs)
                {
                    string  psVersion = obj.Properties["Version"].Value.ToString();
                    int     pos       = psVersion.IndexOf('-');
                    Version currentAz = (pos == -1) ? new Version(psVersion) : new Version(psVersion.Substring(0, pos));
                    if (currentAz > latestAz)
                    {
                        latestAz = currentAz;
                    }
                }
            }
            catch (Exception)
            {
            }

            return(latestAz);
        }