PowerShell foreach循环打印过多:我希望它仅打印我要打印的内容

时间:2019-10-01 22:20:52

标签: powershell

在PowerShell中,我想遍历其中包含多个项目的对象,但是我只想查看两个项目,即路径和名称,并对它们进行一些逻辑处理。问题是,如果我使用foreachForEach-Object,当我不希望它们打印时,它会立即打印出$line.Name$line.Path

尝试foreachForEach-Object,尝试void| Out-Null,尝试$element.Name.ToString()

voidOut-Null几乎擦除了整个内容,因此我看不到任何值,而且我仍然可以看到voidOut-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

0 个答案:

没有答案