Hi,
I am creating a script to automatically configure SQL servers in Azure from one of the base Azure SQL VM templates. All of my code works well besides changing the default collation of the instance. The PowerShell code works but the problem is that the
command only runs the setup.exe command correctly if I have an open RDP session to the server. I'm trying to figure out why it won't run without the RDP session and either work around it or change the code to something that will work consistently. For
now, I have it open an RDP session but this method won't work in future when I fully automate the scripts.
Here is my code for the ChangeCollation function:
FUNCTION ChangeCollation
{
param
(
[Parameter(Mandatory=$true)]$DeploymentName,
[Parameter(Mandatory=$true)]$AdminName,
[Parameter(Mandatory=$true)]$AdminPassword,
[Parameter(Mandatory=$true)]$Collation
)
Write-Host ("Rebuilding instance collation, setting to $Collation ... ") -ForegroundColor Green
$setup = (Start-Process -FilePath "C:\SQLServer_12.0_Full\Setup.exe" -ArgumentList "/QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=MSSQLSERVER /SQLSYSADMINACCOUNTS=$DeploymentName\$AdminName /SAPWD=$AdminPassword /SQLCOLLATION=$Collation" -NoNewWindow -Wait -PassThru).ExitCode
If ($setup -eq 0) { Write-Host ("Successfully set collation to $Collation") -ForegroundColor Green}
If ($setup -ne 0) { Write-Host ("Failed to set collation to $Collation") -ForegroundColor Red}
}
I execute it as follows, its in a basic retry loop which tries restarting the services or server if the command fails:
try
{
Invoke-Command -ConnectionUri $uri.ToString() -Credential $credential -ScriptBlock ${function:ChangeCollation} -ArgumentList $DeploymentName, $AdminName, $AdminPassword, $Collation
$NewCollation = Invoke-Command -ConnectionUri $uri.ToString() -Credential $credential -ScriptBlock {Invoke-SQLCmd "SELECT CONVERT (varchar, SERVERPROPERTY('collation'));"}
$NewCollation = $NewCollation.Column1
If ($Collation -ne $NewCollation)
{
Throw "Collation not changed exception!"
}
$Stoploop = $true
}
Catch
{
.......
}
If I have an RDP session open as the local admin which is the same account as I am running the invoke-command with, the setup.exe runs through just fine. Without an RDP session, Invoke waits a minute or two then exits. I can see from the logs on the server
it hasn't tried to change the collation of the instance.
I've tried additional switches on the start-process, like -LoadUserProfile or without -nonewwindow but they made no difference. I have also tried changing the collation with the command sqlservr, seems to work sometimes but its inconsistent
and I don't get the returned result saying that it completed successfully so the code just hangs even after the change.
Does anyone have any idea why an exe wouldn't work via an invoke without the RDP session? Or alternatively a better way to change the collation of the database which is consistent?