Share via


Use Powershell to Execute C# Parameterized Console Application

Question

Wednesday, July 22, 2015 8:51 PM

I have been trying to pass arguments to a C# 4.0 console application from Powershell 2.0 and continue to get an "IndexOutOfRangeError" Here is the code in Visual Studio 2013 and in PowerShell.  Could you please tell me what is wrong here?

Here is the Powershell

All replies (6)

Thursday, July 23, 2015 5:10 AM ✅Answered

When the script is ran without the quotes around the text for $exepath, notepad starts with no file loaded.

PS C:\> 
[string]$exePath = notepad.exe
$psi = New-Object System.Diagnostics.ProcessStartInfo $exePath
$psi.Arguments = "C:\Test.txt"
[System.Diagnostics.Process]::Start($psi)

Exception calling "Start" with "1" argument(s): "Cannot start process because a file name has not been provided."
At line:5 char:1
+ [System.Diagnostics.Process]::Start($psi)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException
 

When quotes are around text for $exepath, notepad starts properly with the indicated file loaded.

[string]$exePath = "notepad.exe"
$psi = New-Object System.Diagnostics.ProcessStartInfo $exePath
$psi.Arguments = "C:\Test.txt"
[System.Diagnostics.Process]::Start($psi)


Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                      
                     --                                                       
      4       3      884       1828    11     0.00  15184 notepad     

Wednesday, July 22, 2015 10:27 PM

It doesn't make much sense to post C# code in a scripting forum?  You are not using a script.  You are trying to debug an EXE.

\(ツ)_/


Wednesday, July 22, 2015 10:29 PM

Perhaps:

<$ps.Arguments=@('2015'>)

\(ツ)_/


Wednesday, July 22, 2015 10:32 PM

Also this:

[system.diagnostics.process]::Start('Myprogram.exe'','2015')

\(ツ)_/


Thursday, July 23, 2015 3:43 PM

Since i am not sure whether the problem is in the Powershell script or the C#, I have included both.


Thursday, July 23, 2015 3:51 PM

You should be using a DL and not an EXE to do this.  A DLL can be loaded  into PowerShell natively.

If you just want to runthe program inPowerShell thenjsut run it;

PS >PdfPageCOunt.ClientConsole 2015

If that doesn't work then you have a program issue.

YOu canalso easily use:

Start-Process 'PdfPageCOunt.ClientConsole' -ArgumentList  '2015'

\(ツ)_/