From b3f8eed28b62513d129033d9021d7a8e9612d4ba Mon Sep 17 00:00:00 2001 From: Tarun R <> Date: Wed, 29 Jan 2025 15:53:36 +0530 Subject: [PATCH 1/2] Adding registrykey for VS telemetry to identify if VS is installed on agent machine. --- .../Configuration/ConfigurationManager.cs | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/Agent.Listener/Configuration/ConfigurationManager.cs b/src/Agent.Listener/Configuration/ConfigurationManager.cs index 58c8ae43ca..b8574606d8 100644 --- a/src/Agent.Listener/Configuration/ConfigurationManager.cs +++ b/src/Agent.Listener/Configuration/ConfigurationManager.cs @@ -21,6 +21,7 @@ using System.Security.AccessControl; using System.Security.Principal; using Newtonsoft.Json; +using Microsoft.Win32; using Microsoft.VisualStudio.Services.Agent.Listener.Telemetry; namespace Microsoft.VisualStudio.Services.Agent.Listener.Configuration @@ -42,6 +43,9 @@ public sealed class ConfigurationManager : AgentService, IConfigurationManager private ILocationServer _locationServer; private ServerUtil _serverUtil; + private const string VsTelemetryRegPath = @"SOFTWARE\Microsoft\VisualStudio\Telemetry\PersistentPropertyBag\c57a9efce9b74de382d905a89852db71"; + private const string VsTelemetryRegKey = "IsPipelineAgent"; + public override void Initialize(IHostContext hostContext) { ArgUtil.NotNull(hostContext, nameof(hostContext)); @@ -421,6 +425,12 @@ public async Task ConfigureAsync(CommandSettings command) serviceControlManager.GenerateScripts(agentSettings); } + if(PlatformUtil.RunningOnWindows) + { + // add vstelemetry registrykey + this.AddVSTelemetryRegKey(); + } + try { var telemetryData = new Dictionary @@ -577,6 +587,12 @@ public async Task UnconfigureAsync(CommandSettings command) // delete agent runtime option _store.DeleteAgentRuntimeOptions(); + if(PlatformUtil.RunningOnWindows) + { + // delete vstelemetry registrykey + this.DeleteVSTelemetryRegKey(); + } + _store.DeleteSettings(); _term.WriteLine(StringUtil.Loc("Success") + currentAction); } @@ -855,6 +871,56 @@ private void WriteSection(string message) _term.WriteLine(); } + [SupportedOSPlatform("windows")] + private void AddVSTelemetryRegKey() + { + try + { + // Create registry tree under currentuser + var keyPath = VsTelemetryRegPath.Split("\\").ToList(); + RegistryKey currentKey = Registry.CurrentUser; + foreach (string subKey in keyPath) + { + currentKey = currentKey.CreateSubKey(subKey); + } + + //create the VsTelemetryRegKey under currentuser/VsTelemetryRegPath and set value to true + using (RegistryKey key = Registry.CurrentUser.CreateSubKey(VsTelemetryRegPath)) + { + if (key.GetValue(VsTelemetryRegKey) == null) + { + key.SetValue(VsTelemetryRegKey, "s:true", RegistryValueKind.String); + } + } + } + catch (Exception) + { + // ignore failure as this is not critical to agent functionality + Trace.Info("Error while adding VSTelemetry regkey"); + } + } + + [SupportedOSPlatform("windows")] + private void DeleteVSTelemetryRegKey() + { + try + { + // delete the VsTelemetryRegKey under currentuser/VsTelemetryRegPath + using (RegistryKey key = Registry.CurrentUser.OpenSubKey(VsTelemetryRegPath, writable: true)) + { + if (key != null && key.GetValue(VsTelemetryRegKey) != null) + { + key.DeleteValue(VsTelemetryRegKey); + } + } + } + catch (Exception) + { + // ignore failure as this is not critical to agent functionality + Trace.Info("Error while deleting VSTelemetry regkey"); + } + } + [SupportedOSPlatform("windows")] private void CheckAgentRootDirectorySecure() { From 6701dbd2e1a37765b47c11fc4c87f42e2771f482 Mon Sep 17 00:00:00 2001 From: Tarun R <> Date: Wed, 29 Jan 2025 16:29:09 +0530 Subject: [PATCH 2/2] path should not be created it does not exist already. --- .../Configuration/ConfigurationManager.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/Agent.Listener/Configuration/ConfigurationManager.cs b/src/Agent.Listener/Configuration/ConfigurationManager.cs index b8574606d8..45dcf280f2 100644 --- a/src/Agent.Listener/Configuration/ConfigurationManager.cs +++ b/src/Agent.Listener/Configuration/ConfigurationManager.cs @@ -876,18 +876,10 @@ private void AddVSTelemetryRegKey() { try { - // Create registry tree under currentuser - var keyPath = VsTelemetryRegPath.Split("\\").ToList(); - RegistryKey currentKey = Registry.CurrentUser; - foreach (string subKey in keyPath) - { - currentKey = currentKey.CreateSubKey(subKey); - } - //create the VsTelemetryRegKey under currentuser/VsTelemetryRegPath and set value to true - using (RegistryKey key = Registry.CurrentUser.CreateSubKey(VsTelemetryRegPath)) + using (RegistryKey key = Registry.CurrentUser.OpenSubKey(VsTelemetryRegPath, writable: true)) { - if (key.GetValue(VsTelemetryRegKey) == null) + if (key != null && key.GetValue(VsTelemetryRegKey) == null) { key.SetValue(VsTelemetryRegKey, "s:true", RegistryValueKind.String); }