# help to output array

**URL:** <https://forums.powershell.org/t/help-to-output-array/1857>\
**Category:** PowerShell Help\
**Created:** [September 5, 2013, 6:18am UTC](https://forums.powershell.org/t/help-to-output-array/1857 "2013-09-05T06:18:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![guindasoft](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/guindasoft/32/941_2.png) [@guindasoft](https://forums.powershell.org/u/guindasoft)\
**Post date:** [September 5, 2013, 6:18am UTC](https://forums.powershell.org/t/help-to-output-array/1857/1 "2013-09-05T06:18:29Z")

</div>

first of all, sorry for my english…

hello all, thanks for all your help in advance, im trying to list speceifc services in specific servers.  
this is part of a procedure where i try to check service status before shutdown these 3 servers.-

* * *

## script

function Get-ServicesStatus{  
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact=‘Medium’)]  
param(  
$array = @(  
(“ComputerName”,“ServiceName”),  
(“server01”,“service01”),  
(“server01”,“service02”),  
(“server01”,“service03”),  
(“server01”,“service04”),  
(“server02”,“service05”),  
(“server02”,“service06”),  
(“server02”,“service07”),  
(“server03”,“service08”)  
)  
)  
Begin{}  
Process  
{  
$out = @() #tried with or without it  
for ($i = 1; $i -lt 9; $i++)  
{ $out=Get-Service -ComputerName $array[$i][0] -Name $array[$i][1] |Format-Table -Property MachineName,Status,DisplayName -AutoSize  
}  
Write-Output “------------op1”  
$out  
Write-Output “------------op2”  
$out[0]  
$out[1]  
$out[2]  
$out[3]  
$out[4]  
$out[5]  
$out[6]  
$out[7]  
$out[8]  
Write-Output “------------op3”  
foreach ($element in $out) {$element}  
}  
End{}  
}

* * *

## output

------------op1  
MachineName Status DisplayName

* * *

server03 Running service08

------------op2  
MachineName Status DisplayName

* * *

server03 Running service08

------------op3  
MachineName Status DisplayName

* * *

server03 Running service08

* * *

## i wish

MachineName Status DisplayName

* * *

server01 status service01   
server01 status service02   
server01 status service03   
server01 status service04   
server02 status service05   
server02 status service06   
server02 status service07   
server03 status service08

