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

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?

Related

Azure Managed Instance - Virtual Machine

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

Can you import .CSV data into SQL Server using commands from Powershell 3 or 4?

Is there a Powershell script for PowerShell version 3 or version 4 that allows you to import values from a .CSV file into a SQL table?
I'm trying to figure out whether I can script a PowerShell job to import data, using enterprise Servers running older versions of PowerShell (It would be preferred if this can be done without importing any additional modules)
(Nice to have, but not necessary) Also interested to know if there's a way to report the success or failure of that job, to a log file?
This is the script I've tested in PowerShell 5, just can't use this on my older servers:
$database = 'DATABASE';
$server = 'SERVER';
$table = 'TABLE';
$MyData = import-csv \\TESTPATH\licenses_v2.csv -delimiter '|' |
select-object FirstName,LastName,Department,Title;
Write-DbaDataTable -sqlinstance $server -database $database -table $table -inputobject $MyData -KeepNulls;
The answer is yes, I just need to install the DBATools module.

Script to stop/restart local SQL services

Does anyone have a simple script (powershell or command line) that would enable to me to shutdown local SQL Services (SQL Server, Integration services, etc ..) when I don't need them and then perhaps another script to turn them all back on when I do need them?
Currently I am going to Services and then stopping/starting them manually.
We had a need to stop and disable and then enable and start on multiple remote servers, so this is how I handled it:
Stopping:
function stopdisable ($compnam, $svc)
{
(get-service -computername $compnam -name $svc).stop()
set-service -computername $compnam -name $svc -startuptype disabled
}
stopdisable "server1" "servicename1"
stopdisable "server2" "servicename2"
Starting:
function enablestart ($compnam, $svc)
{
set-service -computername $compnam -name $svc -startuptype automatic
(get-service -computername $compnam -name $svc).start()
}
enablestart "server1" "servicename1"
enablestart "server2" "servicename2"
Of course, this is dependent upon you having the appropriate permissions on the remote servers.

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

SqlService WMI query in PowerShell returns nothing

I'm trying to run a simple Get-WmiObject call in PowerShell.
Get-WmiObject -computerName $srv -namespace root\Microsoft\SqlServer\ComputerManagement -class SqlService
When running under an account that has administrator rights to the remote server I get no response. The command does complete, but no data is shown. When running this command under an account that does not have rights to the server I get an "Access Denied" message.
When I run this on the remote server directly I get "Invalid namespace", but the server is definitely our SQL Server 2008.
Why doesn't this kind of script return the object as expected?
Try
Get-WmiObject -computerName $srv -namespace root\Microsoft\SqlServer\ComputerManagement10 -class SqlService
On several machines I have, the namespace is ComputerManagement10.
BTW, just in case, you need to find the namespace names:
Get-WmiObject -ComputerName $srv -NameSpace root\Microsoft\SQLServer -Class "__NAMESPACE" | Select Name
The above command will tell you the right namespace names.
Have you checked services with get-service -computername $srv -include ā€œ*sql*ā€?
I would also check whether the class is present in:
Get-WmiObject -Namespace root\Microsoft\SqlServer\ComputerManagement10 -List

Resources