Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting PowerShell to Work With SQL Server 2012

Posted on 2014-12-10
3
Medium Priority
?
846 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 83

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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…

604 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