powershell exit code implentation - exit

Hi I am not a script expert and im looking for help:
I would like to have a exit code, im not sure how this works do...
(Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System).EnableLUA
if value = 0 the exit code should be
Write-Host "Success Message" Exit 0
if the value is 1 the exit code should be
Write-Host "Error Message" Exit 1001
but i have no clue how to implent this, could someone please help me, once i see the script working i can have an idea how this exactly works.
This cause were having MAX Remote,
here is some info:
https://dashboard.systemmonitor.co.uk/dashboard/helpcontents/index.html?script_guide.htm
if we can implent this correctly the dashboard would see the correct exit code.
Thank You
$enablelua = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System).EnableLUA
if ($enablelua -eq $0){
Write-Host "UAC Is Enabled"
Exit 0
}
else {
Write-Host "UAC Is Disabled"
Exit 1001
}
doesnt work

$enablelua = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System).EnableLUA
if ($enablelua -eq 0){
Write-Host "UAC Is Disabled"
Exit 1010
}
else {
Write-Host "UAC Is Enabled"
Exit 0
}
this works ;) just sharing

Related

If statement in build using ymal aws

Hey I'm trying to do an if statement in Yaml, something like
if $NUMBER_OF_SOURCES == 3 then echo 1
(echo 1 will change I the future to a script that does something in aws )
what's the correct syntax?
is it even possible?
Hey I'm trying to do an if statement in Yaml,
something like this:
if
$NUMBER_OF_SOURCES == 3 then echo 1
(echo 1 will change I the future to a script that does something in aws ),
what's the correct syntax? is it even possible?
tried to do something like that but i get
- if [ $NUMBER_OF_SOURCES -eq 3 ]
- then
- echo "true"
- fi
[Container] 2022/04/24 10:35:36 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: if [ $NUMBER_OF_SOURCES -eq 3 ]. Reason: exit status 2
You do this using pipe (|) notation in yaml:
- |
if [ $NUMBER_OF_SOURCES -eq 3 ]
then
echo "true"
fi
or just put everything in one line:
- if [ $NUMBER_OF_SOURCES -eq 3 ]; then echo "true"; fi

Why [ ... ] syntax doesn't work in a Makefile while `test` does?

I am on Linux Mint 19. I am entirely new to Makefiles.
Here is the problematic part:
[ $(shell id --user) -eq 0 ] && ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
which throws this error:
[ 1000 -eq 0 ] && ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
Makefile:25: recipe for target 'distrib' failed
make: *** [distrib] Error 1
On the contrary, using test command directly proves to be working entirely:
if test $(shell id --user) -eq 0; then ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 ) fi
I want to ask why that is, did I break some Makefile rule?
This doesn't have anything to do with makefiles, it has to do with shell scripting and the difference between using && vs. if in terms of the exit code. You are comparing apples and oranges here.
It's not related to test vs [. If you write the version using [ inside an if statement you'll get the same behavior as you do with test, and if you write the test version with the && model you'll get the same behavior as you do with [.
Run this in your shell:
[ 1000 -eq 0 ] && echo hi
echo $?
Now run this in your shell:
if [ 1000 -eq 0 ]; then echo hi; fi
echo $?
You'll see the former gives a non-0 exit code, while the latter gives a 0 (success) exit code. That's how if works; it "swallows" the exit code of the condition.
Make always looks at the exit code of the shell script to decide if it failed or not.
Generally in make scripting you want to re-arrange your expressions to use || rather than &&. That ensures that if the script exits early it exits with a success code not a failure code. You can write your script like this:
[ $$(id -u) -ne 0 ] || ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
Note I use $$(id -u) not $(shell id --user); the recipe is run in the shell already and it's an anti-pattern to use the make shell function in a recipe. Also, the -u option is a POSIX standard option while --user is only available in the GNU utilities version of id.

How to make non-interactive Maple exit on assertion failure?

