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
}
}
}
}