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  stewart haddrick 3 years, 5 months ago.

  • Author
    Posts
  • #15829

    stewart haddrick
    Participant

    $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

    Geir Endre Jenssen
    Participant

    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

    Peter Jurgens
    Participant

    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

    Martin Nielsen
    Participant

    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

    stewart haddrick
    Participant

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

    Cheers

You must be logged in to reply to this topic.