This topic contains 5 replies, has 3 voices, and was last updated by
December 31, 2014 at 12:17 pm #21536
Hello! I am very new to PS.
I was wondering if there is a way to import a file into the Get-Service command to save me typing all the computers I need to monitor. For example:
We have a few computers that use DeepFreeze as they are used by students. In the last few months we have been having issues with the program that involves the service dfsrv.exe so I would like to check on time to time the computers to see if the service has stopped running for any reason.
So would it be possible to import a file, whether if it's a .txt or .csv file?
December 31, 2014 at 6:20 pm #21539MemberPoints: 0Rank: Member
I suggest you get all your computers' names into a single variable. If you're using a text file, then use something like this: $Computers = Get-Content -Path 'C:\ComputerList.txt'. If you're using a CSV file you should use the Import-Csv cmdlet – something like this: Import-Csv -Path 'C:\ComputerList.csv'.
Once you have the computers' names in a variable you can use it as the value for the -ComputerName parameter of the Get-Service cmdlet. Notice the difference between the two examples. The second example assumes the your CSV file has a header called Name.
Get-Service -ComputerName $Computers -Name dfsrv.exe
Get-Service -ComputerName $Computers.Name -Name dfsrv.exe
January 1, 2015 at 8:16 am #21543KeymasterPoints: 3Rank: Member
Sure. For example, in a text file containing one computer name per line:
Get-Service -ComputerName (Get-Content names.txt)
In a CSV having a "MachineName" column:
Get-Service -ComputerName (Import-CSV names.csv | Select -Expand MachineName)
You run into two problems. The first is that, if an error occurs, there's no way to easily tell which machine had the error or to do anything about it. Second, the output of the above examples won't include the computer names.
You could do a very similar trick with Get-WmiObject and the Win32_Service class. The upside would be that the output could then contain the computer names (either the PSComputerName property or the __SERVER property).
You could also use the Win32_Process class to check for the running executable.
If you have a LOT of computers, any of these approaches is going to be a bit slow, as they'll check computers sequentially. With Get-WmiObject, you could use -AsJob to run the task as a background job, which will parallelize it a bit.
January 2, 2015 at 8:04 am #21557
Yeah I saw that it didn't give me the computer names. I have 43 machines with DeepFreeze on them but I only have a few that are more troublesome.
I will play with this and see if I can work it out 🙂 Thank you for your input
January 2, 2015 at 1:48 pm #21564
I get an error when I try the first one as a test.
Get-Service : Cannot open Service Control Manager on computer 'computer names' This operation might require other privileges.
I don't why it will work with a couple of names but not with the ones i have.
January 2, 2015 at 2:24 pm #21566KeymasterPoints: 3Rank: Member
So, the problem is exactly what the error says: The command can't connect to the SCM, which is what runs services. Either a port is blocked, or the account you're using doesn't have privileges (different from permissions) on the remote machine.
Get-WmiObject might work, although that requires connecting to a different service, and has its own permissions system.
The topic ‘Import ComputerName for Get-Service’ is closed to new replies.