how to suppress exit 0?

I have two powershell scripts.

script1 is a script that is supposed to be a wrapper and passes parameters to script2. script2 generates data based on those passed parameters.

script1 has currently a for loop the iterates a command like this:

foreach()
{   
    cmd /c "openPS.bat script2.ps1 $($someParam[-1])"
}

everytime that command runs successfully, it outputs exit 0

exit0

as you can see, in one of the runs, there was an error in script2 in which i specified to output exit 1 + $error[0] whenever there is an error/exception. but nowhere in script2 did i specify exit 0. there is only for exit 1 (error).

i read suggestions to use | out-null after the command, but the problem is the error from script2 no longer gets outputted/reported.

can i say something like cmd /c "openPS.bat script2.ps1 $($someParam[-1]) | out-null only exit 0?

HHmmm … sounds very confusing for me … why do you use cmd in your Powershell? Powershell is - just like cmd - a CLI. So you are running CLI #1 to use CLI #2 to start a new session of CLI #1. In the end you have 3 CLIs running where you probably only need one.
That might be the perfect time to refactor your scripts and take the chance to ged rid of some convoluted inefficient code from the past. :wink: :smiley:

[quote quote=140794]HHmmm … sounds very confusing for me … why do you use cmd in your Powershell? Powershell is – just like cmd – a CLI. So you are running CLI #1 to use CLI #2 to start a new session of CLI #1. In the end you have 3 CLIs running where you probably only need one.

That might be the perfect time to refactor your scripts and take the chance to ged rid of some convoluted inefficient code from the past. :wink: :grinning:

[/quote]
Well this is what I found online when I looked for how to launch other scripts from a powershell script. Also the reason for multiple clis is because we are using autosys and I have execution policy set in the batch file since the server is very restricted. I know it’s weird but I use openPS.bat twice to open the two scripts due to execution policy

You might start with learning the very of Powershell from scratch. That would save you from a lot of frustration, missunderstandings and wasted time. It’s not possible to learn a slightly complex technology goolgling and coüpying code snippets.
To run another Powershell script from inside a Powershell script you simply call the file name … preferably including the complete path. There’s no need to invoke CMD in between.
I don’t know what autosys is but I’d imagine that you can specify a commandline to invoke external commands or executables. This way you’d use the same syntax to invoke Powershell from inside autosys like you do from inside CMD. Just append the needed command line arguments.

[quote quote=140815]You might start with learning the very of Powershell from scratch. That would save you from a lot of frustration, missunderstandings and wasted time. It’s not possible to learn a slightly complex technology goolgling and coüpying code snippets.

To run another Powershell script from inside a Powershell script you simply call the file name … preferably including the complete path. There’s no need to invoke CMD in between.

I don’t know what autosys is but I’d imagine that you can specify a commandline to invoke external commands or executables. This way you’d use the same syntax to invoke Powershell from inside autosys like you do from inside CMD. Just append the needed command line arguments.

[/quote]
I’ve been automating with powershell for a year now. Anyways, you can’t run batch files without invoking CMD. According to this answer, this is one of the ways (and safest) to do it. As you can see, I am calling a bat file first, which is why i am invoking CMD.

https://stackoverflow.com/a/20671602/8397835

That’s what I’m talking about. You don’t have to run batch files anymore at all. You wouldn’t carry an old small bag with you inside of a bigger and nicer bag with wheels and an integrated powerbank and GPS and … anymore just because you still have something useful in the old small bag. Or would you? :wink: :smiley:

[quote quote=140842]That’s what I’m talking about. You don’t have to run batch files anymore at all. You wouldn’t carry an old small bag with you inside of a bigger and nicer bag with wheels and an integrated powerbank and GPS and … anymore just because you still have something useful in the old small bag. Or would you? 😉 😀

[/quote]
I would definitely not want to have to use batch, but I was forced to due to server security limits :frowning:

I have to run execution policy from batch file to allow powershell to run, otherwise, we were getting restricted exceptions

[quote quote=140845]
I would definitely not want to have to use batch, but I was forced to due to server security limits 🙁[/quote]
Sorry but that does not make any sense at all. If you depend on colleagues from other departements you should urgently talk to them. If you’re the guy in charge you should educate yourself and change this process.

How do you run these batch files?

this is what the batch file has:

Powershell.exe -ExecutionPolicy ByPass -File %psFile% %2 %3 %4 %5

[quote quote=140862]
this is what the batch file has:[/quote]
That was not my question. How do you run these batch files ???

I think Olaf’s point is you already know how to call powershell in a restricted environment.
Why would you use a batch file, just to execute something you already know how to do.

Olaf’s questions come to the point of “how” are you executing the batch file. If you’re using a scheduled task, you can just call the script exactly the same way you call it in the batch file.

so, if you go back to the process used to start the batch file, you should be able to call powershell with executionpolicy bypassed in the exact same way you are doing in the batch file.

