在Powershell中打印阵列 [英] Printing an array in Powershell

查看:62
本文介绍了在Powershell中打印阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印一个数组(我同时尝试了for循环和直接使用.ToString()进行尝试),但始终获得了System.Object输出.

I am trying to print an array (I tried both with a for loop and directly with .ToString()), but I allways get a System.Object output.

数组的内容是此命令的结果:

The content of the array is the result of this command:

$singleOutput = Invoke-Command -ComputerName $server -ScriptBlock {
    Get-ChildItem C:\*.txt -Recurse |
        Select-String -Pattern "password" -AllMatches
}

这是我得到的输出:

System.Object[]

我想念什么?

这是整个功能:

foreach ($server in $servidores) {
    $result = @()
    Write-Output ("---Searching on Server:---" + $server + "----at:" +
        (Get-Date).ToString() + "----")
    $singleOutput = Invoke-Command -ComputerName $server -ScriptBlock {
        Get-ChildItem C:\*.txt -Recurse |
            Select-String -Pattern "password" -AllMatches
    }
    $result += $singleOutput

    Write-Host $result.ToString()
}
Read-Host -Prompt "Press Enter to exit"

我也尝试过:

foreach ($i in $result) {
    $result[$i].ToString()
}

推荐答案

您正在使用Select-String,它会生成MatchInfo对象.因为看起来您想要文件中的所有匹配行,所以您可能应该仅返回MatchInfo对象的Line属性的值.另外,您的数组处理也太复杂了.只需输出Invoke-Command返回的任何内容,并将循环输出捕获到变量中即可.对于循环内的状态输出,请使用Write-Host,这样消息就不会在$result中捕获.

You're using Select-String, which produces MatchInfo objects. Since it looks like you want the whole matching lines from the files you probably should return just the value of the Line property of the MatchInfo objects. Also, your array handling is way too complicated. Just output whatever Invoke-Command returns and capture the loop output in a variable. For status output inside the loop use Write-Host, so that the messages don't get captured in $result.

$result = foreach ($server in $servidores) {
    Write-Host ("--- Searching on Server: $server at: " + (Get-Date).ToString())
    Invoke-Command -ComputerName $server -ScriptBlock {
        Get-ChildItem C:\*.txt -Recurse |
            Select-String -Pattern "password" -AllMatches |
            Select-Object -Expand Line
    }
}

如果您还需要主机名,则可以使用计算出的属性添加主机名并返回自定义对象:

If you also need the hostname you could add it with a calculated property and return custom objects:

$result = foreach ($server in $servidores) {
    Write-Host ("--- Searching on Server: $server at: " + (Get-Date).ToString())
    Invoke-Command -ComputerName $server -ScriptBlock {
        Get-ChildItem C:\*.txt -Recurse |
            Select-String -Pattern "password" -AllMatches |
            Select-Object @{n='Server';e={$env:COMPUTERNAME}},Line
    }
}

您只需通过回显数组变量即可输出数组:

You output an array simply by echoing the array variable:

PS C:\> $result
Server    Line
------    ----
...       ...

要获取自定义格式的输出,您可以例如使用

To get custom-formatted output you can for instance use the format operator (-f):

$result | ForEach-Object {
  '{0}: {1}' -f $_.Server, $_.Line
}

这篇关于在Powershell中打印阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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