diff --git a/src/StorageSync/StorageSync.Test/Common/MockEcsManagementInteropClient.cs b/src/StorageSync/StorageSync.Test/Common/MockEcsManagementInteropClient.cs index 2156d1ea33d3..7ca465c9c881 100644 --- a/src/StorageSync/StorageSync.Test/Common/MockEcsManagementInteropClient.cs +++ b/src/StorageSync/StorageSync.Test/Common/MockEcsManagementInteropClient.cs @@ -15,6 +15,8 @@ using Commands.StorageSync.Interop.DataObjects; using Commands.StorageSync.Interop.Enums; using Commands.StorageSync.Interop.Interfaces; +using Microsoft.Azure.Commands.StorageSync.Common; +using Microsoft.Azure.Test.HttpRecorder; using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; @@ -342,7 +344,7 @@ public int GetSyncServerCertificate([In, MarshalAs(UnmanagedType.Bool)] bool isP /// System.Int32. public int GetSyncServerId([MarshalAs(UnmanagedType.BStr), Out] out string serverId) { - serverId = Guid.NewGuid().ToString(); + serverId = HttpMockServer.GetVariable(StorageSyncConstants.SyncServerId, Guid.NewGuid().ToString()); return 0; } diff --git a/src/StorageSync/StorageSync/RegisteredServer/SetRegisteredServerCommand.cs b/src/StorageSync/StorageSync/RegisteredServer/SetRegisteredServerCommand.cs index 1a43f4830b91..d20afc6f08fb 100644 --- a/src/StorageSync/StorageSync/RegisteredServer/SetRegisteredServerCommand.cs +++ b/src/StorageSync/StorageSync/RegisteredServer/SetRegisteredServerCommand.cs @@ -13,6 +13,7 @@ // ---------------------------------------------------------------------------------- using Commands.StorageSync.Interop.DataObjects; +using Commands.StorageSync.Interop.Interfaces; using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; using Microsoft.Azure.Commands.StorageSync.Common; using Microsoft.Azure.Commands.StorageSync.Interop.Enums; @@ -141,6 +142,13 @@ public override void ExecuteCmdlet() bool? identity = default; + IEcsManagement ecsManagement = StorageSyncClientWrapper.StorageSyncResourceManager.CreateEcsManagement(); + int hr = ecsManagement.GetSyncServerId(out string localServerId); + if (hr != 0 || !Guid.TryParse(localServerId, out Guid localServerGuid)) + { + throw new PSArgumentException("Unable to retrieve the local server ID. Ensure the Azure File Sync agent is installed and running."); + } + if (this.IsParameterBound(c => c.InputObject)) { resourceName = InputObject.ServerId; @@ -149,9 +157,14 @@ public override void ExecuteCmdlet() } else { - resourceName = ServerId; resourceGroupName = ResourceGroupName; storageSyncServiceName = StorageSyncServiceName; + resourceName = this.IsParameterBound(c => c.ServerId) ? ServerId : localServerId; + } + + if (!Guid.TryParse(resourceName, out Guid resourceServerGuid) || resourceServerGuid != localServerGuid) + { + throw new PSArgumentException($"The provided ServerId '{resourceName}' does not match the local machine's server ID '{localServerGuid}'. Run this command on the correct server."); } if (this.IsParameterBound(c => c.Identity)) @@ -159,10 +172,10 @@ public override void ExecuteCmdlet() identity = Identity; } - RegisteredServer registeredServer = StorageSyncClientWrapper.StorageSyncManagementClient.RegisteredServers.Get(resourceGroupName, storageSyncServiceName, ServerId); + RegisteredServer registeredServer = StorageSyncClientWrapper.StorageSyncManagementClient.RegisteredServers.Get(resourceGroupName, storageSyncServiceName, resourceName); if (registeredServer == null) { - throw new PSArgumentException($"Server {ServerId} not found."); + throw new PSArgumentException($"Server {resourceName} not found."); } if (registeredServer.ServerRole == InternalObjects.ServerRoleType.ClusterName.ToString()) {