How do I list machine names and a service state in a loop please?

Welcome Forums General PowerShell Q&A How do I list machine names and a service state in a loop please?

This topic contains 4 replies, has 4 voices, and was last updated by

 
Participant
4 years, 4 months ago.

  • Author
    Posts
  • #15829

    Participant
    Points: 0
    Rank: Member

    $computers = get-content C:\Users\someguy\Documents\playmachines.txt

    ForEach ($computer in $computers)

    {

    Get-Service -name SCardSvr -ComputerName $computer | stop-service -PassThru

    Get-Service -name SCardSvr -ComputerName $computer | start-service -PassThru

    }
    The above code does what's supposed to but I'd like the output to list the machine name and the service state for each command.
    Thank you in advance.

  • #15832

    Participant
    Points: 0
    Rank: Member

    But by doing it like that, you are getting the services from the Remote computer and piping them to Restart-Service (or Start/Stop-Service) on Your computer, and restarting the service on Your LOCAL computer, not on the target computer.
    Restart-Service, Start-Service and Stop-Service only Works on the local maschine.
    It doesn't have a -ComputerName parameter (check the help file)
    I think you need to use WMI/CIM for this, or use:
    Invoke-Command -ScriptBlock {Restart-Service -Name BITS } -ComputerName SomeComputer
    to run the command on the Remote computer.

  • #15833

    Participant
    Points: 0
    Rank: Member

    I don't think so... What Martin has written will work on a remote machine. Remember that the object being sent through the pipeline represents a specific service on a remote computer because that's what you requested with Get-Service. Therefore when you pipe that object to Restart-Service it knows what to do.

    I can show another way this can be done, and in my opinion a little simpler, understandable and of course much less code.

    Get-Service -Name SCardSvr -ComputerName $(get-content C:\Users\someguy\Documents\playmachines.txt) | Restart-Service -PassThru | Select-Object Status,Name,DisplayName,MachineName
    
  • #15831

    Participant
    Points: 0
    Rank: Member

    This'll do what you want. It creates a custom object containing the details you want, and then writes the object to the pipeline:


    foreach($computer in $computers) {
    # Not sure when Restart-Service was added. May be you don't have it.
    $service = Get-Service -Name SCardSvr -ComputerName $computer | Restart-Service -PassThru
    Write-Output (New-Object PSObject -Property @{ 'Computer' = $computer; 'Status' = $service.Status })
    }

    I'd then wrap it in a function so you can capture the output:


    function Get-ServiceStatus([Parameter(Mandatory=$true)][string[]]$Computers, [Parameter(Mandatory=$true)][string]$ServiceName) {
    foreach($computer in $computers) {
    # Not sure when Restart-Service was added. May be you don't have it.
    $service = Get-Service -Name $ServiceName -ComputerName $computer | Restart-Service -PassThru
    Write-Output (New-Object PSObject -Property @{ 'Computer' = $computer; 'Status' = $service.Status })
    }
    }
    $result = Get-ServiceStatus -Computers 'Computer1','Computer2','Computer3' -ServiceName 'SCardSvr'

  • #15835

    Participant
    Points: 0
    Rank: Member

    Thanks to all that took the time to respond, im looking forward to testing the suggestions in the morning.

    Cheers

The topic ‘How do I list machine names and a service state in a loop please?’ is closed to new replies.