Solved

Powershell Script Return Integer for IF Statement

Posted on 2013-12-03
4
549 Views
Last Modified: 2013-12-04
I need to write a script that will return an exit code (preferrably a 1 or a 0) on the success or failure of a cmdlet.  Then, I need to write an IF statement that takes certain actions based on the success or failure of the first cmdlet.  For example, if the cmdlet completes successfully, I want to delete a certain file.  If it encounters an error, I want it to write an entry in a log.
0
Comment
Question by:marrj
[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
  • 2
4 Comments
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 167 total points
ID: 39693143
Use LastExitCode variable...
#your code...
#if success, $result=0
#else $result=1
exit $result

if ($lastexitcode -eq 0) {
#code to run for success
} else {
#code to run for failure
} 

Open in new window

0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 166 total points
ID: 39693398
Better try with $?
if ($?) {
#code to run for success
} else {
#code to run for failure
}

Open in new window

If I am not wrong, $LastExitCode returns exit code of native applications. If you use $?, then it returns True or False based on last command result. It works for both CMDlet and native applications..
0
 
LVL 11

Accepted Solution

by:
paultomasi earned 167 total points
ID: 39694562
This should do the trick nicely

@echo off

set logfile=logfile.txt
set filetodelete=filetodelete.txt

if "%~1" equ "/?" goto :help

if not "%~1" equ "" (
  set cmdlet=%*
) else (
  set /p cmdlet="Enter name of CMDlet to run: "
)

call %cmdlet% 2>nul
set errorcode=%errorlevel%

if %errorcode% equ 0 (
  del "%filetodelete%" 2>nul
) else (
  echo %date% %time% Error executing %cmdlet%>>"%logfile%"
)

exit /b %errorcode%

:help
echo.
echo Syntax: %0 command
echo Where 'command' is the CMDlet to execute.
echo.
echo If CMDlet runs successfully, file %filetodelete% is deleted
echo If CMDlet does not run successfully, log written to %logfile%
echo.

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 39696407
Thank you
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

740 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