I test it on a CMD console. Once it’s good we create a job on autosys and copy this exact same command we have on CMD.

In other words

OpenPS.bat script1.ps1 scrip2.ps2

[quote quote=140886]I think Olaf’s point is you already know how to call powershell in a restricted environment.

Why would you use a batch file, just to execute something you already know how to do.

Olaf’s questions come to the point of “how” are you executing the batch file. If you’re using a scheduled task, you can just call the script exactly the same way you call it in the batch file.

so, if you go back to the process used to start the batch file, you should be able to call powershell with executionpolicy bypassed in the exact same way you are doing in the batch file.

[/quote]
Well I have tried. In fact our entire team spent a couple days trying to understand why are we unable to execute powershell scripts on the server. Once I discovered I have to include execution policy, it worked.

 

Thts why I am using the batch file. How else am i supposed to allow execution policy? Oh yes, I did try to configure powershell to bypass the policy but that didnt work. The only way was to keep executing it everytime theiugh batch file , hence why we need the batch file.

Excuse me, cataster16, I don’t mean to offend you, but this will be my last reply on this thread. I think you’re lacking the fundamental understandings of Powershell and therefor you don’t understand what we are trying to tell you.
The Powershell execution policy is not a security feature. That’s why I even would recommend for you to disable it by GPOs on the servers where it’ll interfere with you.
I whish you a lot of success and good luck.

[quote quote=140929]Excuse me, cataster16, I don’t mean to offend you, but this will be my last reply on this thread. I think you’re lacking the fundamental understandings of Powershell and therefor you don’t understand what we are trying to tell you.

The Powershell execution policy is not a security feature. That’s why I even would recommend for you to disable it by GPOs on the servers where it’ll interfere with you.

I whish you a lot of success and good luck.

[/quote]
I mean I have automated an entire business Intelligence project with powershell since 2018, how could I he lacking the fundamentals? Lol

Thanks for your input

What is the execution policy on the server you are trying to execute your PowerShell commands/script?

How are you executing these scripts, from a local server / remote server / local workstation?

Like Olaf and David, we are trying to understand the limitation or why you can’t execute commands / error. You shouldn’t have to use batch files anymore, a PowerShell script or multiple scripts pending on your procedure should be enough.

It’s a tricky state If we don’t know “what we don’t know” . There are very few instances where somebody get solution or proper help Within first few replies in a forum. All participants are here for some reason, few will be seeking help, few will help them. During the course there will be lot of questions and cross questioning. Nobody knows everything. So please bare with the team.

So, how does autosys executes a script ? does it support PowerShell by default ? . It will be good if you can give more details on how you are using autosys where the script will finally go in.

[quote quote=140937]What is the execution policy on the server you are trying to execute your PowerShell commands/script?

How are you executing these scripts, from a local server / remote server / local workstation?

Like Olaf and David, we are trying to understand the limitation or why you can’t execute commands / error. You shouldn’t have to use batch files anymore, a PowerShell script or multiple scripts pending on your procedure should be enough.

[/quote]
I am scripting on a remote server. I am using remoteNG/RDP to connect to this server.

It’s a windows 10 server

Its internet access is blocked by a proxy. After a whole 2 weeks of research, I was finally able to successfully bypass the proxy by playing with the registry and writing a couple lines of command in a powershell profile.

Since running automated jobs on autosys is our ultimate goal, our team used to write batch scripts because that’s what an autosys command accepts. As such, I had to use a batch file in order for autosys to understand what to do, since its batch prompt.

And through this batch file, I am launching powershell file.

At some point, if I ran the powershell script standalone through autosys, I would get an execution policy error (on server).

We had to include the execution policy line I posted above in the batch file in order for the powershell script to run through autosys. And hence, I have always been writing and testing my powershell s ripts through this batch file to launch powershell session.

All I wanted from this post, is I dont like the looping exit code 0 output. The script is working great, I just done like the display. That’s all there is to this thread.

I just want to know if I can suppress this exit code 0, by having some sort of where clause on out-null.

[quote quote=140941]It's a tricky state If we don't know "what we don't know" . There are very few instances where somebody get solution or proper help Within first few replies in a forum. All participants are here for some reason, few will be seeking help, few will help them. During the course there will be lot of questions and cross questioning. Nobody knows everything. So please bare with the team.

So, how does autosys executes a script ? does it support PowerShell by default ? . It will be good if you can give more details on how you are using autosys where the script will finally go in.

[/quote]
I think we posted at the exact same time Haha


But anyways, in addition to my response above, autosys takes a command in the following format:

Batch.bat script.ps1 argument (optional)

Of course you can include as many scripts as you want in that command line, but thts the crux of setting up a command job in autosys

I also use autosys and that has nothing to do with this, even calling powershell from the bat file has nothing to do with this

the fact that you are calling powershell and then from within powershell calling bat files is the issue.
once you lunch powershell script you should be able to do everything from that script.