Solved

Powershell Script Return Integer for IF Statement

Posted on 2013-12-03
4
546 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

825 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