diff --git a/src/Agent.Listener/CommandLine/RemoveAgent.cs b/src/Agent.Listener/CommandLine/RemoveAgent.cs index 5b62d3e3a0..433f599f07 100644 --- a/src/Agent.Listener/CommandLine/RemoveAgent.cs +++ b/src/Agent.Listener/CommandLine/RemoveAgent.cs @@ -6,5 +6,7 @@ namespace Agent.Listener.CommandLine [Verb(Constants.Agent.CommandLine.Commands.Remove)] public class RemoveAgent : ConfigureOrRemoveBase { + [Option(Constants.Agent.CommandLine.Args.Url)] + public string Url { get; set; } } } diff --git a/src/Agent.Listener/CommandSettings.cs b/src/Agent.Listener/CommandSettings.cs index c133304a87..4819e65151 100644 --- a/src/Agent.Listener/CommandSettings.cs +++ b/src/Agent.Listener/CommandSettings.cs @@ -318,15 +318,18 @@ public string GetToken() public string GetUrl(bool suppressPromptIfEmpty = false) { + // Check for URL from either Configure or Remove commands + string urlValue = Configure?.Url ?? Remove?.Url; + // Note, GetArg does not consume the arg (like GetArgOrPrompt does). if (suppressPromptIfEmpty && - string.IsNullOrEmpty(GetArg(Configure?.Url, Constants.Agent.CommandLine.Args.Url))) + string.IsNullOrEmpty(GetArg(urlValue, Constants.Agent.CommandLine.Args.Url))) { return string.Empty; } - + return GetArgOrPrompt( - argValue: Configure?.Url, + argValue: urlValue, name: Constants.Agent.CommandLine.Args.Url, description: StringUtil.Loc("ServerUrl"), defaultValue: string.Empty, diff --git a/src/Agent.Listener/Configuration/ConfigurationManager.cs b/src/Agent.Listener/Configuration/ConfigurationManager.cs index 45e60a1542..8ef2bbec70 100644 --- a/src/Agent.Listener/Configuration/ConfigurationManager.cs +++ b/src/Agent.Listener/Configuration/ConfigurationManager.cs @@ -576,6 +576,7 @@ public async Task UnconfigureAsync(CommandSettings command) if (isConfigured && hasCredentials) { AgentSettings settings = _store.GetSettings(); + var credentialManager = HostContext.GetService(); // Get the credentials @@ -602,6 +603,14 @@ public async Task UnconfigureAsync(CommandSettings command) IConfigurationProvider agentProvider = (extensionManager.GetExtensions()).FirstOrDefault(x => x.ConfigurationProviderType == agentType); ArgUtil.NotNull(agentProvider, agentType); + // If a URL is provided via command line, override the stored ServerUrl BEFORE checking if hosted + string commandUrl = command.GetUrl(suppressPromptIfEmpty: true); + if (!string.IsNullOrEmpty(commandUrl)) + { + Trace.Info($"Overriding stored ServerUrl '{settings.ServerUrl}' with command line URL '{commandUrl}' for removal operation"); + settings.ServerUrl = commandUrl; + } + bool isHostedServer = await CheckIsHostedServer(agentProvider, settings, credProvider, agentCertManager.SkipServerCertificateValidation); VssCredentials creds = credProvider.GetVssCredentials(HostContext);