Hear, Hear for Here-Strings
Running commands in PowerShell that require a format that will not run natively in PowerShell could be a difficult task, or can it? PowerShell provides a way to store, for example, a JSON as a string, enter here-string. A here-string is a single or double quoted string in which the quotation marks are interpreted literally. An example would be invoking a Rest API that requires a JSON body. Lets take a look at an example and see how here-strings work.
Trying to store JSON in a variable will return the following error:
`$Body = { “apple”: [ “red”, “green” ], “grape”: [ “green”, “red” ], “blueberry”: “blue” } At line:3 char:12
"apple": [~
Unexpected token ‘:’ in expression or statement. At line:6 char:6
],~
Missing argument in parameter list. At line:10 char:6
],~
Missing argument in parameter list. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken `Oh man… What happened? PowerShell does not understand what is being done and throws an error for an unexpected token. Lets declare this as a here-string by using
@' at the start and end of the JSON variable.
$Body = @' { "apple": [ "red", "green" ], "grape": [ "green", "red" ], "blueberry": "blue" } '@ Great! No errors were thrown, but… Why?
Notice the
@' at the beginning and end, this tells PowerShell to create a here-string and store this string in a variable. Also, a rule to follow: the
@' must be on their own line at the start and end of the declaration or the here-string will not be declared.
# This will not work, PowerShell will not throw an error, but thinks you are still working to create something. $Body = @'{ "apple": [ "red", "green" ], "grape": [ "green", "red" ], "blueberry": "blue" } '@ We can also store variables in a here-string, but that requires double quotes after the
@ . The same rules apply as using single quoted here-strings.
$Red = 'red' $Green = 'green' $Blue = 'blue' $Body = @" { "apple": [ $Red, $Green ], "grape": [ $Green, $Red ], "blueberry": $Blue } "@ Now that we built our here-string, we can now invoke a Rest API and do something with it. This will help when a vendor supplies a JSON payload to be used in a Rest API, all that needs to be done is substitute your values in a here-string and invoke the Rest API. As always, practice makes perfect, try running examples in the console before running in a production environment. Here-strings will save you some lines of code and time when building your PowerShell scripts.
To learn about here-strings, visit Microsoft’s documentation on quoting rules.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6
Chris Liquori - Twitter: @pwshliquori
Related Articles
Media Sync: Organize Your Photos and Videos with PowerShell
Do you have photos and videos that you have taken over the years that are scattered all over the place? Do you want to have all your photos and videos organized? Do you want all your photos and videos to have a standardized naming scheme? If you answered YES to these questions, then this is the post for you. In this post, I will provide you with the PowerShell code and examples for how to use the Media Sync script. The Media Sync script utilizes the Shell.Application COM object to gather file metadata. Only files that have a picture or video metadata type will be processed. The script uses the date taken for pictures and the media created metadata fields to organize the photos and videos. If there is no date taken or media created available for a given file, the script will use the modify date instead. The script also ensures that you won’t have any duplicate files by checking the file hashes of the two files in question. If the script detects duplicate files, it will only keep one copy of the file. There are also tools included to help you cleanup unwanted files or folders, delete empty directories and find duplicate files. The script has a simple menu driven PowerShell GUI similar to what I did in a previous post . The Media Sync PowerShell script provides the following features:
NetNeighbor Watch: The PowerShell Alternative To Arpwatch
In this post, we are going to setup NetNeighbor Watch on a Raspberry Pi. NetNeighbor Watch can keep an eye on your network and send you an email when a new host is discovered. NetNeighbor Watch is done completely in PowerShell. The results are very similar to those of arpwatch. NetNeighbor Watch is for anyone that wants more visibility into the wireless or wired devices on their network. We will also setup a weekly email report with all of the known hosts on your network. In this post, I will walk you through the entire process of setting this up from scratch on a Raspberry Pi, lets get started!
Simple PowerShell GUI
Over the years, I have supported and created multiple types of GUIs. I finally decided a few years ago to create a very simple menu driven PowerShell GUI. I wanted something that was very powerful yet very simple to maintain. I really enjoy automating manual administrative tasks, so that is what drove this project in the first place. Before I created the menu driven PowerShell GUI, I had directories and directories of very specific scripts to do specific tasks. I decided to standardize and consolidate all of those scripts into one menu driven PowerShell GUI. By doing this, I took the guess work out of determining which PowerShell script to run for a given task. This has greatly helped my colleagues know exactly what to run and how.
Feel free to check it out for yourself at my site:
SpiderZebra.com
. While you’re there, you can take a look at a few of my other related posts:
