如何从另一个 powershell 脚本逐字编写/创建 powershell 脚本? [英] How can I write/create a powershell script verbatim, from another powershell script?

查看:53
本文介绍了如何从另一个 powershell 脚本逐字编写/创建 powershell 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码(在底部)在文件中产生以下输出之一

The following code (at the bottom) produces one of the following outputs in the file

4/12/2019 = (get-date).AddDays(2).ToShortDateString();
4/13/2019 = (get-date).AddDays(2 + 1).ToShortDateString();

或者如果我还没有初始化变量

or if I haven't initialized the variable

 = (get-date).AddDays(2).ToShortDateString();
 = (get-date).AddDays(2 + 1).ToShortDateString();

这是代码块,我希望父 ps1 文件逐字写入子 ps1 文件.

This is the code block, I would like the parent ps1 file to write the child ps1 file verbatim.

$multiLineScript2 = @"

    $startDate2 = (get-date).AddDays($resultOfSubtraction).ToShortDateString();
    $endDate2 = (get-date).AddDays($resultOfSubtraction + 1).ToShortDateString();
"@

$multiLineScript2 | Out-File "c:\file2.ps1";

推荐答案

tl;dr:

要创建一个verbatim多行字符串(即具有文字内容的字符串),请使用a 单引号 here-string:

To create a verbatim multi-line string (i.e., a string with literal contents), use a single-quoted here-string:

$multiLineScript2 = @'

    $startDate2 = (get-date).AddDays($resultOfSubtraction).ToShortDateString();
    $endDate2 = (get-date).AddDays($resultOfSubtraction + 1).ToShortDateString();
'@

注意使用 @''@ 作为分隔符.

Note the use of @' and '@ as the delimiters.

仅在需要字符串扩展(插值)时使用双引号此处的字符串选择性抑制扩展,转义 $ 字符.以`$的形式逐字包含,如您的自己的答案.

Use a double-quoted here-string only if string expansion (interpolation) is needed; to selectively suppress expansion, escape $ chars. to be included verbatim as `$, as shown in your own answer.

获取帮助 about_quotingrules 讨论了 PowerShell 支持的字符串文字类型:

  • 要获得具有文字内容的字符串(无插值,C#将称之为逐字字符串),请使用单引号:'...'

  • To get a string with literal content (no interpolation, what C# would call a verbatim string), use single quotes: '...'

  • 嵌入 ' 字符.在 '...' 字符串中,double ('');所有其他字符.可以按原样使用.
  • To embed ' chars. inside a '...' string, double them (''); all other chars. can be used as-is.

获得一个可扩展字符串(字符串插值),即一个字符串,其中变量引用(例如,$var${var}) 和表达式(例如,$($var.Name))可以嵌入并替换为其值,使用双引号: "..."

To get an expandable string (string interpolation), i.e., a string in which variable references (e.g., $var or ${var}) and expressions (e.g., $($var.Name)) can be embedded that are replaced with their values, use double quotes: "..."

  • 要选择性地抑制扩展,反引号转义 $ 字符.;例如,为了防止 $var"..." 字符串中被插入(扩展到它的值),使用 `$var;要嵌入文字反引号,请使用 ``
  • 有关字符串扩展规则的概述,请参阅此答案.
  • To selectively suppress expansion, backtick-escape $ chars.; e.g., to prevent $var from being interpolated (expanded to its value) inside a "..." string, use `$var; to embed a literal backtick, use ``
  • For an overview of the rules of string expansion, see this answer.

这两种基本类型也可用作 here-strings - 形式为 @'<newline>...<newline>'@@"..."@ 分别( 代表实际的换行符(换行符)) - 使定义多行字符串更容易.

Both fundamental types are also available as here-strings - in the forms @'<newline>...<newline>'@ and @"<newline>...<newline>"@ respectively (<newline> stands for an actual newline (line break)) - which make defining multi-line strings easier.

  • 重要:
    • 开始分隔符 - @'@" - 在同一行 - 字符串的内容必须在以下行中定义.
    • 结束分隔符 - '@"@(匹配开始分隔符) - 必须在一开始一行.
    • 此处定义的文件字符串总是使用其封闭文件的换行符格式(CRLF vs. LF),而交互式定义的字符串总是只使用 LF.
    • Important:
      • Nothing (except whitespace) must follow the opening delimiter - @' or @" - on the same line - the string's content must be defined on the following lines.
      • The closing delimiter - '@ or "@ (matching the opening delimiter) - must be at the very start of a line.
      • Here-strings defined in files invariably use the newline format of their enclosing file (CRLF vs. LF), whereas interactively defined ones always use LF only.

      示例:

      # Single-quoted: literal:
      PS> 'I am $HOME'
      I am $HOME
      
      # Double-quoted: expandable
      PS> "I am $HOME"
      I am C:\Users\jdoe
      
      # Here-strings:
      
      # Literal
      PS> @'
      I am
        $HOME
      '@
      I am
        $HOME
      
      # Expandable
      PS> @"
      I am
        $HOME
      "@
      I am
        C:\Users\jdoe
      

      这篇关于如何从另一个 powershell 脚本逐字编写/创建 powershell 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