1

Help me with some problem. I am new in PS and i need output data to table view. Looks like this.

name1  name2 
-----  ----- 
value1 value2

But i have:

$a=(get-service).name
$b=(get-service).Status
foreach ($name in $a)
{

$data = [pscustomobject]@{name1 = $name}
}
$data

RESULT

name1        
-----        
XboxNetApiSvc

WITHOUT FOREATCH

$a=(get-service).name
$b=(get-service).Status


$data = [pscustomobject]@{name1 = $a ; name2 = $b }

$data

Result

name1                                                                               name2                                  
-----                                                                               -----                                  
{2GISUpdateService, AcronisActiveProtectionService, AcrSch2Svc, AdobeARMservice...} {Stopped, Running, Running, Running...}

All of that i need for this script

$list = ((Get-ADComputer   -SearchBase "OU=PC,DC=cbu,DC=lan" -Filter  {Enabled -eq "True" }  -Properties DNSHostName).DNSHostName) 
$encoding = [System.Text.Encoding]::UTF8
$up = @()
$down = @()
$table= @()
foreach ($pc in $list) 
{
if ((Test-Connection -Count 1  -computer $pc -quiet) -eq $True)
 {
  $up += $pc
  #write-host $pc  "is up"

 }
 else
 {
 $down += $pc
 #Write-Host $pc "down"
 }
 } 

After all i need output values of $up and $down in 2 columns

3 Answers 3

3

You probably have a custom commandlet but you can run something similar to:

(Get-Service) | select Name,Status | Format-Table

UPDATE

After reading your update. At the end of your script you have two arrays $up and $down. I will declare it the static way to make an example easier

$up = @('pc1', 'pc2')
$down = @('pc3','pc4', 'pc5')

Because arrays can be diffrent length you need to calculate maximum length with:

$max = ($up, $down | Measure-Object -Maximum -Property Count).Maximum

And than create an object which "merges" above arrays with:

0..$max | Select-Object @{n="Up";e={$up[$_]}}, @{n="Down";e={$down[$_]}}

The output is:

Up  Down
--  ----
pc1 pc3
pc2 pc4
    pc5
Sign up to request clarification or add additional context in comments.

1 Comment

@MikeNuzhdin after reading your update I wrote a solution for you
0

If you are using Get-Service as example only then, you can just use Select :

Get-Service | Select Name, Status

Else

$MyList | Select Name1, Name2

Moreover, if you have a complex command and you want to extract a table of PSObject :

Get-Process | Select-Object -Property ProcessName, Id, WS

Read more about Select-Object : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-4.0

Comments

0

Yes Piotr Stapp is rights. You can use Format-Table

Example 1:

Get-Process | Sort-Object starttime | Format-Table -View starttime

Example 2:

(Get-Service) | select Name,Status | Format-Table

Example 3:

Get-Process | FT

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.