Solved

Getting PowerShell to Work With SQL Server 2012

Posted on 2014-12-10
3
657 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 79

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

786 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