【发布时间】:2020-02-14 20:51:23
【问题描述】:
假设以下 Powershell 脚本...
function Process-Folder {
[cmdletbinding()]
param ([string]$Path)
Process {
$returnvalue = ""
# code here that reads all of the .txt files in $path
# and concatenates their contents to $returnvalue
return $returnvalue
}
}
我想在这个脚本中添加一些行,它会调用这个函数几次来处理多个文件夹。我会编写如下代码:
$allFileContent = ""
$firstFolder = Process-Folder -Path "c:\foo"
$allFileContent = $allFileContent + $firstFolder
$secondFolder = Process-Folder -Path "c:\bar"
$allFileContent = $allFileContent + $secondFolder
此代码有效,但看起来不优雅,看起来不像“Powershell 方式”。我试过了:
$filecontent = ""
$filecontent = $filecontent + Process-Folder -Path "C:\foo"
$filecontent = $filecontent + Process-Folder -Path "C:\bar"
但是 ISE 在表达式或语句中给了我“意外的令牌 'Process-Folder'。我也尝试过:
$filecontent = ""
$filecontent | Process-Folder -Path "C:\foo"
$filecontent | Process-Folder -Path "C:\bar"
返回...
The input object cannot be bound to any parameters for the command either because the
command does not take pipeline input or the input and its properties do not match any
of the parameters that take pipeline input.
如何以更优雅/“类似 Powershell”的方式完成第一个 sn-p 的工作?
【问题讨论】:
标签: powershell string-concatenation