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
[Parameter(Mandatory = $True)]
[Parameter(Mandatory = $True)]
[String]$ErrorLog = 'c:\error.txt',
$dependentservices = Get-Service $servicename -DependentServices -ErrorAction stop
Foreach ($Service in $dependentservices)
if ($Action -eq "Start")
start-service $service
stop-service $service
$props = @{'ServiceName' = $;
'Status' = $service.Status;
'StartType' = $service.StartType}
$obj = New-Object -TypeName PSObject -Property $props
Write-output $obj
Write-Warning "Warning, an error has occured: Please enter a valid service name."
"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:


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.


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!