Checks to see if the server name is updatable and changes the name with a number of prompts.

.EXAMPLE
Repair-DbaServerName -SqlInstance sql2014 -AutoFix

Checks to see if the server name is updatable and automatically performs the change. Replication or mirroring will be broken if necessary.

.EXAMPLE
Repair-DbaServerName -SqlInstance sql2014 -AutoFix -Force

Checks to see if the server name is updatable and automatically performs the change, bypassing most prompts and confirmations. Replication or mirroring will be broken if necessary.
#>[OutputType("System.String")][CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")]Param([parameter(Mandatory=$true,ValueFromPipeline=$true)][Alias("ServerInstance","SqlServer")][DbaInstanceParameter[]]$SqlInstance,[Alias("Credential")][PSCredential]$SqlCredential,[switch]$AutoFix,[switch]$Force)

if($AutoFix-eq$false){throw"Cannot proceed because some databases are involved in replication. You can run exec sp_dropdistributor @no_checks = 1 but that may be pretty dangerous. Alternatively, you can run -AutoFix to automatically fix this issue. AutoFix will also break all database mirrors."}else{if($Pscmdlet.ShouldProcess("console","Prompt will appear for confirmation to break replication.")){$title="You have chosen to AutoFix the blocker: replication."$message="We can run sp_dropdistributor which will pretty much destroy replication on this server. Do you wish to continue? (Y/N)"$yes=New-ObjectSystem.Management.Automation.Host.ChoiceDescription"&Yes","Will continue"$no=New-ObjectSystem.Management.Automation.Host.ChoiceDescription"&No","Will exit"$options=[System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)$result=$host.ui.PromptForChoice($title,$message,$options,1)

if($result-eq1){throw"Cannot continue"}else{Write-Output"`nPerforming sp_dropdistributor @no_checks = 1."$sql="sp_dropdistributor @no_checks = 1"Write-Debug$sqltry{$null=$server.Query($sql)Write-Output"Successfully executed $sql.`n"}catch{Write-Exception$_throw$_}}}}}elseif($Error-like'*mirror*'){if($AutoFix-eq$false){throw"Cannot proceed because some databases are being mirrored. Stop mirroring to proceed. Alternatively, you can run -AutoFix to automatically fix this issue. AutoFix will also stop replication."}else{if($Pscmdlet.ShouldProcess("console","Prompt will appear for confirmation to break replication.")){$title="You have chosen to AutoFix the blocker: mirroring."$message="We can run sp_dropdistributor which will pretty much destroy replication on this server. Do you wish to continue? (Y/N)"$yes=New-ObjectSystem.Management.Automation.Host.ChoiceDescription"&Yes","Will continue"$no=New-ObjectSystem.Management.Automation.Host.ChoiceDescription"&No","Will exit"$options=[System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)$result=$host.ui.PromptForChoice($title,$message,$options,1)

try{$allsqlservices=Get-Service-ComputerName$server.ComputerNamePhysicalNetBIOS-ErrorActionSilentlyContinue|Where-Object{$_.DisplayName-like"SQL*$instance*"-and$_.Status-eq"Running"}}catch{Write-Warning"Can't contact $servername using Get-Service. This means the script will not be able to automatically restart SQL services."}

if($reportingservice.Status-eq"Running"){if($Pscmdlet.ShouldProcess($server.name,"Reporting Services is running for this instance. Would you like to automatically stop this service?")){$reportingservice|Stop-ServiceWrite-Warning"You must reconfigure Reporting Services using Reporting Services Configuration Manager or PowerShell once the server has been successfully renamed."}}}

if($allsqlservices-eq$null){Write-Warning"Could not contact $($server.ComputerNamePhysicalNetBIOS) using Get-Service. You must manually restart the SQL Server instance."$needsrestart=$true}else{if($Pscmdlet.ShouldProcess($server.ComputerNamePhysicalNetBIOS,"Rename complete! The SQL Service must be restarted to commit the changes. Would you like to restart the $instancename instance now?")){try{Write-Output"`nStopping SQL Services for the $instancename instance"$allsqlservices|Stop-Service-Force-WarningActionSilentlyContinue# because it reports the wrong nameWrite-Output"Starting SQL Services for the $instancename instance."$allsqlservices|Where-Object{$_.DisplayName-notlike"*reporting*"}|Start-Service-WarningActionSilentlyContinue# because it reports the wrong name}catch{Write-Exception$_throw"Could not restart at least one SQL Service."}}}