Showing posts with label Interpolation. Show all posts
Showing posts with label Interpolation. Show all posts

Monday, July 28, 2008

Here Docs in PowerShell

Those of you that are familiar with Unix-style shell scripting or Perl may be familiar with the concept of a HERE doc.  This example from Perl may help explain the name:

my $multiline_string = <<HERE;
This
  is
    my
      string.
HERE


The idea is that once you start the Here Doc, everything you type is taken as part of your string until you tell it that it's time to stop, in this case by putting HERE on its own line.

In PowerShell you can do something similar, using @' and '@ or @" and "@.  Everything from the @' or @" to the '@ or "@ is considered part of the same string.

PS C:\Users\tojo2000\Documents> $multiline_string = @'
>> This
>>   is
>>     my
>>       System.String.
>> '@
>>
PS C:\Users\tojo2000\Documents\> $multiline_string
This
  is
    my
      System.String.

Note that the multiline strings use single and double quotes to control variable interpolation just like regular single and double quotes.  Any variables in a multiline double-quoted string will be expanded.

Why would you want to do this?  There are a lot of situations in which this can make your strings a lot more readable.  As an example, let's say you have a SQL query:

$sql = 'SELECT * FROM employees INNER JOIN parking ON parking.emp_id = employees.emp_id WHERE parking.size = "Compact"';

compare that to this:

$sql = @'
SELECT * FROM employees
INNER JOIN parking
  ON parking.emp_id = employees.emp_id
WHERE parking.size = "Compact"
'@


More Preference Variables

I covered $ErrorActionPreference in the last post, but there are a few other preference variables that you might want to be aware of.  The full list can be found here.


$ConfirmPreference  (default value = 'High')

$ConfirmPreference sets the default level for the -Confirm option on cmdlets.  When a developer makes a cmdlet they have the option of setting the impact level to 'Low', 'Medium', or 'High'.  When a command is run, if the impact level is greater than or equal to the level in $ConfirmPreference, a confirmation prompt appears before the action is executed.  The confirmation prompt will look something like this:

Are you sure you want to perform this action?
Performing operation "Stop-Process" on Target "calc (5852)".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): 

To suppress confirmation prompts altogether, you can set $ConfirmPreference to $false, which is especially useful when scripting High impact operations.


$OFS (default value = ' ')

$OFS is the Output Record Separator.  Let's say I make an array and then print it, like so: 

PS C:\Users\tojo2000\Documents> $array = ('T', 'O', 'J', 'O')
PS C:\Users\tojo2000\Documents> echo $array
T
O
J
O

Each element of the array was echoed.  Notice what happens if we embed our array in a double-quoted string, though.
 
PS C:\Users\tojo2000\Documents> "My name is $array."
My name is T O J O.

Because the default value of $OFS is a single space, each element of the array is printed with a single space between.  We can set $OFS to be whatever we want, however:

PS C:\Users\tojo2000\Documents> $OFS = ' to the '
PS C:\Users\tojo2000\Documents> "My name is $array."
My name is T to the O to the J to the O.


$DebugPreference (default value = 'SilentlyContinue')
$ProgressPreference (default value = 'Continue')
$VerbosePreference (default value = 'SilentlyContinue')
$WarningPreference (default value = 'Continue')

These preference variables tell PowerShell what to do if it comes across a Debug, Progress (when using the Write-Progress cmdlet, for example), Verbose, or Warning message.  

The possible options are:
  • Inquire - print the message, then prompt to continue
  • Continue - print the message, then continue
  • SilentlyContinue - suppress the message, then continue
  • Stop - error out of the script or cmdlet