Get-Service multi-machines

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Matt Howard Matt Howard 3 months, 2 weeks ago.

  • Author
    Posts
  • #51416
    Profile photo of Rafal Niznik
    Rafal Niznik
    Participant

    Hi Guys

    I want to create simple script that will show specific service like that below

    param(
    [Parameter(Mandatory=$True)]
    [string[]]$ComputerName
    )
    get-service -ComputerName $ComputerName | where {$_.name -eq 'WinDefend'} | Format-Table Machinename, Name, Status -AutoSize

    My question is:
    How to add mandatory option for service name?
    Also when I import computer list and run this script on some workstations it is asking me for privilages
    How to skip that and mark that specific device needs privileges
    I will be very grateful for help

    Raf

  • #51482
    Profile photo of Matt Howard
    Matt Howard
    Participant

    You could set up a try catch loop to catch the non-privileged machines:

    try {
         # Get-Service ...
    } catch {
         # Write-Warning ...
    }
    

    Also, you could just add a 'ServiceName' parameter with the mandatory block like your ComputerName variable. You could also remove the where statement and just name the service in the Get-Service command:

    Get-Service -Name $ServiceName -ComputerName $ComputerName | Format-Table MachineName,Name,Status -AutoSize
    

You must be logged in to reply to this topic.