Showing posts with label Here-strings. Show all posts
Showing posts with label Here-strings. Show all posts

Thursday, June 25, 2015

Can you use a Here-Strings query to retrieve data using PowerShell?

Yes you can.
You need to understand a few items. Let me go over them.

What is meant by here-strings?
http://hodentekhelp.blogspot.com/2015/06/what-is-here-string-in-windows.html

Using a variable in a here-strings:
http://hodentekhelp.blogspot.com/2015/06/can-you-have-variable-in-powershell.html

How to query a SQL Server with SQLCMD?
http://hodentekmsss.blogspot.com/2015/06/querying-sql-server-database-with-sqlcmd.html

How do you query a database using PowerShell?
http://hodentekmsss.blogspot.com/2015/06/how-do-you-query-database-using.html
If you review the above articles then you can put together all the information in running a query as shown where the query to the database is in here-strings.

That is all folks!

Here-strings come in very handy when the query contains single quote, double quotes and other such ugly characters which a mere string representation cannot handle.

Sunday, June 7, 2015

Can you have a variable in PowerShell here-strings?

Yes.

Review this link before you begin.

You can have a variable in Here-strings but you need to define the variable in the same session. Here is how you do it:

Declare $hello as follows:

$Hello={@"
Hello $x
"@
$x="Jay"}


In order that the variable is expanded run the following:

&$Hello
(This is not case sensitive, you can as well run, &$hello)
and you will get the response:
Hello Jay


(hello or Hello: not case sensitive)
Variable declaration can be in the beginning also as shown:

PS C:\Windows\system32> $Hello={$x="Jay"
@"
Hello $x
"@
}
PS C:\Windows\system32> &$hello
Hello Jay

PS C:\Windows\system32>

Note that defining the variable in single quotes will not alter the response.