Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing an array in Powershell

Tags:

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
}

This is the output I'm getting:

System.Object[]

What am I missing?

EDIT:

This is the whole function:

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"

I also tried with:

foreach ($i in $result) {
    $result[$i].ToString()
}
like image 652
Alexander Meise Avatar asked Sep 16 '16 14:09

Alexander Meise


People also ask

How do you access an array in PowerShell?

To access items in a multidimensional array, separate the indexes using a comma ( , ) within a single set of brackets ( [] ). The output shows that $c is a 1-dimensional array containing the items from $a and $b in row-major order.

What is @() in PowerShell?

What is @() in PowerShell Script? In PowerShell, the array subexpression operator “@()” is used to create an array. To do that, the array sub-expression operator takes the statements within the parentheses and produces the array of objects depending upon the statements specified in it.

What is $_ in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


1 Answers

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
}
like image 157
Ansgar Wiechers Avatar answered Sep 28 '22 10:09

Ansgar Wiechers