Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -342,7 +344,7 @@ public int GetSyncServerCertificate([In, MarshalAs(UnmanagedType.Bool)] bool isP
/// <returns>System.Int32.</returns>
public int GetSyncServerId([MarshalAs(UnmanagedType.BStr), Out] out string serverId)
{
serverId = Guid.NewGuid().ToString();
serverId = HttpMockServer.GetVariable(StorageSyncConstants.SyncServerId, Guid.NewGuid().ToString());
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
Comment on lines +145 to +149
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IEcsManagement implements IDisposable (and the real implementation wraps a COM object). Wrap the instance returned from CreateEcsManagement() in a using/try/finally to ensure COM handles are released promptly.

Suggested change
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.");
string localServerId;
Guid localServerGuid;
using (IEcsManagement ecsManagement = StorageSyncClientWrapper.StorageSyncResourceManager.CreateEcsManagement())
{
int hr = ecsManagement.GetSyncServerId(out localServerId);
if (hr != 0 || !Guid.TryParse(localServerId, out localServerGuid))
{
throw new PSArgumentException("Unable to retrieve the local server ID. Ensure the Azure File Sync agent is installed and running.");
}

Copilot uses AI. Check for mistakes.
}

Comment on lines +145 to +151
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cmdlet calls into the local agent (GetSyncServerId) before verifying that -Identity was supplied. This can change the error users get when they omit -Identity (and can fail on machines without the agent even though the invocation is already invalid). Consider checking/enforcing -Identity first, then retrieving/validating the local server ID.

Copilot uses AI. Check for mistakes.
if (this.IsParameterBound(c => c.InputObject))
{
resourceName = InputObject.ServerId;
Expand All @@ -149,20 +157,25 @@ 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.");
}
Comment on lines +165 to 168
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New behavior (rejecting a non-local ServerId) doesn’t appear to have a negative test. Since StorageSync.Test/ScenarioTests/RegisteredServerTests.ps1 already covers Set-AzStorageSyncServer success paths, add a scenario test that passes a different GUID and asserts the cmdlet throws the expected PSArgumentException so playback/recording covers this validation.

Copilot generated this review using guidance from repository custom instructions.

Comment on lines +165 to 169
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message conflates two different cases: (1) ServerId is not a valid GUID and (2) it is a valid GUID but doesn’t match the local machine. Consider splitting these conditions so an invalid GUID gets a clear format/validation error, and consider wording that also makes sense when InputObject is used (not just when -ServerId is explicitly provided).

Suggested change
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 (!Guid.TryParse(resourceName, out Guid resourceServerGuid))
{
throw new PSArgumentException($"The server ID '{resourceName}' is not a valid GUID.");
}
if (resourceServerGuid != localServerGuid)
{
throw new PSArgumentException($"The server ID '{resourceName}' does not match the local machine's server ID '{localServerGuid}'. Run this command on the correct server.");
}

Copilot uses AI. Check for mistakes.
if (this.IsParameterBound(c => c.Identity))
{
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())
{
Expand Down
Loading