Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3342698
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:52:10+00:00 2026-05-18T00:52:10+00:00

I am writing a rather lengthy PowerShell script to perform a lot of functions.

  • 0

I am writing a rather lengthy PowerShell script to perform a lot of functions. For the most part, everything is going very well.

However, another function I am running at all levels of the script is a very detailed log. I’ll post the command line portion as an example (simplified):

    $Batch = $RunMe[1]
    $ResultCode = (Invoke-Expression $Batch -ErrorAction Stop)
    $ResultCode

My expected result is:

Return Code 0

Then I would log it. Instead I am getting something like:

C:\batchfiles\batchfile.bat argument

Which is the command I am using Invoke-Expression to execute.

Some quick explanation of the variables:

$RunMe is an array that stores the commands (0 is either “Batch” or “Proc” and 1 is the command to run)

$Batch is created in the code as written (this is the first reference to it)

$CurrFileToExec is the current file being processed (the script runs on a folder at a time). I’m using it for a string replace for the command line I got from SQL.

I can’t get $ResultCode to pass the correct output of the command. It just passes the command line function ($Batch) again.

As I said, the actual functionality part works great, but I can’t get that return code to my logfile.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:52:10+00:00Added an answer on May 18, 2026 at 12:52 am

    If I can presume $batch contains external executables, the return code will be in the automatic variable $LASTEXITCODE – invoke-expression’s return consists of things written to STDOUT and STDERR by external applications or things written to powershell’s output or error streams by script. It will not contain the executable’s dos-style return code, if you called one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best practice for writing a rather large wcf service, containing a
I'm writing an ASP.NET 3.5 web application. There is a rather complex input form
When writing multi-threaded applications, one of the most common problems experienced are deadlocks. My
When writing multithreaded applications, one of the most common problems experienced is race conditions.
Most of the languages like C++ when writing into a file, put an EOF
I'm writing a userscript for a website that uses Prototype. Rather than use the
I have a rather simple threading question. I'm writing a simple utility that will
Writing a rather simple command line tool in ruby I need to report meaningful
I'm writing a rather simple application that uses GWT, Hibernate and Google Guice (with
I am writing a python script that parses a 3D model file from one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.