Solved

Getting PowerShell to Work With SQL Server 2012

Posted on 2014-12-10
3
677 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 80

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 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