This topic contains 5 replies, has 3 voices, and was last updated by
January 15, 2015 at 5:48 am #21884
Hi everyone, I know this might be easy to you guys, but I m having a little trouble with my function script.
Here is the problem, I wrote a function to check to see if a service is running or not.
IF the service is running it going to log the time and date then exit.
else if, its going to query the event log and pull the latest event logs, then post them to a file, then restart the service.
Well my problem is this: I can either get the event logs and the service will not restart or if i change it around it will start the service and give me a error about pulling the event logs?
I m attaching a copy of my function, so please let me know where i m going wrong with my function
thanks again for your help
January 15, 2015 at 5:54 am #21886MemberPoints: 0Rank: Member
Your Get-EventLog and Start-Service commands are two separate things; you shouldn't be trying to put them into a single pipeline. Try this:
Get-EventLog -newest 5 -logname 'application' -Source $ServiceName | Out-File $Error Start-Service -name $ServiceName
January 15, 2015 at 2:56 pm #21901
I did try you idea and i got the same results. Well if I figure it out, i will let you know.
January 15, 2015 at 9:42 pm #21911ParticipantPoints: 60Rank: Member
Also to add to Dave's comment, note that $Error is a reserved variable, which is automatically populated with error details when an error event is raised, so you should try and use another variable name, such as $ErrorMessage.
You would normally receive an error message to let you know $Error is read-only, which makes me think that maybe your $ErrorActionPreference set to 'SilentlyContinue'? If so, it's a good idea to change to one of the other options available that will visually provide you with error information. Having it set to SilentlyContinue makes it more difficult to debug and can result in some deadly results (in a metaphoric sense of course!). Red text looks ugly but it can save your life (again, metaphorically!)
Although there's nothing wrong with it, I'd also suggest not using Invoke-Item $File, and instead use Out-File $File as Dave wrote. It's better to be more explicit in your coding to help ensure you can get the results you are looking.
January 17, 2015 at 9:27 pm #21952
I first what to start off saying, thanks for helping me in point my powershell function in the right direction, I wanted to let everyone know that I have figured out my issue.
Dave, you were correct in saying that I did not need a pipe for the cmdlet I was trying to run. Its my inexperience with powershell that i come to this forum looking for help.
With that said, please review the script I have attached to this posting, and let me know if there is a better way of writing it.
January 18, 2015 at 6:19 am #21957ParticipantPoints: 60Rank: Member
There's usually better of different ways to write code, but that's what refactoring is for.
First off all, nice to see you using the full cmdlet and parameter names 🙂
From a quick glance, mainly cosmetic comments to be honest
– Exit isn't required for a script to leave a function, and you are already handling the logic for code execution by the for..else combination. There's nothing wrong with what you've done, it's just not required per se.
– I'd steer away from using the ######## way of things. They're best left for use in comments with text, and you can handle visual separation of code just as easily with a single comment before (if required) and several extra blank lines after the end of that bit of code to split up.
– Unless you're going to use at a later date some of the advanced parameter options (use get-help to get more info on these), the use of Cmdletbinding is pretty academic in this example.
– Personally, I usually indent within each set of braces until they end one occurs, so would indent after the first brace.
The topic ‘Need help with my function’ is closed to new replies.