{"id":926,"date":"2014-07-28T00:01:00","date_gmt":"2014-07-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/28\/troubleshooting-a-powershell-sql-server-script\/"},"modified":"2014-07-28T00:01:00","modified_gmt":"2014-07-28T00:01:00","slug":"troubleshooting-a-powershell-sql-server-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/troubleshooting-a-powershell-sql-server-script\/","title":{"rendered":"Troubleshooting a PowerShell SQL Server Script"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, helps a reader with a Windows PowerShell and SQL Server scripting issue.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a nice cup of Darjeeling tea. I added a bit of peppermint leaves, spearmint leaves, orange peel, licorice root, and a cinnamon stick. The result is very refreshing, with just a little bit of a kick to help jump start one&#039;s scripting. I was reviewing my <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a> email this morning, and I ran across the following question:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! Is there good training content available for Windows PowerShell basics and an introduction to Windows PowerShell for SQL Server?<\/p>\n<p>&mdash;PB<\/p>\n<p>I engaged in a rather long conversation. This post is the result of that conversation.<\/p>\n<h3>Round one:<\/h3>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/A2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/A2.PNG\" border=\"0\" alt=\" \" \/><\/a>&nbsp;Hello PB,<\/p>\n<p>Sure! There are lots of great resources for learning Windows PowerShell and SQL Server scripting. Check out these <a href=\"\/b\/heyscriptingguy\/archive\/tags\/sql+server\/windows+powershell\/default.aspx\" target=\"_blank\">Hey, Scripting Guy! Blog posts<\/a>. I have more than three dozen posts there.<\/p>\n<h3>Round two:<\/h3>\n<p>Thanks Ed!<\/p>\n<p>I went through some of the links and they were very useful. But I am looking for some good training content for Windows PowerShell basics and an introduction to Windows PowerShell for SQL Server.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0285.A2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0285.A2.PNG\" border=\"0\" alt=\" \" \/><\/a>&nbsp;My <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/dd742419.aspx\" target=\"_blank\">Windows PowerShell Scripting<\/a> page in the Script Center has over a dozen hours of training for Windows PowerShell basics, but there is nothing specific to SQL Server and Windows PowerShell other than the information I gave you earlier.<\/p>\n<h3>Round three:<\/h3>\n<p>Hi Ed,<\/p>\n<p>I went through your links and learned a quite a bit. Thanks for the good references. I&rsquo;m running into an issue when reading data from a data table. Can you please help me find what I&rsquo;m doing wrong here:<\/p>\n<p style=\"margin-left:30px\">foreach ($Row in $SrvList.Rows)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;write-host (&quot;value is : &quot;, $Row[0][1])<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p><b><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/8154.A2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/8154.A2.PNG\" border=\"0\" alt=\" \" \/><\/a>&nbsp;$srvlist.rows<\/b> contains a collection of rows. <b>$row<\/b> is your enumerator, and you should be able to list <b>$row<\/b> instead of trying to index into the collection.&nbsp;<span style=\"font-size:12px\">Inside your loop, <\/span><b style=\"font-size:12px\">$row<\/b><span style=\"font-size:12px\"> will have members, you can use <\/span><b style=\"font-size:12px\">$row<\/b><span style=\"font-size:12px\"> | <\/span><b style=\"font-size:12px\">Get-Member<\/b><span style=\"font-size:12px\"> to find those members, which will probably include column headings. Access the items via the members instead of by index number.&nbsp;<\/span><\/p>\n<p><span style=\"font-size:12px\">You can also directly access rows by using <\/span><b style=\"font-size:12px\">$srvlist<\/b><span style=\"font-size:12px\">. Use <\/span><b style=\"font-size:12px\">$srvlist.rows[0]<\/b><span style=\"font-size:12px\"> for the first row, and pipe this to <\/span><b style=\"font-size:12px\">Get-Member<\/b><span style=\"font-size:12px\"> to see the members.&nbsp;<\/span><span style=\"font-size:12px\">Hope this helps.<\/span><\/p>\n<h3>Round four:<\/h3>\n<p>Hi Ed,<\/p>\n<p>I tried <b>$SrvList.Rows[0]&nbsp; | Get-Member<\/b>, and it is giving the following error message:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3617.1.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3617.1.PNG\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Here&rsquo;s the full script:<\/p>\n<p style=\"margin-left:30px\"><strong>Function GetServerList<\/strong><\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">$sqlConnection = new-object System.Data.SqlClient.SqlConnection &quot;Server=localhost;Database=SSISPROTOSERVERS;Integrated Security=sspi&quot;<\/p>\n<p style=\"margin-left:30px\">$sqlConnection.Open()<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">#Create a command object<\/p>\n<p style=\"margin-left:30px\">$sqlCommand = $sqlConnection.CreateCommand()<\/p>\n<p style=\"margin-left:30px\">$sqlCommand.CommandText = &quot;select * from ServerList&quot;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">$adapter = New-Object System.Data.SqlClient.SqlDataAdapter $sqlcommand<\/p>\n<p style=\"margin-left:30px\">$dataset = New-Object System.Data.DataSet<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">$adapter.Fill($dataSet) | out-null<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\"># Close the database connection<\/p>\n<p style=\"margin-left:30px\">$sqlConnection.Close()<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">$dataTable = new-object &quot;System.Data.DataTable&quot; &quot;ServerList&quot;<\/p>\n<p style=\"margin-left:30px\">$dataTable = $dataSet.Tables[0]<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">return $dataTable<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">$SrvList = GetServerList<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Write-Host(&quot;There are &quot;, $SrvList.Count, &quot; SQL Servers in the list&quot;)<\/p>\n<p style=\"margin-left:30px\">$SrvList | format-table -AutoSize<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">foreach ($Row in $SrvList.Rows)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;$SrvList.Rows[0]&nbsp; | Get-Member&nbsp;<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>What does <b>$dataTable<\/b> return? What happens when we pipe it to <b>Get-Member<\/b>? What does <b>$rows<\/b> return?<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5488.A2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5488.A2.PNG\" border=\"0\" alt=\" \" \/><\/a>&nbsp;You should be able to type <b>$dataTable<\/b>, and it will return your <b>DataTable<\/b> object. <b>$rows<\/b> should return the <b>Rows<\/b> object.&nbsp;<span style=\"font-size:12px\">If they don&#039;t, you are not making the connection and returning proper data. You need to backtrack all the way to <\/span><b style=\"font-size:12px\">$sqlconnection<\/b><span style=\"font-size:12px\">, which should return a connection object. Pipe it to <\/span><b style=\"font-size:12px\">Get-Member<\/b><span style=\"font-size:12px\"> also.<\/span><\/p>\n<p>Remember your scoping, so that <b>$sqlconnection<\/b> only exists inside the function. You need to add a line after you make the connection:<\/p>\n<p style=\"margin-left:30px\">$sqlconnection | Get-Member<\/p>\n<p>You do not need <b>Return<\/b> because Windows PowerShell always returns an object from a function, but <b>Return<\/b> makes the script easier to read, so it is no problem leaving it there.<\/p>\n<h3>Round five:<\/h3>\n<p>Hi Ed,<\/p>\n<p>These two statements return fine with the actual data:<\/p>\n<p style=\"margin-left:30px\">Write-Host(&quot;There are &quot;, $SrvList.Count, &quot; SQL Servers in the list&quot;)<\/p>\n<p style=\"margin-left:30px\">2<\/p>\n<p style=\"margin-left:30px\">$SrvList | format-table -AutoSize<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7041.2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7041.2.PNG\" alt=\"Image of command output\" width=\"450\" height=\"91\" title=\"Image of command output\" \/><\/a><\/p>\n<p>But I can&rsquo;t display the rows.<\/p>\n<p style=\"margin-left:30px\">foreach ($Row in $SrvList.Rows)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write-host (&quot;Rows : &quot;)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$Row<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5340.A2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5340.A2.PNG\" border=\"0\" alt=\" \" \/><\/a>&nbsp;Pipe <b>SrvList<\/b> to <b>Get-Member<\/b>. You will see at least two properties: <b>ServerName<\/b> and <b>InstanceName<\/b>. To access the name, you need to use <b>$row.ServerName<\/b>.<\/p>\n<h3>Round six:<\/h3>\n<p>Thanks a lot Ed! Yup. Using <b>$row.ServerName<\/b> worked!<\/p>\n<p>~PB<\/p>\n<p>That is all there is to using Windows PowerShell to query from a SQL Server database. Join me tomorrow when I will talk about more cool stuff.<\/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><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, helps a reader with a Windows PowerShell and SQL Server scripting issue. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a nice cup of Darjeeling tea. I added a bit of peppermint leaves, spearmint leaves, orange peel, licorice root, and a cinnamon stick. The result [&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":[3,45],"class_list":["post-926","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, helps a reader with a Windows PowerShell and SQL Server scripting issue. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a nice cup of Darjeeling tea. I added a bit of peppermint leaves, spearmint leaves, orange peel, licorice root, and a cinnamon stick. The result [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/926","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=926"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/926\/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=926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}