Commands / Repair-sqmServerName
Repair-sqmServerName
SQL Configuration sqmSQLTool v1.8.2+ · Repair ✓ -WhatIf supported
Aligns the server name registered inside SQL Server (@@SERVERNAME / sys.servers) with the current Windows hostname or FQDN, after the machine was renamed (e.g. after a VM clone or datacenter move). Windows reports the new name immediately (SERVERPROPERTY('MachineName')), but SQL Server keeps the OLD name as its "local server" entry until sp_dropserver/sp_addserver is run, a drift that often only surfaces once replication, linked-server loopbacks, SSRS, or certificates start failing against the wrong name.
Guard checks before touching anything: the function does NOT blindly run sp_dropserver/sp_addserver on a mismatch. It first checks failover-cluster membership (IsClustered), AlwaysOn AG membership (sys.availability_replicas.replica_server_name), and replication roles (sys.databases: distributor/publisher/subscriber/merge-publisher). In all of these cases other objects still reference the old name, a plain rename would succeed technically but break those references, so the function aborts with Status = 'Blocked' instead. Use -Force only once those references are handled separately or the breakage is accepted deliberately.
Restart required: sys.servers is updated immediately, but the session-level @@SERVERNAME/SERVERPROPERTY('ServerName') only reflects the change after the SQL Server service is restarted. This function does NOT restart the service itself, it sets RestartRequired = $true in the result and stops there.

Parameters

ParameterTypeRequiredDefaultNotes
-SqlInstancestringOptional$env:COMPUTERNAMEPosition 0.
-SqlCredentialPSCredentialOptional, Default: Windows Authentication.
-ForceswitchSwitch$falseSkips the cluster/AG/replication guard checks and forces the rename anyway.
-EnableExceptionswitchSwitch$falseThrows exceptions instead of only reporting them in the result object.

Execution Flow

START Read SERVERPROPERTY('ServerName'/'MachineName'/ 'InstanceName'/'IsClustered') ExpectedName = MachineName (+ \InstanceName if named instance) Registered == Expected? YES Status: AlreadyCorrect return - nothing to do NO -Force given? NO Guard checks (any hit → Blocked) IsClustered = 1? (network name managed by cluster) sys.availability_replicas .replica_server_name = old name? sys.databases: distributor/publisher/ subscriber/merge-publisher? YES ShouldProcess (-WhatIf/-Confirm)? NO Status: WhatIf no change made YES EXEC sp_dropserver @server = OldName removes the OLD local-server entry sp_addserver succeeds? NO Status: Error (CRITICAL) instance now has NO local server entry - manual sp_addserver required YES Status: Changed, RestartRequired = $true @@SERVERNAME itself still reflects the OLD name until the SQL Server service is restarted Return [PSCustomObject] SqlInstance · RegisteredServerName · ExpectedServerName IsClustered · Status · RestartRequired · Message DONE

Return Value

PropertyDescription
SqlInstanceInstance name, as passed in.
RegisteredServerName@@SERVERNAME before the change.
ExpectedServerNameTarget name computed from MachineName/InstanceName.
IsClusteredSERVERPROPERTY('IsClustered') = 1?
StatusAlreadyCorrect · Changed · Blocked · WhatIf · Error
RestartRequired$true if a service restart is still pending for @@SERVERNAME to reflect the new value.
MessageHuman-readable detail message.

Examples

Check the local default instance and repair if needed (with guard checks)
Repair-sqmServerName
Preview only, show which sp_dropserver/sp_addserver steps would run
Repair-sqmServerName -SqlInstance "SQL01\INST01" -WhatIf
Force the rename even though AG/replication/cluster references were detected
Repair-sqmServerName -SqlInstance "SQL01" -Force -Confirm:$false