首页 > 解决方案 > 如何在powershel脚本中将链接的.net调用分成多行?

问题描述

很少有答案可以显示像这样的不同的多行技巧,但是如果可能的话,没有任何解释可以解释如何实现这种代码格式

set-content $file $fll.Replace("Long line 1", "Very Long line 1")
                      .Replace("Long line 2", "Very Long line 2")
                      .Replace("Long line 3", "Very Long line 3")
                      .Replace("Long line 4", "Very Long line 4")
                      .Replace("Long line 5", "Very Long line 5")

如果Replace以上内容链接在一行中 - 它可以工作

标签: powershellsyntax

解决方案


对于您发布的特定代码片段,不仅仅是打破跨行链式 .NET 调用的一般情况:

[1] 将点放在.右括号之后。(打破任何.NET 跨行调用的一般情况)

[2] 将第二个参数Set-Content括在括号中。

Set-Content $file (
    $fll.Replace("Long line 1", "Very Long line 1").
        Replace("Long line 2", "Very Long line 2").
        Replace("Long line 3", "Very Long line 3").
        Replace("Long line 4", "Very Long line 4").
        Replace("Long line 5", "Very Long line 5")
)

推荐阅读