?
Solved

Getting PowerShell to Work With SQL Server 2012

Posted on 2014-12-10
3
Medium Priority
?
804 Views
Last Modified: 2014-12-18
I am attempting to load the SQL Server snapins in PowerShell and am having difficulty. Searching the web, I've done the following from plain PowerShell:

$framework=$([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())
Set-Alias installutil "$($framework)installutil.exe"
installutil "d:\Program Files\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\Microsoft.SqlServer.Management.PSProvider.dll"
installutil "d:\Program Files\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\Microsoft.SqlServer.Management.PSSnapins.dll"

Open in new window

Which appears to execute properly with no errors. However, when I execute:
Add-PSSnapin SqlServerCmdletSnapin100

Open in new window

I get the following error:
Add-PSSnapin : The Windows PowerShell snap-in 'SqlServerCmdletSnapin100' is not installed on this machine.
At line:1 char:13
+ Add-PSSnapin <<<<  SqlServerCmdletSnapin100
    + CategoryInfo          : InvalidArgument: (SqlServerCmdletSnapin100:String) [Add-PSSnapin], PSArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

Open in new window

I am not sure what I am doing wrong. Help!
0
Comment
Question by:dbbishop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Author Comment

by:dbbishop
ID: 40492116
I guess a better question, after more research, is why invoke-sqlcmd isn't working. If I execute get-pssnapin -registered, if reports:
Name        : SqlServerCmdletSnapin110
PSVersion   : 2.0
Description : This is a PowerShell snap-in that includes various SQL Server cmdlets.

Name        : SqlServerProviderSnapin110
PSVersion   : 2.0
Description : SQL Server Provider

Open in new window

but when I execute invoke-sqlcmd I get the following 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.
At line:11 char:14
+ invoke-sqlcmd <<<<  -serverinstance myServer -database myDatabase -inputfile ("c:\temp\ComplianceTests\ComplianceTestList.sql") | Out-File 
-FilePath "C:\temp\ComplianceTests\DSKCSRBSPRDDB1.txt"
    + CategoryInfo          : ObjectNotFound: (invoke-sqlcmd:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Open in new window

0
 
LVL 82

Accepted Solution

by:
David Johnson, CD, MVP earned 2000 total points
ID: 40493276
import-module sqlps is what I use
 Invoke-Sqlcmd -Query "SELECT GETDATE() AS TimeOfQuery;" -ServerInstance "Servername\InstanceName"

Open in new window

0
 
LVL 15

Author Closing Comment

by:dbbishop
ID: 40506841
Sorry for the delay; busy work schedule and wife had surgery. Anyway, searching the internet, I found that and I tried import-module sqlps before posting and that did not work.
Strangely enough though, I tried it again and it seems to have worked this time.

It sure takes a lot longer to import the module than it did to load the snap-ins.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question