How I can restore SQL Server database from mdf? - sql-server-2008-r2

I work in SQL Server 2008 R2 and want to restore the old MSDB file. There are some very important data inside the MSDB file.
CREATE DATABASE DMSDBData
ON ( FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\DMSDBData.mdf' )
LOG ON ( FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\DMSDBLog.ldf')
GO
This script did not help me.

Related

Issues calling a Batch File through PowerShell

I'm having some issues when trying to call a batch file through PowerShell. When I run the batch file itsel, everything works fine; below is my PS Code
cmd.exe /c "C:\Source\BuildSource\SATCOM\EBEM\LCT GUI 02.01.04\run_ebem_lct_020104.bat"
Below is my batch file
#title EBEM LCT 02.01.04
PathTemp=%Path%
# Path=C:\Program Files\jre7\bin;%Path%
Path=C:\Program Files\jre7\bin;%Path%
start javaw -classpath .\EBEM_LCT_020104.jar;comm.jar ebem_lct.system.LCT_Client Path=%PathTemp% PathTemp= exit
and finally, below is what PS is spitting back to me
C:\Users\localadmin\Desktop\PowerShell>PathTemp=C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\HID Global\ActivClient\;C:\Program Files\HID Global\ActivClient\;C:\Program Files\Tumbleweed\Desktop Validator\;C:\Program Files\Tumbleweed\Desktop Validator\x86;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\;C:\Program Files\IVI Foundation\VISA\Win64\Bin\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin;C:\usr\bin;C:\Program Files\PuTTY\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\dotnet\;C:\Users\localadmin\AppData\Local\Microsoft\WindowsApps;C:\Source\TDC-Certs\TDC-Certs\OpenSSL-Win64\bin;C:\Users\localadmin\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\localadmin\.dotnet\tools
'PathTemp' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\localadmin\Desktop\PowerShell># Path=C:\Program Files\jre7\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\HID Global\ActivClient\;C:\Program Files\HID Global\ActivClient\;C:\Program Files\Tumbleweed\Desktop Validator\;C:\Program Files\Tumbleweed\Desktop Validator\x86;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\;C:\Program Files\IVI Foundation\VISA\Win64\Bin\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin;C:\usr\bin;C:\Program Files\PuTTY\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\dotnet\;C:\Users\localadmin\AppData\Local\Microsoft\WindowsApps;C:\Source\TDC-Certs\TDC-Certs\OpenSSL-Win64\bin;C:\Users\localadmin\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\localadmin\.dotnet\tools
'#' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\localadmin\Desktop\PowerShell>Path=C:\Program Files\jre7\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\HID Global\ActivClient\;C:\Program Files\HID Global\ActivClient\;C:\Program Files\Tumbleweed\Desktop Validator\;C:\Program Files\Tumbleweed\Desktop Validator\x86;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\;C:\Program Files\IVI Foundation\VISA\Win64\Bin\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin;C:\usr\bin;C:\Program Files\PuTTY\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\dotnet\;C:\Users\localadmin\AppData\Local\Microsoft\WindowsApps;C:\Source\TDC-Certs\TDC-Certs\OpenSSL-Win64\bin;C:\Users\localadmin\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\localadmin\.dotnet\tools
No need to use cmd /c to invoke a batch file from PowerShell; just invoke it directly, which in the case of a quoted batch-file path requires use of &, the call operator.
Your batch file:
is missing #echo off to suppress echoing the commands before they're being executed
mistakenly tries to set environment variables without the SET command
mistakenly tries to use # to initiate a line comment.
In general, it looks like the code was copied from a Bash shell script and poorly adapted for cmd.exe.
Invocation from PowerShell:
& "C:\Source\BuildSource\SATCOM\EBEM\LCT GUI 02.01.04\run_ebem_lct_020104.bat"
Fixed batch-file content:
#echo off
setlocal
:: Set the window title - this will revert when the batch file exits,
:: so there is little point in doing this, given that `start` below
:: launches the application *asynchronously* and the batch file therefore
:: exits quickly.
title EBEM LCT 02.01.04
:: Temporarily add a folder to the PATH.
:: Thanks to `setlocal`, there is no need to restore the previous path afterwards.
Set "Path=C:\Program Files\jre7\bin;%Path%"
:: Start the application
start javaw -classpath .\EBEM_LCT_020104.jar;comm.jar ebem_lct.system.LCT_Client
The alternative is to make do without a batch file altogether, and do it all in PowerShell:
# Save the previous $env:Path value and temporarily prepend a new folder.
$prevPath, $env:Path = $env:Path, "C:\Program Files\jre7\bin;$env:Path"
# Invoke the (GUI) application, which launches asynchronously.
javaw -classpath .\EBEM_LCT_020104.jar;comm.jar ebem_lct.system.LCT_Client
# Restore the original $env:Path value.
$env:Path = $prevPath

Invoke-Sqlcmd cmdlet was not available. SQL Server PowerShell components may not be installed

I am getting the exception as mentioned when I am trying to execute sql queries using PowerShell
[ERROR] Invoke-Sqlcmd cmdlet was not available.
SQL Server PowerShell components may not be installed.
The weird thing I figured out was with this path $env:PSMODULEPATH. I am having the paths set as follows
%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\;C:\Program Files\WindowsPowerShell\Modules\
Instead of the above if I just move the following path before SQL Server or to the first position it is working fine
C:\Program Files\WindowsPowerShell\Modules\
When I update this is how it looks
%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files\WindowsPowerShell\Modules\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\
So is there any way to move it to the first place or above to the SQL PS module path
NOw you just need to set the environmental variable to point to that path externally:
Set-Item -Path env:psmodulepath -Value "C:\Program Files\WindowsPowerShell\Modules\;%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\;"
This should do our job.
Hope it helps.

'Invoke-Sqlcmd' is not recognized as the name of a cmdlet

We have recently started using SQL Server 2012 SP3 and building the SQL server 2012 using a PowerShell script. There is a requirement in our automation process to run multiple database scripts on a db and I have found Invoke-Sqlcmd very reliable until I found this issue.
When I run Invoke-sqlcmd with a proper set of parameters in PowerShell's debug mode on the system on which the SQL server is installed recently, I don't have problem.
PowershellCommand   : Invoke-Sqlcmd -InputFile $sStrJBSPExecRolePath -ServerInstance $sStrSQLName -ErrorAction Stop
But when I execute same query through a PowerShell automation script after rebuilding the same server, I end up getting below error 
The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I did research online many suggested to Import SQLPS, etc., so for testing I added the below command in my script
get-pssnapin -Registered
Import-Module “sqlps” -DisableNameChecking**
Even after adding the above into the script, I still end up with same error. But when I run the same script manually it runs perfectly fine. I don't understand what is wrong.
PowerShell automation script - This script installs the .Net Framework 3.5, SQL Server 2012, SQL Server 2012 SP3, and then loads the SMO assembly that I use to change SQL settings such as the Max Memory limit of SQL.
Open up PowerShell as an Administrator and install the sqlserver module by Install-Module sqlserver
After the module has installed, the module commands including the Invoke-sqlcmd should be readily available.
You can check the same using Get-Command -Module sqlserver.
If this module is not readily available, you can Import-Module sqlserver after installing it.
This is not a complete solution, but just a work around which is working for me.
When you execute the query from automation the user which is executing that is not having access to the sqlcmd. Execute you command for the directory where your sqlcmd.exe is present.
Just put
CD "C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn".
to get the location for sqlcmd search the location for SQLCMD.exe in the search box.
if not found, you need to install that where it is missing, but in your case I think it is present, you just need to get the location right.
Also you will need set the path variable for the user executing the automation script or else it will only recognize the sqlcmd, but wont execute that.
$env:Path += ";C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\"
you can get this path from you local user for which it is working by $Env:Path

Not able to log the sql server installation progress when running with powershell

I am installing SQL Server 2008R2 with Powershell.
Below is the command
function install{
$Command ="SQL_SERVER_CDs\SQL2008_R2\R2_ENTERPRISE\Setup.exe /q /ACTION=Install /IACCEPTSQLSERVERLICENSETERMS /INDICATEPROGRESS /FEATURES=SQLENGINE,REPLICATION,FULLTEXT /INSTANCENAME=DEMO5 /SECURITYMODE=SQL /SAPWD=Me#inv2011 /SQLSYSADMINACCOUNTS=CORP\R-PITTUR CORP\AIMFUNDS-G-Admin-SQL /SQLSVCACCOUNT=RAMU-PC\RAMU /SQLSVCPASSWORD=***** /AGTSVCACCOUNT=RAMU-PC\RAMU /AGTSVCPASSWORD=***** /ISSVCACCOUNT=RAMU-PC\RAMU /ISSVCPASSWORD=***** /INSTANCEDIR=S:\\ /INSTALLSHAREDDIR=C:\Program Files\Microsoft SQL Server /SQLUSERDBDIR=I:\DB_DATA /SQLUSERDBLOGDIR=H:\DB_LOGS /SQLTEMPDBDIR=T:\DEMO5 /SQLBACKUPDIR=S:\DEMO5 /SQLCOLLATION=SQL_Latin1_General_CP1_CI_AS"
Invoke-Expression $Command
}
When i call this install function, SQL installation is happenening but nothing is displayed in the Powershell console though i put Indicateprogress in the above installation string. How can we display logging of sql server installation. Is it possible to redirect the output to any control like Richtextbox using Powershell.
Some of your command options contain spaces, for eg:
/INSTALLSHAREDDIR=C:\Program Files\Microsoft SQL Server
wrap them in quotes like this:
`"/INSTALLSHAREDDIR=C:\Program Files\Microsoft SQL Server`"
note the use of the backtick character (`) to escape the quote within the string.
/q means quite install, right?
Here's the command I use to run SQL 2008 R2 installs:
$command += 'setup.exe /CONFIGURATIONFILE=`"$configFile`" /SAPWD=`"$sysadminPassword`" /SQLSVCPASSWORD=`"$servicePassword`" /AGTSVCPASSWORD=`"$servicePassword`" /FTSVCPASSWORD=`"$servicePassword`" /ISSVCPASSWORD=`"$servicePassword`"'
Invoke-Expression $command

How to call SQLPS from TSql in SQL Server 2008

I see lots of examples on running SQLPS from SQL Server Agent, but how do you call a powershell script from TSQL on demand?
I'm replacing a C# CLR function with a PowerShell script. The script will simply get a path and date modified for files in a directory structure.
What's the command to call a PowerShell script from a T-SQL stored procedure?
Something like this:
set #sql = 'powershell.exe -file "YourScript.ps1" -nologo'
EXEC xp_cmdshell #sql