We help IT Professionals succeed at work.

Powershell running within a DOS batch script to include error handling

rakkad
rakkad asked
on
169 Views
Last Modified: 2017-05-10
Hi

I have the following powershell script which is called within a DOS batch:-

@ECHO ON
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"

I need to include error conditions in the script, if the script fails and logs these entries to a log file or eventviewer please ?

Thanks
Comment
Watch Question

Lionel MMSmall Business IT Consultant
CERTIFIED EXPERT

Commented:

Author

Commented:
Are there any specific examples which illustrates the use of exit codes using the code that I have supplied ?

Thanks
CoralonSenior Citrix Engineer
CERTIFIED EXPERT

Commented:
I don't quite understand why you are nesting powershell executables, and we can't tell what that actual script that is supposed to be running is?  What are you actually trying to accomplish?

In the mean time, for your 2nd powershell.exe command string, you could add a start-transcript & end-transcript to catch everything?  
You can also add a -redirectstandarderror <filename> to the Start-Process that gets issued..

I think your nested powershell's would probably screw up your CMD error codes.. if you want the actual powershell errors, then there are far far better ways to catch & handle them..

Coralon

Author

Commented:
Hi

Thanks for getting back to me.

The script highlighted:-

@ECHO ON
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"

runs in a DOS batch environment so it will be run as a scheduled task

What I am trying to achieve is to try and report any errors if the script fails as you mentioned you can use catch statements? can you show me examples of using these commands please using the script I have mentioned ?

Thanks
Lionel MMSmall Business IT Consultant
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Senior Citrix Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CoralonSenior Citrix Engineer
CERTIFIED EXPERT

Commented:
No response from user; unknown if this will accomplish what he was trying to do, but we were on the right track to getting the answer..
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.