Issues with outputing

I'm trying to output to a text file if a user receives an error while typing in a service. Any help would be appreciated.
Function Set-DependentService { #command retrieves the dependent services for a service and allows user to take start/stop action on them
[CmdletBinding()]
param(
[Parameter(Mandatory = $True)]
[String]$ServiceName,
[Parameter(Mandatory = $True)]
[String]$Action,
[String]$ErrorLog = 'c:\error.txt',
[switch]$LogErrors
)
PROCESS{
try{
$dependentservices = Get-Service $servicename -DependentServices -ErrorAction stop
Foreach ($Service in $dependentservices)
{
if ($Action -eq "Start")
{
start-service $service
}
else
{
stop-service $service
}
}
$props = @{'ServiceName' = $service.name;
'Status' = $service.Status;
'StartType' = $service.StartType}
$obj = New-Object -TypeName PSObject -Property $props
Write-output $obj
}
catch{
Write-Warning "Warning, an error has occured: Please enter a valid service name."
if($LogErrors){
"Service Name: $servicename... Warning, an error has occured: Please enter a valid name." | out-file $ErrorLog -Append
}
}
}
}

What exactly is the error\issue? First thing that jumps out is you are writing to the root of C:, which standard users don’t have permissions to do. A better default path would be something like this:

[environment]::GetFolderPath('Desktop')

So if they put in a service that isn’t recognized it’ll warn them and log it into a file. I have everything work besides the log being sent to a file.

Thanks

There is code in the function to accomplish what you are asking. Are you getting an error? Are you adding the switch -LogErrors to the command?

I got it to work thanks!