ActiveXPoSH V3 - Return Exit code

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
dtmoreno
Posts: 3
Last visit: Wed Jul 12, 2023 12:35 pm

ActiveXPoSH V3 - Return Exit code

Post by dtmoreno »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: PrimalScript 2017
32 or 64 bit version of product: 64
Operating system: W10
32 or 64 bit OS: 64

*** Please add details and screenshots as needed below. ***

I'm using ActiveX powershell V3 in an HTM application and use the command:
ActiveXPosh.Execute(A PS command) to execute a powershell command.
I'd like to set a variable to the command exit code to determine if the powershell
command executed without error.

How is that done using ActiveX Powershell?
Example please?

thanks
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: ActiveXPoSH V3 - Return Exit code

Post by Alexander Riedel »

Set your variable as needed, e..g $MyExitCode
Then use
exitcode = ActiveXPosh.GetValue("$MyExitCode")
Please note that this is returned as a string. If you need another type you will need to convert it according to your needs.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
dtmoreno
Posts: 3
Last visit: Wed Jul 12, 2023 12:35 pm

Re: ActiveXPoSH V3 - Return Exit code

Post by dtmoreno »

Hi

Just so I understand...Does this look ok?
=============================
myresults = ActiveXPosh.Execute("get-ADUser ADuserthatdoesntexist")
exitcode = ActiveXPosh.GetValue("$MyExitCode")

If (exitcode > 0) then
msg="Failed"
else
msg="succeeded"
End If
wscript.echo (msg)
=====================================
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: ActiveXPoSH V3 - Return Exit code

Post by Alexander Riedel »

Nope. Not going to work.
You cannot transport objects across from PowerShell to VBScript.

"myresults = ActiveXPosh.Execute("get-ADUser ADuserthatdoesntexist")"
You assume that MyResults contains the values returned from Get-ADUser. It will not.

"exitcode = ActiveXPosh.GetValue("$MyExitCode")"
Since you are not setting $MyExitCode anywhere it will simply be empty.

"If (exitcode > 0) then"
As I pointed out before, GetValue returns a string and you need to convert it to another type if need. You don't do that here, so a > 0 will simply not be the right comparison
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
dtmoreno
Posts: 3
Last visit: Wed Jul 12, 2023 12:35 pm

Re: ActiveXPoSH V3 - Return Exit code

Post by dtmoreno »

So....what do you recommend?
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: ActiveXPoSH V3 - Return Exit code

Post by Alexander Riedel »

I have moved the topic to the general PowerShell forum.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked