I used to put a script fragment in a string, a multiline string called here-string, such as:
Copy Code code as follows:
$code =@ "
Invoke-webrequest
Get-service-name Adobearmservice
"@
Disadvantages have two:
If I am this $code string is very long, the execution error, I want to be wrong, but because it is a string, in the ISE can not be highlighted, inconvenient. So, I guess I'll have to take the script out of this string and put it in another Ise window to make a careful identification.
The string in the $code defines some variables, and I don't want these variables to be automatically parsed into values. Then I'm going to have to get out of the box. Single quotes, if the string contains just a large number of single quotes, the estimated inverted quotation marks (escape characters) are flooded.
Today it is not the fighter of the hen, but the Tchaikovsky in the hen: the Magic brace.
Copy Code code as follows:
{
Invoke-webrequest ' Http://www.jb51.net '
Get-service-name Adobearmservice
$name = ' Mosser Lee '
} | Out-file ' My.ps1 '
The above script can be highlighted in Ise, and after execution, the source script in curly braces is clean and authentic and is saved to the script file.
Article Source: http://www.pstips.net/output-source-scripts-to-file.html