I have a problem setting the BackupFile-attribute on the Backup-SqlDatabase command (in Powershell for Linux). It expects a Windows path.
What I did:
I've installed PowerShell on a centos Linux. I started Powershell with pwsh and installed the SqlServer module. I store my db-credentials in $cred.
I then run:
PS /> Backup-SqlDatabase -ServerInstance "-----" -Database "MyDatabase" -BackupFile "~/db-backup.bak" -Credential $cred
The output is:
Backup-SqlDatabase: System.Data.SqlClient.SqlError: Cannot open backup device 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\~\db-backup.bak'. Operating system error 3(The system cannot find the path specified.).
If I omit the BackupFile attribute, it'll actually do a backup - but I have no idea, where it stored the .bak-file (if it even did?)
So, how can I make the BackupFile-attribute accept a Linux path?
Related
I want to connect two VM/server one has data file and batch file another have sql install wanted to trigger command of server A which will hit server b files
Sql command--- in server A
EXEC master..xp_cmdshell 'cd.. && "C:\Program Files\Powershell\6\pwsh.exe" -File "C:\Users\sprasad\Desktop\script\command1.ps1"'
error are
1- import-module: The specified module 'C:\Program
Files\Derivation_19_01_rev0\Core.PowershellModule.TradeLoader.dll' was
not loaded because no valid module file was found in any module
directory.
because files are in server B
I am running sql scripts stored in files (sqlscript.sql) on ServerA against a SQL installation on ServerB using remote powershell. The Powershel module SQLPS is needed on serverA.
see link
steps:
enable remotepowershell on serverB
Enable-PSRemoting -force
in your SQL server, add the user that executes the script on serverA in the list of users with rights on the database.
use a script on severA like:
Import-Module -Name SQLPS -NoClobber -DisableNameChecking -Scope Local Invoke-Sqlcmd -ServerInstance serverA -InputFile sqlscript.sql
-Verbose
Invoke-Sqlcmd -ServerInstance 'serverB' -InputFile sqlscript.sql -Verbose
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.
I installed ms sql server with chocolatey:
choco install SQLServer2012DeveloperEditionWithSP1 -y -f -source 'http://choco.developers.tcpl.ca/chocolatey' -c "$env:WINDIR\temp"
SQL seems to be installed and working well outside of powershell where it doesn't work. I can see the sqlps module with:
Get-Module -listavailable
...
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.0 SQLASCMDLETS
Manifest 1.0 SQLPS
The commands seem to be missing though. I don't have invoke-sqlcmd etc. In theory I should get access to them if I install the module but when I try to import-module sqlps but I get an error about not having a sqlserver drive:
PS C:\WINDOWS\system32> Import-Module SQLPS
Set-Location : Cannot find drive. A drive with the name 'SQLSERVER' does not exist.
At C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\SqlPsPostScript.ps1:1 char:1
+ Set-Location SQLSERVER:
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (SQLSERVER:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
I know several people in my group who went through these steps and did get the correct sql ps setup working.
Any tips or ideas would be very helpful. Thanks.
Good day,
I am guessing that you are using SQL Server 2017, since this is common issue in 2017, as I will explain below. I am not sure what version is used since this question is a bit old and was asked on May 2 '17 at 22:58
The error that you get includes the basic issue
Set-Location : Cannot find drive. A drive with the name 'SQLSERVER' does not exist.
It does not say that the module 'SQLPS' does not exist, but that the module 'SQLSERVER' does not exist
The explanation is that until 2016 SQLPS module was included with the SQL Server installation, but the PowerShell module which we use is the 'SqlServer' module. 'SqlServer' module was included with SQL Server Management Studio (SSMS) 16.x, but if you are using SSMS 2017 (17.x) then 'SqlServer' module must be installed from the PowerShell Gallery.
The procedure to install it is to execute the command:
Install-Module -Name SqlServer
If you get error like PackageManagement\Install-Package : The following commands are already available on this system:...
Then you can enforce the installation using the parameters: -Force and –AllowClobber
Since I am not familiar with your system, I will NOT advice you what to do or say if you should enforce the installation, but this is the solution which I would probably do in most cases like this (according to the information I noticed in this thread)
Install-Module -Name SqlServer -Force –AllowClobber
In order to confirm that the module is instead you can execute the following command:
Get-Module -Name SqlServer -listAvailable | select Name, ModuleType, Version
Check the version of your installation using the command above, and use it in the following command in order to import the newest version (at the time I write this answer the version is 21.0.17279):
Import-Module SqlServer -Version 21.0.17279
That is all... If all went well then you should be able to use all the SQL Server PowerShell commands
Just a side-note for future readers, I was trying to create a sql backup via powershell. The cmdlets ran as they should under an Administrator account, however running the script under a regular user account, I got the following error:
Cannot find a provider with the name 'SqlServer'
Googling that question brought me here, but the answer to my issue was in a forum post here:
https://social.technet.microsoft.com/Forums/windowsserver/en-US/626bb81a-00ba-4239-ad0a-fec32546350a/check-if-drive-exists-if-not-map?forum=winserverpowershell
I encountered a weird issue and hope that somebody may have a fix for this.
Microsoft SQL Server 2014 (SP2-GDR) (KB4505217) - 12.0.5223.6 (X64) (yeah I know...it's a dev server)
Windows 2012 R2
PSVersion 4.0
I load the SQL assemblies in the PS script
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
The following will fail if I run this immediately after loading the assemblies
Get-ChildItem SQLSERVER:\SQL$env:COMPUTERNAME
Get-ChildItem : Cannot find drive. A drive with the name 'SQLSERVER' does not exist.
If I run the following first, I'm fine.
Invoke-Sqlcmd -Query "SELECT TOP 1 * FROM sys.sysobjects" | Out-Null
Get-ChildItem SQLSERVER:\SQL$env:COMPUTERNAME
I just wanted to amend that I just need to run
Invoke-Sqlcmd | Out-Null
This seems to then fully load the assemblies into memory and everything is OK after.
So I added this to my script:
# SQL cmdlets below need some dll imports from system
# These do not get loaded sometime when running under an non-admin account (Cannof find a provider with the name 'SqlServer')
# Running this dummy command seems to load all needed dlls
# Also see: https://www.sqlservercentral.com/forums/topic/unable-access-sql-provider-in-powershell-without-running-an-invoke-sqlcmd-first
Invoke-Sqlcmd | Out-Null
Seems like valuable information that shouldn't get lost, so I thought i'd post it in the highest SO when googling for that particular error.
One could argue that installing the module rather than this dummy method would be cleaner.
As pointed, here are the links that you should refer now.
MSDN Link
Running SQL Server Powershell
Cannot find path 'SQLSERVER' Issue
There is an answer given by Jarret. Simply loading the module won't help actually. These set of commands have to run after that.
Push-Location
cd $sqlpsPath
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
Update-TypeData -PrependPath SQLProvider.Types.ps1xml
update-FormatData -prependpath SQLProvider.Format.ps1xml
Pop-Location
Hope it helps.
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!
We back up our SQL Server databases to NAS, and was curious if it would be possible to back up to a temporary mapped drive, as opposed to the UNC path. I'm hitting a wall.
Using New-PSDrive:
New-PSDrive -Name "Y" -PSProvider FileSystem -Root "\\NAS\Backups" -Scope Global -Persist
Test-Path "Y:\SQLDBA" <# returns True #>
<# Method 1 #>
sqlcmd -E -d master -Q "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -b
<# Method 2 #>
Invoke-Sqlcmd -Database master -Query "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -QueryTimeout 999999999 -verbose -ErrorAction Stop
Remove-PSDrive "Y"
Using net use:
net use Y: \\NAS\Backups /persistent:yes
<# Method 1 #>
sqlcmd -E -d master -Q "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -b
<# Method 2 #>
Invoke-Sqlcmd -Database master -Query "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -QueryTimeout 999999999 -verbose -ErrorAction Stop
net use Y: /delete
Either method gets me the same error:
Cannot open backup device 'Y:\SQLDBA\SQLDBA.bak'. Operating system error 3(The system cannot find the path specified.).
Is this possible?
No, it is not. Mapped drives exist in a Session. The server process (of sql server) has it's own session, so it does not see your mapped drive.
To back up to a network drive when SQL Server is running in a domain user account, the shared drive must be mapped as a network drive in the session where SQL Server is running. If you start Sqlservr.exe from command line, SQL Server sees any network drives you have mapped in your login session.
Please check the following article for reference:
http://msdn.microsoft.com/en-us/library/ms179313.aspx#NetworkShare