My t.ms file contains:
interface(prettyprint=0): kernelopts(assertlevel=1):
ASSERT(1<1):
ASSERT(2<2):
When I run:
maple -q t.ms; echo $?
I get:
Error, assertion failed
Error, assertion failed
0
When I run:
maple -e 2 -q t.ms; echo $?
I get:
Error, assertion failed
0
I want to get:
Error, assertion failed
4
That is, I want Maple to exit with a nonzero exit status upon the first failing assertion. (I don't care if the exit code is 1 or anything else as long as it's nonzero. I've got the number 4 from the documentation, related to errorbreak) How do I get that?
The documentation doesn't make it very clear that one must use,
`quit`(n)
with name-quotes.
interface(prettyprint=0):
handler:=proc(e::uneval)
local failed;
printf("entered\n"); # remove this when satisfied
failed:=false;
try
if evalb(eval(e)) <> true then
error;
end if;;
catch:
failed:=true;
printf("Error, assertion failed\n");
finally;
if failed then
`quit`(5);
end if;
end try;
true;
end proc:
ASSERT( handler( 1<1 )):
ASSERT( handler( 2<2 )):
Now, saving this as file uh.mpl then using Maple 18.01 for Linux I see,
$ maple18.01 -q -A 2 ~/uh.mpl ; echo $?
entered
Error, assertion failed
5
And if run without the -A 2 then it doesn't run the asserted checks.
[edited] Here below is a slight modification, to process additional arguments as part of the printing.
handler:=proc(e::uneval)
local failed;
printf("entered\n"); # remove this when satisfied
failed:=false;
try
if evalb(eval(e)) <> true then
error;
end if;;
catch:
failed:=true;
printf("Error, assertion failed, %q\n", _rest);
finally;
if failed then
`quit`(5);
end if;
end try;
true;
end proc:

what is wrong with this conditional (if-else) execution in batch script?

I am testing some small scripts that I have. This particular one is driving me crazy. In my mind it should work, but it is not doing what I thought it would/should.
#echo off
echo testing the issue
echo.
ECHO Checking the log file for errors...
echo %errorlevel%
FINDSTR /C:"open failed" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else (GOTO TSTErrChecking1)
:TSTErrChecking1
FINDSTR /C:"does not exist" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else (GOTO TSTErrChecking2)
:TSTErrChecking2
FINDSTR /C:"Logon failed" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else ( GOTO TSTErrChecking3)
:TSTErrChecking3
FINDSTR /C:"Failure" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else ( GOTO TSTErrChecking4)
:TSTErrChecking4
FINDSTR /C:"RC (return code) = 0" C:\automatic_deployment\test1.txt
echo %errorlevel%
IF %ERRORLEVEL% == 0 (
ECHO Deployment was successful.
pause
GOTO quit) ELSE (
ECHO Deployment failed.
pause
GOTO quit)
:quit
exit
As you can see I am using "FINDSTR" to find certain words/strings from a small test1.txt file and act accordingly. I want it to say "deployment failed" and quit asap if it sees any "fail"/ "does not exist"/"open failed". If it cannot find anyone of those above and finds the "RC (return code) = 0" I want it to say it was "successful".
So, i put all those test words and strings in my test1.txt file and tested the batch script, but it keeps skipping (i think) all those fail hints/words and keeps saying it was successful. Plz, help. Thanks in advance.
it could be that you already have an %ERRORLEVEL% variable defined in your environment
Read
Raymond Chen's ERRORLEVEL is not %ERRORLEVEL% http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx
and then try
FINDSTR /C:"RC (return code) = 0" test.txt
IF ERRORLEVEL 1 ECHO Deployment failed.
I just didn't have the matching strings to search for. I didn't know that FINDSTR looked for EXACTLY the same case letters when searching and comparing. That was the problem.

how to detect when compiler emits an error

To compile a C++ project, I want to write a perl script to compile my program and see if the compilation went wrong or not. If the compiler gives any compilation error, I'll need to perform some other task.
The perl script will be something like this:
#l1 = `find . -name '*.c'`;
#l2 = `find . -name '*.cpp'`;
#l3 = `find . -name '*.cc'`;
my $err;
my $FLAGS = "-DNDEBUG"
push(#l , #l1, #l2, #l3);
chomp(#l);
foreach (#l) {
print "processing file $_ ...";
$err = `g++ $_ $FLAGS`;
if($err == something) {
#do the needful
}
}
so what should be something?
You should check $? instead, after g++....
perlvar
$?
The status returned by the last pipe close, backtick (`` ) command,
successful call to wait() or waitpid(), or from the system() operator.
The exit value of the subprocess is really ($?>> 8)
So you should check if g++ returned 0 (success) or non-zero.
if ($? >> 8) {
/* Error? */
}
IPC::System::Simple/IPC::Run3 make this easier