Script to stop/restart local SQL services - sql-server

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.

Related

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?

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

Using a batch file on a remote computer's powershell scripts

When using a command like
powershell -command "\\%host1%\supportfiles\mypowershellscript"
from my central server to a remote computer, would it be using that powershell on the remote computer or on my own computer when I run it using the batch?
When you execute:
PowerShell -command <path to a script>
The script, whether it is located on the local machine or on a remote machine, will execute locally. If you want to execute some PowerShell script remotely, you need to enable remoting on the remote machine using Enable-PSRemoting -force. Then on the local machine, you have to execute your script as administrator and your account also has to have admin privileges on the remote machine. Inside your script you can execute parts of the script remotely like so:
$session = New-PSSession remoteComputerName
Invoke-Command -Session $session -Scriptblock { ... script to execute on remoteComputerName ...}
...
Remove-PSSession $session

Deploying database using TFS Deployer and SqlPackage

I'm trying to make PowerShell script that uses SqlPackage to deploy database through TFS Deployer service. Script is working if it is executed directly from command line but it fails when TFS Deployer tries to execute it, the same user account is used for both cases.
The service is running in test mode (TfsDeployer -d), as console application, but it also fails when runs as Windows Service.
This is log file (captured output of SqlPackage and exception caught in PowerShell script):
11/18/2012 17:51:49 | Publishing to database 'databaseName' on server 'machineName'.
11/18/2012 17:51:56 | An error occurred while the batch was being executed.
11/18/2012 17:51:56 | System.Management.Automation.RemoteException: *** Could not deploy package.
These are the only information I was able to collect. Error code (HRESULT) was not present in the caught exception.
PowerShell script goes like this:
try{
$cmd = Join-Path (Get-Item "Env:ProgramFiles(x86)").Value "Microsoft SQL Server\110\DAC\bin\SqlPackage.exe"
$src = Join-Path $source "db.dacpac"
$cfg = Join-Path $source "db.publish.xml"
&$cmd /Action:Publish /SourceFile:$src /Profile:$cfg 2>&1 | ForEach-Object -Process {
Write-Log $_
}
}
catch [Exception] {
Write-Log $_.Exception.ToString()
if($_.Exception.HResult) {
Write-Log "Code: $($_.Exception.HResult.ToString('X'))"
}
}

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