The form to hit a regular sql server:
$server = "192.168.1.1"
$myDatabase = "Adventures"
$myUser = "Joe"
$myPass = "BadPass"
Invoke-Sqlcmd -ServerInstance $server -Database $myDatabase -Username $myUser -Password $myPass
For Azure SQL http://www.connectionstrings.com/sql-azure/ tells me to bracket the servername as:
[plkahglkhjl].database.windows.net
but that gets me a error message that the server can not be found or was not accessible. It's behaving as if it were a firewall blocking but I have a open connection in SSMS without problem.
Is there another set of validation/authentication that PowerShell scripts have to deal with?
thx
ConnectionStrings.com is just indicating that you need to replace those fields with your own, not that you actually need brackets.
1) You do not need to bracket your server name
plkahglkhjl.database.windows.net
2) You do need to put the server name in the UserName
Joe#plkahglkhjl
More details here: Error connecting to SQL Azure Database
Related
I am trying to Iterate through a CSV file to do an Update Query in SQL. The CSV has Websites, a nickname for a site, and a resource group. It is having issues reading the website because it contains / which I think is an escape charactor. Is there a way to format WHERE SITES = $($c.sites) so it doesnt throw the following error? Invoke-SQLcmd : Incorrect syntax near 'https:'. The sites look like https://fakesite.com/something
foreach($c in $csv){
$updatequery="
UPDATE [dbo].[Table]
SET SiteName = $($c.sitename), RSG_Name= $($c.Resource_Group)
WHERE SITES = $($c.sites)
GO"}
Invoke-SQLcmd -ServerInstance 'serverinstance' -query $updatequery -Database db```
I would like to send a Telnet command or start a *.bat file when I get a result in a SQL view.
I am using SQL Express and so don't have the SQL Server Agent.
I am trying to launch a cue in a lighting software, 30 minutes before one of our shows starts.
The software listens to Telnet commands or bat files.
Could anyone suggest a free software of a way to do this from within SQL Express Server thru SSMS?
Thanks in advance,
I think the best option you have is to use Windows Task Scheduler. Create a PowerShell script that selects the records from your View and if the count is greater than 0 launch the .bat file .
Example :
$query = "SELECT * FROM MY_VIEW"
$results = Invoke-Sqlcmd -ServerInstance "yourserver\instance" -Database "yourdatabase" -Username "username" -Password "password" -Query $query
if ($results.Count() -gt 0){
C:/path_to_bat_file.bat
}
Links :
https://learn.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps
https://learn.microsoft.com/en-us/powershell/module/nettcpip/test-netconnection?view=win10-ps
I've done some research and I found the command "Invoke-Sqlcmd". I believe this is the command I want to use, but not which arguments. I've tried multiple things, but keep getting "invoke-sqlcmd : Login failed for user 'Hello'". Here is some practice data:
"Forehead" is the name of the server I am executing the PowerShell query from
"Elbow" is the name of the server that is running the SQL database
"Kitten" is the name of the database on "Elbow"
"Fluid" is the name of the table I wish to query
"Hello" is the username to log into "Elbow"
"World" is the password to log into "Elbow"
"Timmy" is the username to log into "Kitten"
"Sticky" is the password to log into "Kitten"
"SELECT * FROM Fluid" is the query I wish to run
With this information, from "Forehead" how can I execute the above query on Kitten? Is there more information I need?
Thank you for any help!
You need to pass the username, password, sql server and database to Invoke-Sqlcmd. Then your query (nice namings... :-)) should work.
$ServerInstance = 'Elbow'
$Database = 'Kitten'
$Username = 'Timmy'
$Password = 'Sticky'
$Query = 'SELECT * FROM Fluid'
Invoke-Sqlcmd -ServerInstance $ServerInstance -Database $Database -Username $Username -Password $Password -Query $Query
I have the following code that I am hoping to use to determine whether there is an active query on the server, and this logic will be used to prevent or allow something else to happen. I'm doing this in Powershell.
$Active = invoke-sqlcmd "sp_whoisactive" -database $DATABASE -serverinstance $SQLSERVER -verbose
Foreach($item in $Active){
If ($item -eq $null) {
"There is nothing active on the database server."
}
Else {
"There is something active on the database server."
}
}
Regardless of whether something runs or not it gives me the following warning:
VERBOSE: Warning: Null value is eliminated by an aggregate or other SET operation.
Which I'm assuming is coming from the internals of sp_whoisactive.
The other problem is that if nothing is active on the server it doesn't display the message, so I'm not sure that logic is actually firing.
Why would it be showing that information and how could I use the results of that SP in that sort of a test?
Note that I'm open to doing this in other ways, the problem is that I just need some logic that could fire to see if there is an active transaction on the server. I'd use something like seeing if there are connections, but there are always background connections. I'm only concerned about actual active transactions that are affecting tables.
EDIT: So I just gave another idea a shot and it seems to have the same problem, I think it's choking on what to do when nothing is returned except for the headers (when nothing is running).
$Active = invoke-sqlcmd "sp_whoisactive" -database $DATABASE -serverinstance $SQLSERVER -verbose | Out-String
select-string -InputObject $Active -Pattern "query" -quiet
It will return True if something is running (so I could use that in a conditional check) but doesn't return False if nothing is running.
It looks like I needed to just understand how Powershell handles null values berrer, which I found using this link.
Basically if I just leave the $Active variable alone and have Powershell test that, it works. So the end code looks like this:
$Active = invoke-sqlcmd "sp_whoisactive" -database $DATABASE -serverinstance $SQLSERVER -verbose
If ($Active) {
"There is something active on the database server. Stopping."
}
Else {
"There is nothing active on the database server." }
And it looks like it works!
MSDN states that there are 4 steps in creating a server alias:
In SQL Server Configuration Manager, expand SQL Server Native Client Configuration, right-click Aliases, and then click New Alias.
In the Alias Name box, type the name of the alias. Client applications use this name when they connect.
In the Server box, type the name or IP address of a server. For a named instance append the instance name.
In the Protocol box, select the protocol used for this alias. Selecting a protocol, changes the title of the optional properties box
to Port No, Pipe Name, or Connection String.
But instead of doing it the "UI way", is there a SQL command to do it?
The configuration of server aliases for clients is a client configuration rather than a SQL server configuration. As such there's no SQL command to create one, much the same as there is no SQL command to create an ODBC connection.
You can script the configuration using WMI through powershell, the place to start is:
http://msdn.microsoft.com/en-us/library/ms179354.aspx
and http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.wmi.aspx
Here is a powershell example using wmi to create an alias
# List existing aliases
Get-WmiObject -Namespace 'root\Microsoft\SqlServer\ComputerManagement10' -Class 'SqlServerAlias' |
Format-Table -Property 'AliasName', 'ServerName', 'ProtocolName', 'ConnectionString'
# Example script to create an alias
$alias = ([wmiclass] '\\.\root\Microsoft\SqlServer\ComputerManagement10:SqlServerAlias').CreateInstance()
$alias.AliasName = 'bob'
$alias.ConnectionString = '1433' #connection specific parameters depending on the protocol
$alias.ProtocolName = 'tcp'
$alias.ServerName = 'example_server'
$alias.Put() | Out-Null;
To edit an existing alias, I would do a delete\insert.
I use this PS to delete the alias entries (SQL 2016):
$alias=Get-WmiObject -namespace 'root\Microsoft\SqlServer\ComputerManagement13' -class 'SqlServerAlias' -filter "AliasName='YourAliasName'"
$alias.Delete()