Import ComputerName for Get-Service

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Don Jones Don Jones 1 year, 11 months ago.

  • Author
    Posts
  • #21536
    Profile photo of James
    James
    Participant

    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?

  • #21539
    Profile photo of tommymaynard
    tommymaynard
    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

  • #21543
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #21557
    Profile photo of James
    James
    Participant

    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

  • #21564
    Profile photo of James
    James
    Participant

    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.

  • #21566
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

You must be logged in to reply to this topic.