Solved

Getting PowerShell to Work With SQL Server 2012

Posted on 2014-12-10
3
599 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
  • 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 78

Accepted Solution

by:
David Johnson, CD, MVP earned 500 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
A procedure for exporting installed hotfix details of remote computers using powershell
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now