Output Help

Function Set-DependentService { #command retrieves the dependt services for a service and allows user to take start/stop action on them
[CmdletBinding()]
param(
[Parameter(Mandatory = $True)]
[String]$ServiceName,
[Parameter(Mandatory = $True)]
[ValidateSet("Start","Stop")]
[String]$Action
)

BEGIN {}

PROCESS{
$dependentservices = Get-Service $servicename -DependentServices
Foreach ($Service in $dependentservices) {
if ($Action -eq "Start"){
start-service $service
}else{
stop-service $service
} #end else
} #end foreach
} #end process

END {}

}

I’ve created this command and I need help in outputting by Service Name, Status, and start type of each dependent service.

It’s not very clear how you want to modify your code but here’s an example for getting the information you want:

function Get-DependentServiceInfo {

    param (
        [Parameter(Mandatory = $true)]
        [String]$serviceName
    )

    BEGIN {}
 
    PROCESS {
        
        Get-Service $serviceName | Select-Object -ExpandProperty DependentServices | ForEach-Object {

            [PSCustomObject] @{

                Name   =    $_.Name
                Status =    $_.Status
                StartType = $_.StartType

            }

        }

    }
     
    END {}
     
}

Get-DependentServiceInfo WinMgmt

 

Why create a function when the following cmdlets work to start/stop dependent services.

Get-Service -Name ServiceName -DependentServices | Start-Service