Solved

Powershell Script Return Integer for IF Statement

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

759 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now