problem listing services

This topic contains 4 replies, has 3 voices, and was last updated by  bytejunkie 4 years, 1 month ago.

  • Author
    Posts
  • #8654

    bytejunkie
    Participant

    test out the following code. For me, it misses out services that were installed by non system users. specifically the services im looking to list, which are those run by a service account, not a local or system account.

    Param(
    
    [string]$path = "c:\Scripts\admin-accounts.csv",
    
    [array]$servers = @("UKSERVER","UKSERVER2")
    
    )
    
     
    
    Function Get-Accounts
    
    { Param ([string[]]$servers)
    
    Foreach ($s in $servers)
    
    {
    
    Write-Output $s
    
    get-wmiobject win32_service | format-table name, startname, startmode
    
    } #end foreach $s
    
    } #end function Get-Uptime
    
     
    
    # Entry Point ***
    
    Get-Accounts -servers $servers |format-list
    

    so my problem is that it doesn't list the services im actually bothered about. anyone see an issue with the code?

  • #8662

    Poshoholic
    Participant

    You're missing -ComputerName $s in your call to Get-WmiObject. Right now all it is doing is enumerating the Win32_Service objects on your local computer, once for each of the strings in the $servers parameter.

  • #8665

    bytejunkie
    Participant

    good spot, thanks.

    got it working just by adding that.

  • #8671

    Matt Tilford
    Participant

    Don't worry though, it's an easy mistake to make. I've seen Don make the same mistake in one of his training videos. Also you have exposed your stea re-purposing of code because you forgot to change #end function Get-Uptime.

    For best practice i believe the parameter servers should be called computername to align with other cmdlets.

  • #8672

    bytejunkie
    Participant

    haha,yeah, i am the copy and paste king.

    actually i'm just converting to posh from vbs, so copying and adjusting is the norm for me right now.

You must be logged in to reply to this topic.