0

I've been tasked with auditing Windows servers to see what services and programs are installed. I though PowerShell would be ideal for this, though I am new to PowerShell scripting.

I have a list of servers in a txt file and have been using the following commands.

Get-CimInstance -ComputerName (Get-Content .\servers.txt) -ClassName win32_product | Select-Object PSComputerName, Name, PackageName, InstallDate, IdentifyingNumber | Export-CSV -Path .\programs.csv

Get-Service -ComputerName (Get-Content .\servers.txt) -ErrorAction Continue | Select-Object MachineName, Name, DisplayName, Status, StartType | Export-CSV .\services.csv

The .\servers.txt is a simple text file with the server FQDNs.

If the server is inaccessible or not found, the first statement prints an error message and continues. As I understand the ErrorAction by default is to continue. However, the second statement fails and stops executing. I expect there will be errors and I could deal with them on a case by case, but why does the statement not continue on error as expected?

If I remove the bad server from the list, Get-Services runs fine. I'd rather get an error in the script and deal with that separately.

Update to include Powershell output

Get-Service : Cannot open Service Control Manager on computer 'computer_name'. This operation might require other privileges. At .\get-service.ps1:1 char:1 + Get-Service -ComputerName (Get-Content .\servers.txt) -ErrorAction Co ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-Service], InvalidOperationException + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetServiceCommand

6
  • What is your $ErrorActionPreference variable set to? "Continue"? Commented Sep 21, 2020 at 14:19
  • @LukasRäpple, yes, $ErrorActionPreference is Continue and I'm also being explicit about that in the Get-Services command. Commented Sep 21, 2020 at 14:24
  • I think what could be a problem is if the first command parses a wrong server, because of the continue statement the stdout will still be passed to Select-Object. But Select-Object is not able to access those Elements so it throws an error. You could try adding -ErrorAction to the second and third command as well. Also try removing this explicit -ErrorAction "Continue" completely one time. Commented Sep 21, 2020 at 14:36
  • @LukasRäpple Unfortunately I get the same error when adding the -ErrorAction parameter or if it is removed entirely. Commented Sep 21, 2020 at 14:47
  • Did you try a try{}catch{} statement? The error message also mentions privileges, do you have the required privileges for this operation? Commented Sep 21, 2020 at 15:25

1 Answer 1

2

Just to show you how I imagined using a try-catch-statement.

$serverList = Get-Content .\servers.txt foreach ($server in $serverLists) { try { Get-CimInstance -ComputerName $server -ClassName win32_product | Select-Object PSComputerName, Name, PackageName, InstallDate, IdentifyingNumber | Export-CSV -Path .\programs.csv -Append Get-Service -ComputerName $server | Select-Object MachineName, Name, DisplayName, Status, StartType | Export-CSV .\services.csv -Append } catch { "Error on server $server occurred." | Out-File -FilePath .\errors.log -Append } } 

You could tweak the catch blocks to distinguish between CimInstance and the Service or instead of the generic error message you can forward the $_.Exception.Message.

2
  • Thanks @Alex_P. This is useful but it doesn't help me understand why the one cmdlet fails but the other works as I had written them before. Had I tested them the other way around, maybe I'd assume I'd coded it wrong in the first instance and never realised it would work. Alas, I'll revert to this method. Perhaps there is a bug in Get-Service? Commented Sep 23, 2020 at 15:32
  • @sbmobile, I would not assume a bug in Get-Service. Do you know which service throws the error? Is it always the same service on the same server? Commented Sep 23, 2020 at 19:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.