I'm using sqlps powershell module to get some data from my local database. My code is something like this
PS C:\> Import-Module sqlps -DisableNameChecking
PS SQLSERVER:\> cd "SQL\myMachineName\..."
It works correctly, but sometimes I need to connect to a remote server running SQL server, so I use Enter-PSSession. Is this the correct way or can I somehow map that server under local SQLSERVER:\SQL directory?
The PowerShell provider for SQL server can be used to connect to a remote SQL server without PowerShell remoting. For example, at the SQLSERVER: drive, you can change the location to a different SQL Server using
PS SQLSERVER:\> cd "SQL\remoteMachineName\SQLInstanceName"
Related
I am using inno setup to install SQL Server with Database and Wildfly in two different machines (Machine A and Machine B) in a network respectively.
As a first step, I install any one SQL Server (2012, 2014 or 2016 versions) in Machine A.
In second step, I install Wildfly 10 in Machine B and during the installation I have a page where the user inputs the name of Database Server name, Database name and Instance name.
In order to verify the above entries, I have to validate that the Database name and Instance name is available in the Database server.
I was able to connect to the registry of the remote Machine B from Machine A to get the instance name installed using Power shell command with the below query
Invoke-Command -ComputerName MachineB -Command {Get-ItemProperty 'HKLM:\Software\Microsoft\Microsoft SQL Server\Instance Names\SQL'}
However, I could not find the list of databases installed in the registry.
Is there any way to get the list of databases available in the machine so that I can validate the same?
Sql Server does not store database names in the registry. Query sys.databases for db names. Like so,
Invoke-Sqlcmd -ServerInstance myServer\myInstance -Query "select name from sys.databases"
In order to use Invoke-SqlCmd on a system that doesn't have Sql Server installed, install Sql Server Powershell module to the local system.
I'm trying to enable the TCP/IP and Named Pipes Protocols using PowerShell, but I'm unable to due to the following error.
Here is the code I'm using to connect to the Server Protocols.
Is there a way to make SQL Server WMI available through PowerShell?
Here is what the $machine variable looks like after this cmdlet:
$machine = New-Object 'Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer' -ArgumentList 'STORE000013'
I think you are missing one... When running PowerShell (PowerShell.exe) instead of SQL Server PowerShell, first execute the following statements to manually load the required assemblies:
# Load the assemblies
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")
For more information, here:
https://technet.microsoft.com/en-us/library/dd206997(v=sql.105).aspx
We have an EC2 instance for a .NET application and a RDS instance for SQL Server Express. Since Express doesn't support Sql Agent Jobs we need to use sqlcmd and schedule it as a batch. Can the batch file be setup in the RDS instance and if so how? If not, are there other free alternatives without setting up the SQL server in EC2?
You actually cannot RDP into an RDS instance:
From Microsoft SQL Server on Amazon RDS
In order to deliver a managed service experience, Amazon RDS does not
provide shell access to DB instances
So you cannot run sqlcmd directly on the instance. Likewise for batch files, they cannot be installed on the RDS instance.
As #vmachan suggested, you will need to install the client tools on a separate instance in order to access command line functionality.
You can use the command below. It works for me.
"c:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE" -S
"xxxxxxxxx.us-east-1.rds.amazonaws.com" -d database -U user_id -P
password -i "path\SqlQueryName.sql"
or without database name
"c:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE" -S
"xxxxxxxxx.us-east-1.rds.amazonaws.com" -U user_id -P password -i
"path\SqlQueryName.sql"
I think you might have to use some PowerShell commands from your EC2 instance for .Net app against the RDS instance..
A sample command would like below
C:\Users\Administrator> Invoke-Sqlcmd –ServerInstance -Database EduData –Query “SELECT id, name, value, testscore FROM TestScores” –Username -Password
NOTE: I think you would need the SQL Client software installed on your .Net app EC2 instance to be able to do this.
Now you can create your batch jobs on your EC2 instance.
Hope this helps
You cannot call CMD file from RDS instance. Unlike with on Prem sql servers where cmd can be called from a sql server agent job, in RDS it’s not possible. I had a task of stopping DMS task in sql server agent job and then do a fresh restore of the database using backup. This task of stopping DMS wanted to have it in sql agent job, tried a lot & the only solution I see was having this DMS task in a batch file outside the sql agent job and then running it first and then sql agent job to do the restore. Any other useful tips or solutions will be appreciated! Thanks! Note : it’s RDS and not sql on EC2.
I got access to SQL Azure. My problem is I can see Powershell in my local system but not in remote Sql server.
Through SSMS I can access SQL Azure and I can see the Databases and I can create database, but I can't use Powershell.
In my local sql server I used Powershell to send the http request like
$http_Request = New-Object system.Net.WebClient;
$Result = $http_Request.downloadString("url");
It was working fine, but when I try the same thing in SQL Azure I can't. any help?
I have used sqlcmd to capture the required data from a remote server ,which is coming fine .
I have used the below query in a batch file -
>>"Output_hvac.xls" echo %date% %time%
>>"Output_hvac.xls" SQLCMD ...
Now This batch file is kept on a local machine ,thus when it runs it produces an output _hvac excel file .
I want that this excel file be stored in some other server which is not having sql server installed .The batch has to run on the local machine and the excel sheet to be updated on the server .Now I have access to the server but if i am trying to do this with the following query ,it shows access denied .
>>"//172......./d$/Output_hvac.xls" echo %date% %time%
>>"Output_hvac.xls" SQLCMD ...
Now how would i do this .
You need to formally connect to a remote server when you use the sqlcmd command.
Check out the -S argument:
-S [protocol:]server[\instance_name][,port] Specifies the instance of SQL Server to which to connect. It sets the sqlcmd scripting variable SQLCMDSERVER.
Specify server_name to connect to the default instance
of SQL Server on that server computer. Specify server_name [
instance_name ] to connect to a named instance of SQL Server on that
server computer.
If no server computer is specified, sqlcmd connects
to the default instance of SQL Server on the local computer. This
option is required when you execute sqlcmd from a remote computer on
the network. protocol can be tcp (TCP/IP), lpc (shared memory), or np
(named pipes). If you do not specify a server_name [ \instance_name ]
when you start sqlcmd, SQL Server checks for and uses the SQLCMDSERVER
environment variable.
Full article on msdn.