{"id":4809,"date":"2012-10-18T00:01:00","date_gmt":"2012-10-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/10\/18\/introduction-to-the-sql-server-2012-powershell-provider\/"},"modified":"2012-10-18T00:01:00","modified_gmt":"2012-10-18T00:01:00","slug":"introduction-to-the-sql-server-2012-powershell-provider","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/introduction-to-the-sql-server-2012-powershell-provider\/","title":{"rendered":"Introduction to the SQL Server 2012 PowerShell Provider"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, provides an introduction to the SQL Server 2012 provider for&nbsp;Windows PowerShell.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One of the really cool things about SQL Server&nbsp;2012 is the SQL Server provider for Windows PowerShell. By using the SQL Server provider, you gain access to a driver that permits working with SQL Server&nbsp;2012 in the same way that you might work with the file system. It becomes another drive, just like the other Windows PowerShell drives.<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>For more information about the various Windows PowerShell drives, see this <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=psdrive&amp;sections=7618\" target=\"_blank\">series of Hey, Scripting Guy! Blog posts<\/a>.<\/p>\n<p>After you install SQL Server&nbsp;2012, there are two additional downloads you need to gain access to the SQL Server&nbsp;2012 provider for Windows PowerShell. The first is the <i>Microsoft Windows PowerShell Extensions for Microsoft SQL Server 2012, <\/i>and the second is the <i>Microsoft SQL Server 2012 Shared Management Objects. <\/i>Both of these packages come in x86 and in x64 flavors, and they are available via the <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=29065\" target=\"_blank\">Microsoft Download site<\/a>.<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>To work with the SQL Server provider, you do not have to enable script execution on your system. If you decide to enable script execution on your system, take a look at <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/04\/18\/hey-scripting-guy-april-18-2010.aspx\" target=\"_blank\">The Scripting Wife Sets the Script Execution Policy<\/a>.<\/p>\n<h2>Creating the remote Windows PowerShell console session<\/h2>\n<p>I am going to create a remote Windows PowerShell console session on my computer running SQL Server. The server&rsquo;s is named SQL1, and I am going to pass alternate credentials when I make the connection. I type two commands. The first creates a credential object that I store in a variable named <b>$cred<\/b>. When this command runs, a pop-up dialog box appears as shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6866.hsg-10-18-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6866.hsg-10-18-12-01.png\" alt=\"Image of dialog box\" title=\"Image of dialog box\" \/><\/a><\/p>\n<p>The second command uses the credential object that I just created to make a remote Windows PowerShell session on the remote computer. When I connect to the remote computer, the Windows PowerShell prompt changes to include the name of the remote computer. Here are the commands I type:<\/p>\n<p style=\"padding-left: 30px\">$cred = Get-Credential nwtraders\\administrator<\/p>\n<p style=\"padding-left: 30px\">Enter-PSSession -ComputerName sql1 -Credential $cred<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>I have SQL Server&nbsp;2012 running on a machine that is also running Windows Server&nbsp;2012. As such, Windows PowerShell remoting is already configured&mdash;it just works. In Windows Server&nbsp;2008 or Windows Server&nbsp;2008&nbsp;R2, you need to run the <b>Enable-PSRemoting<\/b> command to configure Windows PowerShell remoting. I have written <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=Enable-PSRemoting&amp;sections=7618\" target=\"_blank\">several Hey, Scripting Guy! Blogs<\/a> about this topic.<\/p>\n<p>Now that I have entered a Windows PowerShell session on the remote server running SQL Server&nbsp;2012, I need to load the SQL Server module for Windows PowerShell. To do this I use the <b>Import-Module<\/b> cmdlet.<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>A<b> <\/b>warning message appears because the SQL Server team used two unapproved cmdlet verbs. The unapproved cmdlet verbs are <i>Encode <\/i>and <i>Decode. <\/i>If you do not want to see the warning message (which does not impact performance) use the <b>DisableNameChecking<\/b><i> <\/i>switch.<\/p>\n<h2>Exploring the SQL Server provider<\/h2>\n<p>The cool thing about the SQL Server provider for Windows PowerShell is that it exposes a SQLSERVER drive. I can use commands like <b>dir<\/b><i> <\/i>(an alias for the <b>Get-ChildItem<\/b> cmdlet) or <b>cd<\/b><i> <\/i>(an alias for the <b>Set-Location<\/b> cmdlet) to navigate through SQL Server. Under the covers, the SQL Server provider is masking the complexity of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sqlserver.management.smo.aspx\" target=\"_blank\">SQL Server Management Objects<\/a> (SMOs), and every level in the hierarchy exposes a different SMO.<\/p>\n<p>To begin, I type <b>dir<\/b><i>. <\/i>The results of this command (and my previous commands) are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6545.hsg-10-18-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6545.hsg-10-18-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>As you can see, there are a number of different folders that provide management capabilities at different levels of the SQL Server hierarchy. For more information, see <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc281947.aspx\" target=\"_blank\">SQL Server PowerShell Provider<\/a>&nbsp;(on Microsoft TechNet, not on MSDN). These folders and the associated capabilities are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8688.hsg-10-18-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8688.hsg-10-18-12-03.png\" border=\"0\" alt=\"\" \/><\/a><\/p>\n<h2>Navigating the SQLSERVER drive<\/h2>\n<p>To navigate the SQLSERVER drive, use the <b>Get-ChildItem<\/b> and the <b>Set-Location<\/b> cmdlets (or use the <b>dir<\/b><i> <\/i>and <b>cd<\/b><i> <\/i>aliases). I like to navigate to a directory, then use <b>dir<\/b><i> <\/i>to produce a list of that directory, and then decide which directory to explore next. This technique is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1362.hsg-10-18-12-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1362.hsg-10-18-12-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>To move up a level in the hierarchy, I use the <b>cd<\/b><i> <\/i>alias with a couple of dots as shown here.<\/p>\n<p style=\"padding-left: 30px\">Cd ..<\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell and SQL Server&nbsp;2012 stuff as I explore a specific SQL Server instance in the hierarchy.<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>For more information about using Windows PowerShell with SQL Server (multiple versions) refer to <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/sql+server\/\" target=\"_blank\">this collection of Hey, Scripting Guy! Blog posts<\/a>.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, provides an introduction to the SQL Server 2012 provider for&nbsp;Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. One of the really cool things about SQL Server&nbsp;2012 is the SQL Server provider for Windows PowerShell. By using the SQL Server provider, you gain access to a driver that permits [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[146,3,176,375,45],"class_list":["post-4809","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-databases","tag-scripting-guy","tag-sql-server","tag-sql-server-2012","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, provides an introduction to the SQL Server 2012 provider for&nbsp;Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. One of the really cool things about SQL Server&nbsp;2012 is the SQL Server provider for Windows PowerShell. By using the SQL Server provider, you gain access to a driver that permits [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4809"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4809\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}