Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell Script Return Integer for IF Statement

Posted on 2013-12-03
4
Medium Priority
?
562 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 668 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 664 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 668 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to dynamically set the form action using jQuery.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

715 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