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
try{}catch{}statement? The error message also mentions privileges, do you have the required privileges for this operation?