在PowerShell中,我想遍历其中包含多个项目的对象,但是我只想查看两个项目,即路径和名称,并对它们进行一些逻辑处理。问题是,如果我使用foreach
或ForEach-Object
,当我不希望它们打印时,它会立即打印出$line.Name
和$line.Path
。
尝试foreach
和ForEach-Object
,尝试void
和| Out-Null
,尝试$element.Name.ToString()
。
void
和Out-Null
几乎擦除了整个内容,因此我看不到任何值,而且我仍然可以看到void
或Out-Null
处的空白行线在。
ToString()
仍会打印该值。
foreach ($line in $buildPipelineObject) {
# $line.* has leading and trailing spaces, want to trim them
$trimPath = $line.Path # don't print!
$trimPath.Trim()
$trimName = $line.Name.ToString() # don't print!
#[void]Set-Variable -Name "trimName" -Value $line.Name # makes trimName null
$trimName.Trim()
# if pipeline isn't in a subfolder, then I don't need to insert backslash
if ($trimPath -eq "\") {
$pipelineName = $trimPath + $trimName
} else {
$pipelineName = $trimPath + "\" + $trimName
}
Write-Host "pipeline: $pipelineName"
}
问题是,我运行此代码块,然后看到类似
的内容\ pipelinename1 pipeline: \pipelinename1 \myfolder pl2 pipeline: \myfolder\pl2
我只想看到输出的“ pipeline:”行,例如
pipeline: \pipelinename1 pipeline: \myfolder\pl2