Struggling to output services and computer name to output file

Hi Guys

Newbie here to powershell! I have a script which looks through servers and services listed in an array and stops and starts depending on input parameter’s. What i’d like to do is list out the service name, status and servername (variable $Servername) into an output folder. Below is the work line I have so far which lists the service name, status and startup type…now how do I add the server name? I’m guessing it real simple but my skills are not up to scratch yet nore can I find anything on the net.

Thanks

(Get-Service -ComputerName $server -Name $ServiceName | Select Name, Status, StartType) | Out-File -Append -FilePath “c:\Temp\output1.txt”

Hello Dave,

I would suggest using Invoke-Command for running commands against multiple computers. You could then throw the Computernames in a text file and make the process smoother as well. You will need to make sure PSRemoting is enabled in your environment. Invoke-Command by default will provide you with the PSComputername Property providing the server name you would like to see.

Invoke-command -Computername (Get-Content -Path C:\ServerList.Txt) -ScriptBlock {Get-Service | Select-Object Name,Status,StartType} | Out-File C:\Temp\OutputFile.Txt

1 Like

Another vote for Invoke-Command, considering that in PowerShell 7, the ComputerName parameter does not exist on Get-Service. So if you go to PS7 your script will not work as written.

1 Like

You might try something like this:

Invoke-Command -ComputerName (Get-Content -Path C:\ServerList.Txt) -ScriptBlock { 
    $list = Get-Service
    foreach ($item in $list) {
        $result = [pscustomobject]@{
            Name = $item.Name
            Status = $item.Status
            StartType = $item.StartType
            ComputerName = $env:COMPUTERNAME
        }
        Write-Output $result
    }
} | Export-Csv C:\Temp\OutputFile.csv

2 Likes

It’s not a big thing but you can actually omit the variable assignment for the [PSCustomObject]

Invoke-Command -ComputerName (Get-Content -Path C:\ServerList.Txt) -ScriptBlock {
    $list = Get-Service
    foreach ($item in $list) {
        [PSCustomObject]@{
            Name         = $item.Name
            Status       = $item.Status
            StartType    = $item.StartType
            ComputerName = $env:COMPUTERNAME
        }
    }
} | 
    Export-Csv -Path 'C:\Temp\OutputFile.csv' -NoTypeInformation
1 Like

Thanks guys - so does the Invoke-command like a look and just runs through all the suggested computer names?

Furthermore to this topic how to run this against specific services? I’m only interested in stopping MSSQLSERVER’ for example and then checking and putting it status into a text file.

how to run this against specific services? I’m only interested in stopping MSSQLSERVER’ for example and then checking and putting it status into a text file.

Your first stop if you have some questions should always be the help for the cmdlets you’re about to use. You should always read the help completely including the examples - even if you don’t understand everything yet.

All the examples you’ve got here from the answers do export the results to a TXT or CSV file. Did you try the code snippets?

yes it work but it enters every service into a text file - I am only interested in returning one service titled “MSSQLSERVER”. From what I read online I need to pass the service parameter into Invoke-Command using -ArgumentList but Im struggling to get this to work at the moment

I manage this so far but need to put ‘MSSQLSERVER’ in a parameter and pass it through…

Invoke-command -Computername $servers -ScriptBlock { get-service -Name ‘MSSQLSERVER’ | Select-Object Name,Status,StartType}| Out-File C:\Temp\OutputFile5.Txt

You would need to add the service name to the name parameter pertaining to Get-Service

Get-Service -Name MSSQLSERVER

I really like this approach. Thank you for the good insight as always.

Cool. :+1:t4: I’m proud of you. :slightly_smiling_face:

Don’t you think a CSV file would be better? You can even easily read it again with PowerShell and use the informations contained within.

AAnd BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink: