Issues with outputing

Welcome Forums General PowerShell Q&A Issues with outputing

Viewing 4 reply threads
  • Author
    Posts
    • #277353
      Participant
      Topics: 5
      Replies: 6
      Points: 59
      Rank: Member
      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
                  }
              }
          }
      }
    • #277356
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      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:

    • #277362
      Participant
      Topics: 5
      Replies: 6
      Points: 59
      Rank: Member

      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

    • #277365
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      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?

    • #277368
      Participant
      Topics: 5
      Replies: 6
      Points: 59
      Rank: Member

      I got it to work thanks!

Viewing 4 reply threads
  • You must be logged in to reply to this topic.