Azure Managed Instance - Virtual Machine - sql-server

I have setup an Azure VM that is linked to my Managed SQL, but I need to install SSMS and ODBC drivers but the machine tells me nothing can be installed.
Also, I did the install from this (https://learn.microsoft.com/en-us/azure/azure-sql/managed-instance/connect-vm-instance-configure) and it was meant to do the SSMS install as part of the process
Do these need to be done via PowerShell scripts? And if so, does anyone have one spare for this or another idea?
Thanks

It can be done directly, install from Browser using link SSMS and ODBC and can also be done with PowerShell Script
PowerShell Script for Downlod SSMS
#Set file and folder path for SSMS installer .exe
$folderpath="c:\windows\temp"
$filepath="$folderpath\SSMS-Setup-ENU.exe"
#If SSMS not present, download
if (!(Test-Path $filepath)){
write-host "Downloading SQL Server 2016 SSMS..."
$URL = "https://download.microsoft.com/download/3/1/D/31D734E0-BFE8-4C33-A9DE-2392808ADEE6/SSMS-Setup-ENU.exe"
$clnt = New-Object System.Net.WebClient
$clnt.DownloadFile($url,$filepath)
Write-Host "SSMS installer download complete" -ForegroundColor Green
}
else {
write-host "Located the SQL SSMS Installer binaries, moving on to install..."
}
# start the SSMS installer
write-host "Beginning SSMS 2016 install..." -nonewline
$Parms = " /Install /Quiet /Norestart /Logs log.txt"
$Prms = $Parms.Split(" ")
& "$filepath" $Prms | Out-Null
Write-Host "SSMS installation complete" -ForegroundColor Green
PowerShell Script for Downlod ODBC Driver.
$url = "https://download.microsoft.com/download/1/E/7/1E7B1181-3974-4B29-9A47-CC857B271AA2/English/X64/msodbcsql.msi"
$outpath = "c:/odbc.msi"
Invoke-WebRequest -Uri $url -OutFile $outpath
Start-Process -Filepath $outpath -ArgumentList "/qr IACCEPTMSODBCSQLLICENSETERMS=YES"
Use this command to run command above on your Azure VMs :
$vm = Get-AzVM -Name <VM name> -ResourceGroupName <resource group name>
Invoke-AzVMRunCommand -VM $vm -CommandId 'RunPowerShellScript' -ScriptPath "<install odbc or SSMS file path>"
Reference : Azure VM: Update Microsoft ODBC Driver via Powershell
https://gist.github.com/justinsoliz/34324700ea93c7b77b4ac3e132584de7

Related

Initialize database in SQL Server Linux container using Powershell Core

I'm running SQL Server in a Linux container. When the container starts, I'd like some initialization and create a database using Powershell Core.
My Dockerfile looks like this:
FROM mcr.microsoft.com/mssql/server:2017-latest
ENV ACCEPT_EULA="Y" `
DATA_PATH="./data" `
sa_password="MyP#ssw0rd"
VOLUME ${DATA_PATH}
WORKDIR ./init
RUN apt-get update
RUN apt-get install -y powershell
COPY SomeFolder/Initialize-Database.ps1 .
CMD pwsh ./Initialize-Database.ps1 -sa_password $env:sa_password -data_path $env:DATA_PATH -Verbose
In the Powershell script I'm installing the SQLServer module in order to perform SQL queries.
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Install-Module SQLServer
if ($sa_password -ne "_") {
Write-Verbose 'Changing SA login credentials'
$sqlcmd = "CREATE DATABASE my-db"
Invoke-SqlCmd -Query $sqlcmd -ServerInstance "."
}
The Invoke-SqlCmd command fails with a connection error:
A network-related or instance-specific error occurred while
| establishing a connection to SQL Server. The server was not
| found or was not accessible. Verify that the instance name is
| correct and that SQL Server is configured to allow remote
| connections. (provider: TCP Provider, error: 40 - Could not
| open a connection to SQL Server)
How do I check if the SQL instance is started and if not, how can I start it?
When I start a container using this image and exec into the container, Get-SqlInstance -ServerInstance "." -Credential Get-Credential fails with the error 'Failed to connect to server'.
Found it. In the Dockerfile, I'm now calling the Powershell script after SQL has started:
CMD ( /opt/mssql/bin/sqlservr & ) | grep -q "Service Broker manager has started" \
&& pwsh ./Initialize-Database.ps1 -sa_password $SA_PASSWORD -data_path $DATA_PATH -Verbose
https://www.kenmuse.com/blog/devops-sql-server-dacpac-docker

Inno script to identify SQL Server instances installed on client system and skip installing instance if already installed

I am creating a package using inno script and want to install SQL Server SQLEXPR_x64_ENU on the client's machine. I want code which determines installed SQL Server instances (MSSQL$MYSQL and MSSQLSERVER) on the target system. If the instances are already installed, then the script should skip installing the instances again.
I am using following shell script to find instances and calling it by batch file but it is not working.
$server = $env:computername
$object = Get-WmiObject win32_service -ComputerName $server | where {($_.name -like "MSSQL$*" -or $_.name -like "MSSQLSERVER" -or $_.name -like "SQL Server (*") -and $_.name -notlike "*helper*" -and $_.name -notlike "*Launcher*"}
if ($object)
{
echo " One OR More Instances running..."
$instInfo= $object |select Name,StartMode,State, Status
}
else
{
Start-Process -FilePath "C:\sql\SQLEXPR_x64_ENU.exe"
echo " No SQL Instances running..."
}
Is there any Inno script which can find number of SQL Server instances installed?

SQL Powershell Error: Invoke-Sqlcmd : The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet

When I try to run a powershell script I get the following error:
Invoke-Sqlcmd : The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program.
So in Powershell I ran the following:
install-module sqlserver
update-module sqlserver
import-module sqlserver
This all runs fine. However when I rerun my script I still get the same error.
I have also installed powershelltools.msi, downloaded as part of the SQL Server 2014 Feature pack here: https://www.microsoft.com/en-gb/download/details.aspx?id=42295
If I run this command:
Get-Command -Module sqlserver
I get this result:
CommandType Name Version Source
----------- ---- ------- ------
Alias Decode-SqlName 21.0.17224 sqlserver
Alias Encode-SqlName 21.0.17224 sqlserver
Function SQLSERVER: 21.0.17224 sqlserver
Any ideas on what else I should try?
I have SQLServer 2014 and Powershell Version 5
Are you using Powershell Core (v6.1)?
I was having a similar issue and found this SO answer. Turns out the SQLServer module for Powershell Core doesn't include the Invoke-SqlCmd (among others). I switched back to the 64-bit version of Powershell I have installed on Windows 10 (v5.1) and installed, then imported the sqlserver module. Invoke-SQLCmd is now listed.
Install-Module -Name SqlServer -AllowClobber
Import-Module -Name SqlServer -Force
Get-Command -Module SqlServer
Import-Module imports a module only to current powershell session, not globally. Add the import to your script or to profile.
Was searching a solution for the same problem and found the below worked for me.
find-module sqlserver | Install-Module -AllowClobber -Force
Original Answer: https://social.technet.microsoft.com/Forums/en-US/f3a52235-e62a-402e-9b1b-0b0c0cdd17aa/sql-powershell-error-invokesqlcmd-the-term-invokesqlcmd-is-not-recognized-as-the-name-of-a?forum=winserverpowershell
I had the same problem. Apparently I had to unblock all the dll files in the new module folder. In my case C:\Program Files\WindowsPowerShell\Modules\SqlServer.
I found the link here how to do it.
https://www.404techsupport.com/2016/06/24/unblock-files-powershell/
dir -Path [directory path] -Recurse | Unblock-File
Close powershell if you still have a session open.

Change the default installation path of SQL Server Management Studio

Can I change the default installation path when installing SQL Server Management Studio that is C:?
My C: drive is full as it has system applications and I want to install SQL Server Management Studio in D: drive. Is this possible?
There is no direct approach (At last now) to change the default SQL Server Management Studio installation path but you can use Symbolic Links:
What are Symbolic Links?
From The Complete Guide to Creating Symbolic Links (aka Symlinks) on Windows :
Symbolic links are basically advanced shortcuts. Create a symbolic link to an individual file or folder, and that link will appear to be the same as the file or folder to Windows even though it’s just a link pointing at the file or folder.
Windows 10 users
Windows 10 users must first enable Developer Mode from
Settings > Update & Security > For Developers.
How To
Open Command Prompt or Windows PowerShell with Run as Administrator and paste this command before installing SQL Server Management Studio
mkdir "D:\Program Files\Microsoft SQL Server"
mkdir "D:\Program Files (x86)\Microsoft SQL Server"
mklink /J "C:\Program Files\Microsoft SQL Server" "D:\Program Files\Microsoft SQL Server"
mklink /J "C:\Program Files (x86)\Microsoft SQL Server" "D:\Program Files (x86)\Microsoft SQL Server"
Now install SQL Server Management Studio (SSMS).
There may be some other file and folder in drive C like
AppData\Local\Microsoft\Microsoft SQL Server but they aren't bigger
than 1GB.
What if already installed SSMS?
Close all instance of SSMS and rename these folders to anything you wish:
C:\Program Files\Microsoft SQL Server
C:\Program Files (x86)\Microsoft SQL Server
Follow How To steps and move the contents of the folders (cut/paste) to the new location
D:\Program Files\Microsoft SQL Server
D:\Program Files (x86)\Microsoft SQL Server
Yes, it is possible starting from SSMS 18.0.
SQL Server Management Studio (SSMS) 18.0 released for general availability
SSMS can be installed in a custom folder – This has been a long standing request. With SSMS 18, you can install SSMS in any folder and the option is available from both command line and the setup UI.
Doing this need updating registry value with use of powershell script and then running the installation, Post installation registry values are reset to default
Update the Value of default installation Directory
$RegKey =”HKLM:\Software\Microsoft\Windows\CurrentVersion”
Set-ItemProperty -Path $RegKey -Name “ProgramFilesDir” -Value “D:\Program Files”
Set-ItemProperty -Path $RegKey -Name “ProgramFilesDir (x86)” -Value ‘D:\Program Files (x86)’
Get-ItemProperty -Path $RegKey -Name “ProgramFilesDir”
Get-ItemProperty -Path $RegKey -Name “ProgramFilesDir (x86)”
Write-Host “1. Run the SSMS installer and wait for its completion… (Start-Process -Wait)” -ForegroundColor Yellow
$process=”D:\Software\SSMS-Setup-ENU.exe”
$args=”/install”
Start-Process $process -ArgumentList $args -Wait
Write-Host “`nProcess `”$process`” has been executed and is now stopped.” -ForegroundColor DarkGreen
Revert the Value to default installation Directory
$RegKey =”HKLM:\Software\Microsoft\Windows\CurrentVersion”
Set-ItemProperty -Path $RegKey -Name “ProgramFilesDir” -Value “C:\Program Files”
Set-ItemProperty -Path $RegKey -Name “ProgramFilesDir (x86)” -Value ‘C:\Program Files (x86)’
Get-ItemProperty -Path $RegKey -Name “ProgramFilesDir”
Get-ItemProperty -Path $RegKey -Name “ProgramFilesDir (x86)”
update the location of icons in start menu to the updated location of the SSMS file.
In my case I had to browse to
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft SQL Server Tools 17
Right click on SSMS and update the target to the new location of SSMS program files and you are all set to go. For detailed instructions
https://sqlx86.com/2018/06/28/installing-ssms-to-a-different-location/
I have earlier given an answer to this post to do this using powershell by editing registry values.
Since then I have tried different things and here I am again with a new way to install SQL Server management Studio from command line to an user desired path.
Steps to do so are as follows
Download latest version of SSMS from this link
Press Win+Q key to open search on your computer and type in cmd, Cick on Run as administrator on the right hand side pane.
Browse to recently downloaded SSMS media from URL in step 1, In my case it was in D:\Software
Use below command to install it to a different folder, In my case I am installing it on D:\test.
“SSMS-Setup-ENU.exe /Install /quiet /norestart /log D:\Test\log.txt SSMSInstallRoot = D:\test”
And you are all set…
For instructions with screenshot do visit
https://sqlx86.com/2018/12/27/change-the-default-installation-path-for-sql-server-management-studio-using-command-prompt/
I followed the instruction given on microsoft support https://learn.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-2017
I repaired the Visual Studio 2015 IsoShell and its worked for me.
I'm using 18.8 version (latest) and presto!

PowerShell Silent Install Windows Application

To give the background, I’m trying to automate silent install of our Windows application (setup.exe) on a remote machine via PowerShell scripts and it is getting stuck due to a dependency, in our case SQLExpress Edition 2005.
Since our application depends on SQLExpress, during the installation process it tries to install the SQL Server 2005 Express Edition and that's when it gets stuck forever. I can see the process (SQLExpress**.exe) in Task Manager and nothing happens after that. However this works just fine when I logged in to the remote machine and do a manual installation (run setup.exe myself) as well as run through the PowerShell script locally (so that verify no issues with setup.exe or PowerShell script)
I tried different things but of no success. There are no logs or messages anywhere. All I know is, it is stuck while the setup launches the installation of SQLExpress.
Any help will be highly appreciated.
PowerShell command:
Start-Process -FilePath "C:[myapplicatonname]\setup.exe" -ArgumentList '-s -f2"c:\LogFiles\setup.log" -K"XXXXXXX-XXXX-XXXXX;XXXX-XXXX-XXXXXX" -gS' -Wait -PassThru
PS script to silent install application remotely:
$username = "[username]"
$password = "[pwd]"
$cred = new-object -typename System.Management.Automation.PSCredential ` -argumentlist $username, $password
$testCon = Test-Connection [machine name]
echo ($testCon + "==========================")
$dc1 = New-PSSession -ComputerName \\machinename -Credential $cred
Enter-PSSession -Session $dc1
$script = {
$p = Start-Process -FilePath "C:\[application name]\setup.exe" -ArgumentList '-s -f2"c:\LogFiles\setup.log" -K"1A34AQ9-SAHYTH-UMA68;JA34AQ9-YLMT-C7THH" -gS' -Wait -PassThru
}
invoke-command -computername \\machinename -Credential $cred -scriptblock $script
if($p.ExitCode -ne 0)
{
Write-Host " successfully installed"
}
else
{
Write-Host "installer exit code $($p.ExitCode)"
}
Exit-PSSession

Resources