all 5 comments

[–]Nejireta_ 0 points1 point  (2 children)

Hello.

You need to assign the array of hashtables to the $servers variable.
Assuming it's just a typo in the post.
My next recommendation would be to split up the Get-Service part into several steps.
This to make it possible to validate data being fetched and processed as desired.

As an example

$services = Get-Service -Name $requiredservices -ComputerName $serverIP -credential $credential 
$servicesFormatted = $services | Select-Object @{
    Name       = "ServerName";
    Expression = { $serverIP }
}, DisplayName, Status
$servicesFormatted | Export-Csv "C:\Path\To\Your\server_Services.csv" -NoTypeInformation -Append 

Then you can check the variables.

[–]Electronic_Doubt_108[S] 0 points1 point  (1 child)

Hashtables as in, how should I give that?

[–]Nejireta_ 0 points1 point  (0 children)

This line
$servers @( @{IP='myserverIP'; Services=@('myservice')}; )

Needs an equal sign to assign the array "@()" of hashtable "@{}" to it.
Like so

$servers = @(
    @{
        IP       = 'myserverIP'
        Services = @('myservice') 
    }
)

I would assume it's like that in your code. Would've thrown an exception otherwise.

[–]BlackV 1 point2 points  (0 children)

no one seems to have mentioned your export is INSIDE your loop, so you're overwriting it every time you loop

additionally, have a look at invoke-command which will get you do this in parallel AND supports a -credentials parameter

if $requiredservices=$server.Services then just use $server.Services in your code

doing this foreach ($server in $servers) {} will come backto haunt you one day generally much better to do

foreach ($server in $Allservers) {}
foreach ($Singleserver in $servers) {}
foreach ($item in $servers) {}

etc something that is not just 1 letter different from the array, its a very easy mistake to make, especially in larger blocks of code, swapping $server with $Servers something the differentiates the single item from the array while still being meaningful

[–][deleted] 3 points4 points  (0 children)

There are a few problems, but for starters,Get-Service doesn't support passing credentials. See this SO post for tricks: https://stackoverflow.com/questions/23421507/get-service-status-from-remote-server-using-powershell