-2

I have a simple .bat file that executes a exe file and also passes some parameters in txt file. How do i acheive the same in powershell script (.ps1 file) ?

.bat file content:

@echo on
C:\Windows\System32\cmd.exe /C "C:\Program Files\BMC Software\AtriumCore\cmdb\server64\bin\cmdbdiag.exe" -u test -p test -s remedyar -t 41900 < "C:\Program Files\BMC Software\ARSystem\diserver\data-integration\batch\CleanupInputs.txt" > "C:\Program Files\BMC Software\ARSystem\diserver\data-integration\batch\Snow_Output\DailyOutput.log"
Exit 0
Subbu
  • 41
  • 1
  • 8
  • 2
    take a look at `Get-Help Start-Process -Examples`. [*grin*] – Lee_Dailey Jun 01 '21 at 13:39
  • @Lee_Dailey, `Start-Process` is virtually never the right tool for invoking _console_ applications (synchronously, with their streams connected to PowerShell's) - see [this answer](https://stackoverflow.com/a/51334633/45375). – mklement0 Jun 01 '21 at 14:08

1 Answers1

2

Fundamentally, you invoke console applications the same way in PowerShell as you do in cmd.exe, but there are important differences:

# If you really want to emulate `@echo ON` - see comments below.
Set-PSDebug -Trace 1 

# * PowerShell doesn't support `<` for *input* redirection, so you must
#   use Get-Content to *pipe* a file's content to another command.
# * `>` for *output* redirection *is* supported, but beware encoding problems:
#     * Windows PowerShell creates a "Unicode" (UTF-16LE) file,
#     * PowerShell (Core, v6+) a BOM-less UTF-8 file.
#     * To control the encoding, pipe to Out-File / Set-Content with -Encoding
# * For syntactic reasons, because your executable path is *quoted*, you must
#   invoke it via `&`, the call operator.
Get-Content "C:\..\CleanupInputs.txt" | 
  & "C:\...\cmdbdiag.exe" -u test -p test -s remedyar -t 41900 > "C:\...\DailyOutput.log"

# Turn tracing back off.
Set-PSDebug -Trace 0

exit 0

Note:

  • For brevity I've replaced the long directory paths in your command with ...

  • Character-encoding caveats:

    • When PowerShell communicates with external programs, it only "speaks text" (and it generally never passes raw bytes through its pipelines (as of v7.2)), which therefore involves potentially multiple passes of encoding and decoding strings; specifically:

    • Get-Content doesn't just path a text file's raw bytes through, it decodes the content into .NET strings and then sends the content line by line through the pipeline. If the input file lacks a BOM, Windows PowerShell assumes the active ANSI encoding, whereas PowerShell (Core) 7+ assumes UTF-8; you can use the -Encoding parameter to specify the encoding explicitly.

    • Since the receiving command is an external program (executable), PowerShell (re)-encodes the lines before sending them to the program, based on the $OutputEncoding preference variable , which defaults to ASCII(!) in Windows PowerShell, and UTF-8 in PowerShell (Core) 7+.

    • Since > - effectively an alias for Out-File - is used to redirect the external program to a file, another round of decoding and encoding happens:

      • PowerShell first decodes the external program's output into .NET strings, based on the character encoding stored in [Console]::OutputEncoding, which defaults to the system's active OEM code page.
      • Then Out-File encodes the decoded strings based on its default encoding, which is UTF-16LE ("Unicode") in Windows PowerShell, and BOM-less UTF-8 in PowerShell (Core); to control the encoding, you need to use Out-File (or Set-Content) explicitly and use its -Encoding parameter.

See also:

  • about_Redirection

  • &, the call operator

  • This answer discusses default encodings in both PowerShell editions; the short of it: they vary wildly in Windows PowerShell, but PowerShell (Core) 7+ now consistently used BOM-less UTF-8.


Re execution tracing: use of @echo ON in your batch file and how it compares to PowerShell's
Set-PSDebug -Trace 1:

  • Batch files typically run with @echo OFF so as not to echo every command itself before printing its output.
    • @echo ON (or omitting an @echo ON/OFF statement altogether) can be helpful for diagnosing problems during execution, however.
    • Set-PSDebug -Trace 1 is similar to @echo ON, but it has one disadvantage: the raw source code of commands is echoed, which means that you won't see the value of embedded variable references and expressions - see this answer for more information.
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thanks for your answer. I tried this, however it neither gave any error nor ran the exe file. – Subbu Jun 02 '21 at 04:17
  • @Subbu, just to state the obvious: you need to use the _original_ paths when you actually try the code, not my abbreviated versions. If you do that, threre's no reason for the code to behave differently than your batch file - except perhaps for _character-encoding issues_ (please see my update). If the code doesn't work with the original paths, you need to describe (a) what you expect to happen and (b) what actually happens and how it differs from what you expect. Without that, everyone loses: (a) you won't get a solution and (b) if there's a flaw in the answer, it won't help future readers. – mklement0 Jun 02 '21 at 13:02