Powershell getting services, not quite working

Evening

$name = "WDSServer"
$servers = Get-content "C:\temp\CheckWDS\ServerList.txt"

#Check for csv file and delete.
$FileName = 'C:\Temp\CheckWDS\ServerServices.csv'
    if (Test-Path $FileName) 
{
     Remove-Item $FileName
}

function Confirm-WindowsServiceExists($name)
{   
   if (Get-Service -Name $name -Computername $servers -ErrorAction Continue)
   {
       Write-Host "$name Exists on $server"
       return $true
   }
       Write-Host "$name does not exist on $server"
       return $false
}

ForEach ($server in $servers)

 {Confirm-WindowsServiceExists $name | Export-csv 'C:\Temp\CheckWDS\ServerServices.csv' -APPEND -Force}

Open in new window



So, I want this to go pull the server list, it works when I don't try to export it to a CSV but errors with

Export-csv : Cannot process argument because the value of argument "name" is not valid. Change the value of the "name" argument and run the operation again.
At line:24 char:40
+ ... sts $name | Export-csv 'C:\Temp\CheckWDS\ServerServices.csv' -APPEND  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ExportCsvCommand
 

When I do.... Any help would be great.

Thanks
Alex
LVL 16
Alex Green3rd Line Server SupportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That code doesn't make much sense - it has many issues.
It looks like you want to go thru a list of servers, contained in a text file, and check for existence of a single service. However, your function uses the script variables $server and $servers - $server works as side effect, $servers however causes that you are asking for the service at all computers in each call.

You need to provide the server as another parameter with the call.

Another issue is that you do not export the server name. If the export would work, the result were only true/false lines.
0
oBdACommented:
Can't test it, but I think you want something like this:
$ServiceName = "WDSServer"
$Servers = Get-Content "C:\temp\CheckWDS\ServerList.txt"

#Check for csv file and delete.
$FileName = 'C:\Temp\CheckWDS\ServerServices.csv'
If (Test-Path $FileName) {
	Remove-Item $FileName
}

Function Test-WindowsServiceExists($ComputerName, $ServiceName) {
	$ReturnObject = '' | Select-Object -Property ComputerName, ServiceName, ServiceExists
	$ReturnObject.ComputerName = $ComputerName
	$ReturnObject.ServiceName = $ServiceName
	If (Get-Service -Name $ServiceName -Computername $ComputerName -ErrorAction Continue) {
		Write-Host "$($ServiceName) exists on $($ComputerName)"
		$ReturnObject.ServiceExists = $true
	} Else {
		Write-Host "$($ServiceName) does not exist on $($ComputerName)"
		$ReturnObject.ServiceExists = $false
	}
	$ReturnObject
}

$Servers | ForEach-Object {
	Test-WindowsServiceExists -ComputerName $_ -ServiceName $ServiceName
} | Export-Csv -NoTypeInformation -Path $FileName

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Very similar to what I had in mind and would use. In particular moving the Export-CSV out of the loop too.
This is no real improvement, just slightly modified, and with a "tricky" conditional console output ;-):
$ServiceName = "WDSServer"

# Check for csv file and delete.
$FileName = 'C:\Temp\CheckWDS\ServerServices.csv'
Remove-Item $FileName -ea SilentlyContinue

Function Test-WindowsServiceExists($ComputerName, $ServiceName) {
    $exists = Get-Service -Name $ServiceName -Computername $ComputerName -ErrorAction Continue

    write-host "Service $serviceName $('does not exist'*!$exists+'exists'*$exists) on $ComputerName"

    [PsCustomObject] @{
        ComputerName  = $ComputerName
        ServiceName   = $ServiceName
        ServiceExists = $exists
    }
}

Get-Content "C:\temp\CheckWDS\ServerList.txt" |
% {
    Test-WindowsServiceExists -ComputerName $_ -ServiceName $ServiceName
} | Export-Csv -NoTypeInformation -Path $FileName

Open in new window

0
What were the top attacks of Q1 2018?

The Threat Lab team analyzes data from WatchGuard’s Firebox Feed, internal and partner threat intelligence, and a research honeynet, to provide insightful analysis about the top threats on the Internet. Check out our Q1 2018 report for smart, practical security advice today!

Alex Green3rd Line Server SupportAuthor Commented:
Just used this one, it works flawlessly
0
Alex Green3rd Line Server SupportAuthor Commented:
So either way I wasn't actually that far off, saying my code doesn't make much sense but it gives me exactly what I want on the screen is, to be honest, quite harsh.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Sorry, the results you got on screen where nonsense! It might have been looking correct, but only on a first glance. Just compare the output of your code with ours (doesn't matter which one), running against a mix of servers with the service available and unavailable, and you'll see they have nothing in common.

Each of my points stated is true.
've said everything only for the purpose to show you where issues were, so you can do better next time. From posting just code you won't learn much.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.