How do you take a command like the following in PowerShell and split it across multiple lines?
&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" -verb:sync -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" -dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"
Trailing backtick character, i.e.,
&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `
-verb:sync `
-source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" `
-dest:contentPath="c:\websites\xxx\wwwroot,computerName=192.168.1.1,username=administrator,password=xxx"
White space matters. The required format is Space`Enter.
Another method for cleaner argument passing would be splatting.
Define your parameters and values as a hashtable like this:
$params = @{ 'class' = 'Win32_BIOS';
'computername'='SERVER-R2';
'filter'='drivetype=3';
'credential'='Administrator' }
And then call your commandlet like this:
Get-WmiObject @params
Microsoft Docs: About Splatting
TechNet Magazine 2011: Windows PowerShell: Splatting
Looks like it works with Powershell 2.0 and up
$params.add('name','Bob Newhart')
ramblingcookiemonster.wordpress.com/2014/12/01/…
Ah, and if you have a very long string that you want to break up, say of HTML, you can do it by putting a @
on each side of the outer "
- like this:
$mystring = @"
Bob
went
to town
to buy
a fat
pig.
"@
You get exactly this:
Bob
went
to town
to buy
a fat
pig.
And if you are using Notepad++, it will even highlight correctly as a string block.
Now, if you wanted that string to contain double quotes, too, just add them in, like this:
$myvar = "Site"
$mystring = @"
<a href="http://somewhere.com/somelocation">
Bob's $myvar
</a>
"@
You would get exactly this:
<a href="http://somewhere.com/somelocation">
Bob's Site
</a>
However, if you use double-quotes in that @-string like that, Notepad++ doesn't realize that and will switch out the syntax colouring as if it were not quoted or quoted, depending on the case.
And what's better is this: anywhere you insert a $variable, it DOES get interpreted! (If you need the dollar sign in the text, you escape it with a tick mark like this: ``$not-a-variable`.)
NOTICE! If you don't put the final "@
at the very start of the line, it will fail. It took me an hour to figure out that I could not indent that in my code!
Here is MSDN on the subject: Using Windows PowerShell “Here-Strings”
You can use the backtick operator:
& "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `
-verb:sync `
-source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" `
-dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"
That's still a little too long for my taste, so I'd use some well-named variables:
$msdeployPath = "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe"
$verbArg = '-verb:sync'
$sourceArg = '-source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web"'
$destArg = '-dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"'
& $msdeployPath $verbArg $sourceArg $destArg
If you have a function:
$function:foo | % Invoke @(
'bar'
'directory'
$true
)
If you have a cmdlet:
[PSCustomObject] @{
Path = 'bar'
Type = 'directory'
Force = $true
} | New-Item
If you have an application:
{foo.exe @Args} | % Invoke @(
'bar'
'directory'
$true
)
Or
icm {foo.exe @Args} -Args @(
'bar'
'directory'
$true
)
Another way to break a string across multiple lines is to put an empty expression in the middle of the string, and break it across lines:
sample string:
"stackoverflow stackoverflow stackoverflow stackoverflow stackoverflow"
broken across lines:
"stackoverflow stackoverflow $(
)stackoverflow stack$(
)overflow stackoverflow"
In PowerShell 5 and PowerShell 5 ISE, it is also possible to use just Shift + Enter for multiline editing (instead of standard backticks `
at the end of each line):
PS> &"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" # Shift+Enter
>>> -verb:sync # Shift+Enter
>>> -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" # Shift+Enter
>>> -dest:contentPath="c:\websites\xxx\wwwroot,computerName=192.168.1.1,username=administrator,password=xxx"
Splat Method with Calculations
If you choose splat method, beware calculations that are made using other parameters. In practice, sometimes I have to set variables first then create the hash table. Also, the format doesn't require single quotes around the key value or the semi-colon (as mentioned above).
Example of a call to a function that creates an Excel spreadsheet
$title = "Cut-off File Processing on $start_date_long_str"
$title_row = 1
$header_row = 2
$data_row_start = 3
$data_row_end = $($data_row_start + $($file_info_array.Count) - 1)
# use parameter hash table to make code more readable
$params = @{
title = $title
title_row = $title_row
header_row = $header_row
data_row_start = $data_row_start
data_row_end = $data_row_end
}
$xl_wksht = Create-Excel-Spreadsheet @params
Note: The file array contains information that will affect how the spreadsheet is populated.
Success story sharing