could be possible???

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [September 5, 2013, 7:02am UTC](https://forums.powershell.org/t/help-to-output-array/1857/2 "2013-09-05T07:02:35Z")

</div>

There are a number of problems with your script including:

- the array structure you’ve used doesn’t work
- you’ve used format-table and then tried to access the results which is what is giving you the odd looking results
- you’re making multiple trips to the server

You could put the server & service information into a CSV file but that makes dealing with the services a bit more difficult.  
I’d start by trying something like this  
`
$servers = “server02”, “exch10”
$server02_services = “BITS”, “NtFrs”, “MSMQ”, “Kdc”
$exch10_services = “MSExchangeAB”, “W32Time”, “W3SVC”`

`
`

`foreach ($server in $servers){
Get-Service -ComputerName $server -Name (Get-Variable -Name ($server + “_services”)).value |
select @{N=“Server”; E={$server}}, Status, Name, DisplayName
}
`  
Create a list of servers  
Create a list of services for each server  
loop through the servers using foreach  
use get-service

- 
  - the -computername comes for the foreach variable

- 
  - use the server name & add \_services to get the variable containing the list of services  
use select to create a calculated field for the computername

hope this helps

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [September 5, 2013, 8:21am UTC](https://forums.powershell.org/t/help-to-output-array/1857/3 "2013-09-05T08:21:53Z")

</div>

Another option is to create a custom PSObject:

```
$data = @()
 
$data += New-Object â€“TypeName PSObject -Property `
        (@{'ComputerName'="server10"; 
            'Name'= @("Spooler", "Server", "Workstation");})

$data += New-Object â€“TypeName PSObject -Property `
        (@{'ComputerName'="exch10"; 
            'Name'= @("BITS", "NtFrs", "MSMQ", "Kdc");})

$data | Get-Service | Select MachineName, Name, DisplayName, Status
```

Additionally, if you would like to keep the data source in another file you can export the above to an XML file (CSV will not hold the object [array] type):

```
$data | Export-CliXML C:\Temp\test.xml
```

and then you can do something like this:

```
$xmlData = Import-Clixml C:\Temp\test.xml
$xmlData | Get-Service | Select MachineName, Name, DisplayName, Status
```

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [September 5, 2013, 6:26pm UTC](https://forums.powershell.org/t/help-to-output-array/1857/4 "2013-09-05T18:26:18Z")

</div>

I like that approach - its very neat

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [September 5, 2013, 6:40pm UTC](https://forums.powershell.org/t/help-to-output-array/1857/5 "2013-09-05T18:40:21Z")

</div>

Another option using hash tables instead of PSObjects

`
$data = @{
"server02" = "BITS", "NtFrs", "MSMQ", "Kdc";
"exch10" = "MSExchangeAB", "W32Time", "W3SVC"
}
`

`foreach ($server in $data.Keys){
Get-Service -ComputerName $server -Name ($data[$server]) |
select @{N=“Server”; E={$server}}, Status, Name, DisplayName
}
`

Greatest thing about PowerShell is that you get options

---

<div class="post-metadata">

**Author:** ![guindasoft](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/guindasoft/32/941_2.png) [@guindasoft](https://forums.powershell.org/u/guindasoft)\
**Post date:** [September 6, 2013, 4:03pm UTC](https://forums.powershell.org/t/help-to-output-array/1857/6 "2013-09-06T16:03:58Z")

</div>

@Richard Siddaway, thank you, im actually working on your idea. i have modified my script and tried using -whatif and seems to be ready for QAS.  
YES RICHARD, SEEMS TO BE SEVERAL WAYS TO SOLVE SAME ISSUE… SO I THINK EVERYTHING IS DECIDED BY PERFORMANCE AND OPTIONS

@Rob Simmers, thank you too, i will try yours later… seems to be a little advance for me at this time…

could you guys share some reference site/stuff/link whatever about working with powershell+sql & powershell+oracle, i need simply check that some databases are open and ready before start app service.

THANK YOU FOR ALL YOUR HELP.

IT WAS VERY EDUCATED.

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [September 6, 2013, 7:45pm UTC](https://forums.powershell.org/t/help-to-output-array/1857/7 "2013-09-06T19:45:09Z")

</div>

PowerShell & SQL Server isn’t a topic that’s been covered very well so far. I haven’t seen a book that I would be prepared to recommend. There is a chapter on accessing data from SQL Server in PowerShell in Practice ([PowerShell in Practice](http://www.manning.com/siddaway)) & we cover working with databases in PowerShell in Depth ([PowerShell in Depth](http://www.manning.com/jones2/)). I’ve not seen much at all on PowerShell & Oracle.

before tackling either of the above books I’d recommend you work through Learning PowerShell in a Month of Lunches ([Learn Windows PowerShell in a Month of Lunches, Second Edition](http://www.manning.com/jones3/))

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [September 8, 2013, 11:32pm UTC](https://forums.powershell.org/t/help-to-output-array/1857/8 "2013-09-08T23:32:26Z")

</div>

Jep,

The example code I provided leverages how the Get-Service cmdlet was built. If you run _Get-Help Get-Service -Full_ and look at the parameters you’re using. Both ComputerName and Name accept string arrays (i.e. string) and Accept pipeline input. So, if you wanted to check the SAME services on 3…4…5… servers, you could do:

```
Get-Service -ComputerName "Server01" "Server02", "Server03" -Name "service1", "service2", "service3"
```

This would go through each server and get the status of each service on that server. The Accept pipeline input allows you to build a PSObject with the ComputerName and Name properties and the cmdlet will absorb and loop through each string array. So, by passing the parameters over the pipeline the cmdlet knows how to process the passed data. When you start building functions, you will get a better understanding how exactly the cmdlet is built and leverage the pipeline.

As far as checking SQLconnections, if you are just checking to see if you can connect, you should be able to something like:

```
$conn=new-object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerInstance,$Database,$ConnectionTimeout
$conn.ConnectionString=$ConnectionString
Try {
    #Attempt to open a connection to the database
    $conn.Open()
    $conn.Close()
    (Get-Service -Name "blah").Start
}
Catch {
    "Could not connect to SQL server"
}
```

Oracle is more convoluted, you typically need to be connecting to Oracle with a particular driver for the version of the database. This typically means you need to install a Oracle client on the system you are connecting from and reference the appropriate ODBC driver to connect. Other than that, you could use a similar method using System.Data.OracleClient

---

<div class="post-metadata">

**Author:** ![guindasoft](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/guindasoft/32/941_2.png) [@guindasoft](https://forums.powershell.org/u/guindasoft)\
**Post date:** [September 9, 2013, 7:41am UTC](https://forums.powershell.org/t/help-to-output-array/1857/9 "2013-09-09T07:41:35Z")

</div>

[quote=10008]Jep,  
The example code I provided leverages how the Get-Service cmdlet was built. If you run _Get-Help Get-Service -Full_ and look at the parameters youâ€™re using. Both ComputerName and Name accept string arrays (i.e. string) and Accept pipeline input. So, if you wanted to check the SAME services on 3…4…5… servers, you could do:

```
Get-Service -ComputerName "Server01" "Server02", "Server03" -Name "service1", "service2", "service3"
```
This would go through each server and get the status of each service on that server. The Accept pipeline input allows you to build a PSObject with the ComputerName and Name properties and the cmdlet will absorb and loop through each string array. So, by passing the parameters over the pipeline the cmdlet knows how to process the passed data. When you start building functions, you will get a better understanding how exactly the cmdlet is built and leverage the pipeline.

[/quote]  
@Rob Simmers,THANK YOU!  
i’ve tried it x 3 (one for each server) but as you can see in script i need to stop/start differents services en each server so it doesn’t work for me. Actually im ussing Richard ideas and it is working fine for me.

now i will start working on you advice about sql. THANK YOU.

@Richard Siddaway, THANK YOU, i will get recommended books. I thik i will start with " Learning PowerShell in a Month" in order to be guided.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:57pm UTC](https://forums.powershell.org/t/help-to-output-array/1857/10 "2024-05-16T20:57:03Z")

</div>

Closed on May 16, 2024, 8:57pm UTC


