Skip to content

SQL Padawan

"The force is strong with this one"

  • About

Tag: service account

Powershell function to get extended service account info

I created this function to get more information for any service account, for example if you need to know the login use in SQL Server services.

Check the script below and also can download here

<#
.SYNOPSIS
Get-ServiceAccountInfo retrieves account info from windows services

.DESCRIPTION
Get-ServiceAccountInfo gets the data from services accounts

.PARAMETER ComputerName
Server name that you want to get information

.PARAMETER Filter
Specify a value name to filter by service name

.PARAMETER Credential
Provide the user name and password to retrive the services information if not logged with admin account.

.EXAMPLE
Get-ServiceAccountInfo -ComputerName 'server_name' -Filter 'SQL'
#> 
function Get-ServiceAccountInfo {
    [cmdletbinding()] 
    param (
        [Parameter(Mandatory = $true
            , ValueFromPipelineByPropertyName = $true
            , Position = 0)]
        [string[]]$ComputerName,

        [Parameter(Mandatory = $false, 
            Position = 1)]
        [string]$Filter,

        [Parameter(Mandatory = $false, 
            Position = 2)]        
        [PSCredential]$Credential
    )            
    begin {
        $params = @{
            Class           = 'Win32_Service';
            ComputerName    = '';
        }

        if($Credential){
            $params.Credential = $Credential
        }

        if($Filter){
            $Filter = "*" + $Filter.TrimStart('*').TrimEnd('*') + "*"
        }

        $outArray = @()
    }
    process{
        foreach($Computer in $ComputerName) {
            $params.ComputerName = $Computer

            try{
                if($Filter) {
                    $data = Get-WmiObject @params | Where-Object { $_.StartName -like $Filter } | Select-Object DisplayName, StartName, State, Name, StartMode, Status, Description
                } else {
                    $data = Get-WmiObject @params | Select-Object DisplayName, StartName, State, Name, StartMode, Status, Description
                }
            
            $outObj = New-Object PSObject 
            Add-Member -InputObject $outObj -MemberType NoteProperty -Name ComputerName -Value $Computer
            Add-Member -InputObject $outObj -MemberType NoteProperty -Name ServiceName -Value $data.Name
            Add-Member -InputObject $outObj -MemberType NoteProperty -Name DisplayName -Value $data.DisplayName
            Add-Member -InputObject $outObj -MemberType NoteProperty -Name StartName -Value $data.StartName
            Add-Member -InputObject $outObj -MemberType NoteProperty -Name State -Value $data.State
            Add-Member -InputObject $outObj -MemberType NoteProperty -Name StartMode -Value $data.StartMode
            Add-Member -InputObject $outObj -MemberType NoteProperty -Name Status -Value $data.Status
            Add-Member -InputObject $outObj -MemberType NoteProperty -Name Description -Value $data.Description

            $outArray += $outObj
            } catch {
                Write-Message -Level Error -Message $_.Exception.Message 
            }
        }
    } end {
        Write-Output $outArray
    }

}

Author sqlpadawanPosted on 2019-03-06Categories powershell, script, sql server, toolsTags powershell, script, service account, tool, windowsLeave a comment on Powershell function to get extended service account info
  • LinkedIn
  • Twitter
  • Skype
  • Email
  • GitHub
  • YouTube
  • Facebook
My Tweets
May 2022
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031  
« Nov    

Translate

azure b-tree backup best practices checkpoint configuration database DMV How to index job linux locks migration monitoring powershell replication script sets SQL Operations Studio sql server statistics storage tempdb tools transaction log tsql undocumented wait
  • About
SQL Padawan Create a free website or blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • SQL Padawan
    • Join 148 other followers
    • Already have a WordPress.com account? Log in now.
    • SQL Padawan
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar