[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to run a batch file in power shell and tell if it generates error

Posted on 2013-11-26
14
Medium Priority
?
503 Views
Last Modified: 2013-11-26
If I execute a batch file from Power Shell, how can I tell if it errors?
0
Comment
Question by:Delta7428
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 9

Expert Comment

by:tsaico
ID: 39677877
Not exactly through powershell, but I always put an output to a TXT command for all the results in my batches for this reason.
0
 

Author Comment

by:Delta7428
ID: 39678048
Can I capture the results from the screen when the powershell script runs to a text file?
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39678054
yes, with
| Out-file "log.txt"
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
LVL 41

Accepted Solution

by:
footech earned 1200 total points
ID: 39678119
When you run an external command within PowerShell, you can get the result code it returned by looking at the automatic variable $LASTEXITCODE.  Typically programs return a 0 if there was no error.
0
 

Author Comment

by:Delta7428
ID: 39678165
Dan, can I specify the log file in the script? When I specify it at the command line, I get an empty log file.  But need to be able to specify it in the script.

footech, This is what I really need.  Can I get $lastexitcode to write to a text file from the script?


script.ps1
-------------------
c:\test\batch.bat
//write last exit code to text file
0
 
LVL 41

Expert Comment

by:footech
ID: 39678221
It's just a variable.  You can write it's contents to a file just like any other string (e.g. piping it to Add-Content, Out-File, or other commands).
0
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 800 total points
ID: 39678234
You can do script.ps1 | Out-file "log.txt" and if your bat produces output you'll find it in "log.txt".

It's really up to you, the programmer, to treat the errors and send messages.
You can start by removing the traditional @echo off.
0
 

Author Comment

by:Delta7428
ID: 39678283
Thanks.  I got what I need for the beginnings of my first PS script:

C:\Test\test.bat
$myfile = "c:\Test\log.txt"
Get-Date | Out-File $myfile -append
$lastexitcode | Out-File $myfile -append

Thanks!   Points coming!
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39678300
Just as a curiosity: if you're learning powershell, why are you learning how to keep using bat files? :)

I would think a better learning exercise would be to transform your bat into a true powershell script.

my 2¢
0
 

Author Comment

by:Delta7428
ID: 39678319
I agree.  The call to the batch file will be replaced by calling an exe file.  This is just a test script to get me started coding.
Thanks again
0
 
LVL 41

Expert Comment

by:footech
ID: 39678824
BTW, it's great to see someone new to PS scripting asking how to do something vs. just "please write a script that does this", so kudos to you.
I think you'll find quite a few people in the PowerShell TA here that are willing to explain things when you have questions.
0
 

Author Comment

by:Delta7428
ID: 39679018
Thanks footech!  I'm looking forward to my journey through PowerShell.  I love my experts here.
0
 
LVL 9

Expert Comment

by:tsaico
ID: 39679443
Second on that!  I troll the forum all the time reading other people's questions and the responses...
0
 

Author Comment

by:Delta7428
ID: 39679626
:)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
This video teaches viewers how to encrypt an external drive that requires a password to read and edit the drive. All tasks are done in Disk Utility. Plug in the external drive you wish to encrypt: Make sure all previous data on the drive has been …
This Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question